Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

7.8 KB
Raw Blame
import mongoose, { Types } from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { createModels, createMethods } from '@librechat/data-schemas';
import type { PromptDatabase, PromptGroupRecord, PromptRecord } from './types';
import { createNativePromptAdapter } from './native';
import { createPromptStore } from './store';

let mongo: MongoMemoryServer;
let db: PromptDatabase;
let group: PromptGroupRecord;
let production: PromptRecord;
let draft: PromptRecord;
const author = new Types.ObjectId().toString();

beforeAll(async () => {
  mongo = await MongoMemoryServer.create({ instance: { args: ['--nounixsocket'] } });
  await mongoose.connect(mongo.getUri());
  createModels(mongoose);
  db = createMethods(mongoose);
  await mongoose.models.PromptGroup.syncIndexes();
});

beforeEach(async () => {
  const store = createPromptStore(db);
  const created = await store.createPromptGroup({
    prompt: { prompt: 'Production prompt', type: 'text' },
    group: { name: 'Native group' },
    author,
    authorName: 'Author',
  });
  production = created.prompt as PromptRecord;
  ({ prompt: draft } = await store.savePrompt({
    groupId: production.groupId,
    prompt: { prompt: 'Draft prompt', type: 'chat' },
    author,
  }));
  group = (await store.getPromptGroup(production.groupId)) as PromptGroupRecord;
});

afterEach(async () => {
  jest.restoreAllMocks();
  await mongoose.models.Prompt.deleteMany({});
  await mongoose.models.PromptGroup.deleteMany({});
});

afterAll(async () => {
  await mongoose.disconnect();
  await mongo?.stop();
});

describe('createNativePromptAdapter', () => {
  it('resolves Production from the group inlined revision without reading it again', async () => {
    const adapter = createNativePromptAdapter(createPromptStore(db));
    const readPrompt = jest.spyOn(db, 'getPrompt');

    await expect(
      adapter.resolvePrompt({ group, selection: { type: 'production' } }),
    ).resolves.toEqual({
      ok: true,
      value: {
        source: 'native',
        groupId: group._id,
        promptId: production._id,
        prompt: 'Production prompt',
        type: 'text',
      },
    });
    expect(readPrompt).not.toHaveBeenCalled();
  });

  it('reads the Production revision when the group has no inlined revision', async () => {
    const adapter = createNativePromptAdapter(createPromptStore(db));
    const readPrompt = jest.spyOn(db, 'getPrompt');
    const { productionPrompt: _productionPrompt, ...groupWithoutInlinedRevision } = group;

    await expect(
      adapter.resolvePrompt({
        group: groupWithoutInlinedRevision,
        selection: { type: 'production' },
      }),
    ).resolves.toMatchObject({ ok: true, value: { promptId: production._id } });
    expect(readPrompt).toHaveBeenCalledTimes(1);
  });

  it('resolves an exact revision from a loaded revision without another read', async () => {
    const adapter = createNativePromptAdapter(createPromptStore(db));
    const readPrompt = jest.spyOn(db, 'getPrompt');

    await expect(
      adapter.resolvePrompt({
        group,
        selection: { type: 'exact', promptId: draft._id },
        loadedRevision: draft,
      }),
    ).resolves.toEqual({
      ok: true,
      value: {
        source: 'native',
        groupId: group._id,
        promptId: draft._id,
        prompt: 'Draft prompt',
        type: 'chat',
      },
    });
    expect(readPrompt).not.toHaveBeenCalled();
  });

  it('reads an exact revision when the loaded revision is a different revision', async () => {
    const adapter = createNativePromptAdapter(createPromptStore(db));
    const readPrompt = jest.spyOn(db, 'getPrompt');

    await expect(
      adapter.resolvePrompt({
        group,
        selection: { type: 'exact', promptId: draft._id },
        loadedRevision: production,
      }),
    ).resolves.toMatchObject({ ok: true, value: { promptId: draft._id } });
    expect(readPrompt).toHaveBeenCalledTimes(1);
  });

  it('returns unavailable_selection for a missing revision or group with no Production', async () => {
    const adapter = createNativePromptAdapter(createPromptStore(db));

    await expect(
      adapter.resolvePrompt({
        group,
        selection: { type: 'exact', promptId: new Types.ObjectId().toString() },
      }),
    ).resolves.toEqual({
      ok: false,
      error: { type: 'unavailable_selection', reason: 'revision' },
    });
    await expect(
      adapter.resolvePrompt({
        group: { ...group, productionId: null, productionPrompt: null },
        selection: { type: 'production' },
      }),
    ).resolves.toEqual({
      ok: false,
      error: { type: 'unavailable_selection', reason: 'production' },
    });
  });

  it('does not resolve a revision belonging to another group', async () => {
    const adapter = createNativePromptAdapter(createPromptStore(db));
    const otherGroup = { ...group, _id: new Types.ObjectId().toString() };

    await expect(
      adapter.resolvePrompt({
        group: otherGroup,
        selection: { type: 'exact', promptId: draft._id },
      }),
    ).resolves.toEqual({
      ok: false,
      error: { type: 'unavailable_selection', reason: 'revision' },
    });
    await expect(
      adapter.resolvePrompt({ group: otherGroup, selection: { type: 'production' } }),
    ).resolves.toEqual({
      ok: false,
      error: { type: 'unavailable_selection', reason: 'production' },
    });
  });

  it('resolves a stored revision without a type as text', async () => {
    const adapter = createNativePromptAdapter(createPromptStore(db));
    await mongoose.models.Prompt.collection.updateMany(
      { groupId: new Types.ObjectId(group._id) },
      { $unset: { type: '' } },
    );
    const loaded = (await createPromptStore(db).getPromptGroup(group._id)) as PromptGroupRecord;

    await expect(
      adapter.resolvePrompt({ group: loaded, selection: { type: 'production' } }),
    ).resolves.toMatchObject({ ok: true, value: { promptId: production._id, type: 'text' } });
    await expect(
      adapter.resolvePrompt({
        group: loaded,
        selection: { type: 'exact', promptId: draft._id },
      }),
    ).resolves.toMatchObject({ ok: true, value: { promptId: draft._id, type: 'text' } });
  });

  describe('listRevisions', () => {
    it('lists revisions newest first, numbered from the oldest, with Production marked', async () => {
      const adapter = createNativePromptAdapter(createPromptStore(db));

      await expect(adapter.listRevisions({ group })).resolves.toEqual({
        ok: true,
        value: {
          source: 'native',
          groupId: group._id,
          revisions: [
            { source: 'native', promptId: draft._id, version: 2, isProduction: false },
            { source: 'native', promptId: production._id, version: 1, isProduction: true },
          ],
        },
      });
    });

    it('marks no revision as Production when the group has none', async () => {
      const adapter = createNativePromptAdapter(createPromptStore(db));

      await expect(
        adapter.listRevisions({ group: { ...group, productionId: null } }),
      ).resolves.toEqual({
        ok: true,
        value: {
          source: 'native',
          groupId: group._id,
          revisions: [
            { source: 'native', promptId: draft._id, version: 2, isProduction: false },
            { source: 'native', promptId: production._id, version: 1, isProduction: false },
          ],
        },
      });
    });

    it('returns an empty revision list for a group with no stored revisions', async () => {
      const adapter = createNativePromptAdapter(createPromptStore(db));
      const otherGroup = { ...group, _id: new Types.ObjectId().toString() };

      await expect(adapter.listRevisions({ group: otherGroup })).resolves.toEqual({
        ok: true,
        value: { source: 'native', groupId: otherGroup._id, revisions: [] },
      });
    });
  });
});