import { getContextNotesCollection, getContextNotes, getActiveContextNotes, createContextNote, setContextNoteEntries, setContextNoteConfig, deleteContextNoteEntry, } from "../contextNotes.getters"; import { ObjectId } from "../../index"; import { createContextNote as buildContextNote, createContextNoteEntry, } from "../../../test-utils/factories/talkpilot/contextNotes"; describe("contextNotes getters", () => { it('should return the "contextNotes" collection', () => { const collection = getContextNotesCollection(); expect(collection.collectionName).toBe("contextNotes"); }); it("should create a context note document and return its id", async () => { const doc = buildContextNote({ notes: [createContextNoteEntry()] }); const id = await createContextNote(doc); expect(id).toBeInstanceOf(ObjectId); const saved = await getContextNotesCollection().findOne({ _id: id }); expect(saved).toBeDefined(); expect(saved?.clientId).toBe(doc.clientId); expect(saved?.notes).toHaveLength(1); }); it("should return documents matching clientId and product", async () => { const clientId = new ObjectId().toString(); await createContextNote( buildContextNote({ clientId, product: "Municipal" }), ); await createContextNote(buildContextNote({ clientId, product: "Clinics" })); await createContextNote(buildContextNote({ product: "Municipal" })); // different client const results = await getContextNotes(clientId, "Municipal"); expect(results).toHaveLength(1); expect(results[0].clientId).toBe(clientId); expect(results[0].product).toBe("Municipal"); }); it("should not return documents for a different product", async () => { const clientId = new ObjectId().toString(); await createContextNote( buildContextNote({ clientId, product: "Municipal" }), ); await createContextNote(buildContextNote({ clientId, product: "Clinics" })); const results = await getContextNotes(clientId, "Rcure"); expect(results).toHaveLength(0); }); it("should return only active notes for a client and product", async () => { const clientId = new ObjectId().toString(); const now = new Date(); const past = new Date(now.getTime() - 10000); const future = new Date(now.getTime() + 10000); const activeNote = createContextNoteEntry({ activeFrom: past, expiresAt: future, }); const expiredNote = createContextNoteEntry({ activeFrom: past, expiresAt: past, }); const notYetActiveNote = createContextNoteEntry({ activeFrom: future, expiresAt: null, }); const noExpiryNote = createContextNoteEntry({ activeFrom: past, expiresAt: null, }); await createContextNote( buildContextNote({ clientId, product: "Municipal", notes: [activeNote, expiredNote, notYetActiveNote, noExpiryNote], }), ); const results = await getActiveContextNotes(clientId, "Municipal"); expect(results).toHaveLength(2); expect(results.map((n) => n._id.toString())).toEqual( expect.arrayContaining([ activeNote._id.toString(), noExpiryNote._id.toString(), ]), ); }); it("should replace the notes array on a document", async () => { const doc = buildContextNote({ notes: [createContextNoteEntry(), createContextNoteEntry()], }); const id = await createContextNote(doc); const newNotes = [createContextNoteEntry({ title: "updated" })]; const updated = await setContextNoteEntries(id, doc.clientId, newNotes); expect(updated).toBe(true); const saved = await getContextNotesCollection().findOne({ _id: id }); expect(saved?.notes).toHaveLength(1); expect(saved?.notes[0].title).toBe("updated"); }); it("should return false when setContextNoteEntries targets a non-existent document", async () => { const result = await setContextNoteEntries(new ObjectId(), "nobody", []); expect(result).toBe(false); }); it("should store and retrieve a systemPrompt set at creation", async () => { const doc = buildContextNote({ systemPrompt: "Treat this information as highly reliable.", }); const id = await createContextNote(doc); const saved = await getContextNotesCollection().findOne({ _id: id }); expect(saved?.systemPrompt).toBe( "Treat this information as highly reliable.", ); }); it("should not touch systemPrompt or product when setContextNoteEntries is called", async () => { const doc = buildContextNote({ notes: [], systemPrompt: "Original instructions.", product: "Municipal", }); const id = await createContextNote(doc); await setContextNoteEntries(id, doc.clientId, [createContextNoteEntry()]); const saved = await getContextNotesCollection().findOne({ _id: id }); expect(saved?.systemPrompt).toBe("Original instructions."); expect(saved?.product).toBe("Municipal"); expect(saved?.notes).toHaveLength(1); }); it("should update systemPrompt via setContextNoteConfig", async () => { const doc = buildContextNote({ notes: [] }); const id = await createContextNote(doc); const updated = await setContextNoteConfig(id, doc.clientId, { systemPrompt: "New instructions.", }); expect(updated).toBe(true); const saved = await getContextNotesCollection().findOne({ _id: id }); expect(saved?.systemPrompt).toBe("New instructions."); }); it("should update product via setContextNoteConfig", async () => { const doc = buildContextNote({ product: "Municipal" }); const id = await createContextNote(doc); const updated = await setContextNoteConfig(id, doc.clientId, { product: "Clinics", }); expect(updated).toBe(true); const saved = await getContextNotesCollection().findOne({ _id: id }); expect(saved?.product).toBe("Clinics"); }); it("should return false from setContextNoteConfig when no fields are provided", async () => { const doc = buildContextNote({}); const id = await createContextNote(doc); const result = await setContextNoteConfig(id, doc.clientId, {}); expect(result).toBe(false); }); it("should return false from setContextNoteConfig for a non-existent document", async () => { const result = await setContextNoteConfig(new ObjectId(), "nobody", { systemPrompt: "x", }); expect(result).toBe(false); }); it("should delete a single note entry by id", async () => { const noteToKeep = createContextNoteEntry(); const noteToDelete = createContextNoteEntry(); const doc = buildContextNote({ notes: [noteToKeep, noteToDelete] }); const id = await createContextNote(doc); const deleted = await deleteContextNoteEntry( id, doc.clientId, noteToDelete._id, ); expect(deleted).toBe(true); const saved = await getContextNotesCollection().findOne({ _id: id }); expect(saved?.notes).toHaveLength(1); expect(saved?.notes[0]._id.toString()).toBe(noteToKeep._id.toString()); }); it("should return false when deleteContextNoteEntry targets a non-existent note", async () => { const doc = buildContextNote({ notes: [] }); const id = await createContextNote(doc); const result = await deleteContextNoteEntry( id, doc.clientId, new ObjectId(), ); expect(result).toBe(false); }); });