import { describe, it, expect } from 'vitest';
import * as Chat from '../../../src/platforms/openai/chat';
import { MessageRole } from '../../../src/platforms/openai';

describe('OpenAI', () => {
  describe.skip('chat', () => {
    describe('normalizeChatMessage', () => {
      it('normalizes a basic string', () => {
        const message = Chat.normalizeChatMessage('This is a user message.');

        expect(message).toHaveLength(1);

        expect(message).toContainEqual({
          role: 'user',
          content: 'This is a user message.',
        });
      });

      it('normalizes a string array', () => {
        const message = Chat.normalizeChatMessage([
          'This is a user message.',
          'This is another user message.',
        ]);

        expect(message).toHaveLength(2);

        expect(message).toContainEqual({
          role: 'user',
          content: 'This is a user message.',
        });

        expect(message).toContainEqual({
          role: 'user',
          content: 'This is another user message.',
        });
      });

      it('normalizes a message object', () => {
        const message = Chat.normalizeChatMessage({
          role: MessageRole.USER,
          content: 'This is a user message.',
        });

        expect(message).toHaveLength(1);

        expect(message).toContainEqual({
          role: 'user',
          content: 'This is a user message.',
        });
      });

      it('normalizes a message object array', () => {
        const message = Chat.normalizeChatMessage([
          {
            role: MessageRole.USER,
            content: 'This is a user message.',
          },
          {
            role: MessageRole.USER,
            content: 'This is another user message.',
          },
        ]);

        expect(message).toHaveLength(2);

        expect(message).toContainEqual({
          role: 'user',
          content: 'This is a user message.',
        });

        expect(message).toContainEqual({
          role: 'user',
          content: 'This is another user message.',
        });
      });
    });

    describe('buildChatFromTranscript', () => {
      it('processes line breaks correctly', () => {
        const message = `
SYSTEM: This is a system message. This is a chat from the
user: This is a user message.
`;
        const parameterized = Chat.buildChatFromTranscript(message);
        const prompts = parameterized({});

        expect(prompts[0]).toHaveLength(1);

        expect(prompts[0]).toContainEqual({
          role: 'system',
          content: 'This is a system message. This is a chat from the user: This is a user message.',
        });
      });

      it('makes distinctions between different dialogues', () => {
        const message = `
SYSTEM: This is a system message. This is a chat from the

user: This is a user message.
`;
        const parameterized = Chat.buildChatFromTranscript(message);
        const prompts = parameterized({});

        expect(prompts[0]).toHaveLength(2);

        expect(prompts[0]).toContainEqual({
          role: 'system',
          content: 'This is a system message. This is a chat from the',
        });

        expect(prompts[0]).toContainEqual({
          role: 'user',
          content: 'This is a user message.',
        });
      });

      it('builds an array of chat messages from a single string.', () => {
        const message = `
SYSTEM: This is a system message.

USER: This is a user message.

SYSTEM: This is another system message.

USER: This is another user message.

ASSISTANT: This is an assistant message.
`;
        const parameterized = Chat.buildChatFromTranscript(message);
        const prompts = parameterized({});

        expect(prompts[0]).toHaveLength(5);

        expect(prompts[0]).toContainEqual({
          role: 'system',
          content: 'This is a system message.',
        });

        expect(prompts[0]).toContainEqual({
          role: 'user',
          content: 'This is a user message.',
        });

        expect(prompts[0]).toContainEqual({
          role: 'system',
          content: 'This is another system message.',
        });

        expect(prompts[0]).toContainEqual({
          role: 'user',
          content: 'This is another user message.',
        });

        expect(prompts[0]).toContainEqual({
          role: 'assistant',
          content: 'This is an assistant message.',
        });
      });

      it('builds multiple chat messages with a divider.', () => {
        const message = `
SYSTEM: This is a system message.
---
USER: This is a user message.
`;
        const parameterized = Chat.buildChatFromTranscript(message);
        const prompts = parameterized({});

        expect(prompts).toHaveLength(2);
        expect(prompts[0]).toContainEqual({
          role: 'system',
          content: 'This is a system message.',
        });
        expect(prompts[1]).toContainEqual({
          role: 'user',
          content: 'This is a user message.',
        });
      });

      it('injects parameters into the chat messages.', () => {
        const message = `
        Say {{name}}. <name> <age> <foo> {{htmlChar}} \\{{escaped}}
        `;

        const parameterized = Chat.buildChatFromTranscript(message);
        const prompts = parameterized({ name: 'Hello', htmlChar: '<html>' });
        expect(prompts[0][0]).toEqual({
          role: 'user',
          content: 'Say Hello. <name> <age> <foo> <html> {{escaped}}',
        });
      });
    });
  });
});
