import { createSystemInstruction } from "../instructions.setters"; import { getSystemInstructionById, getActiveSystemInstructionsByCity, getSystemInstructionsByToolAndCity, findSystemInstructions, } from "../instructions.getters"; import { ObjectId } from "mongodb"; import { MIS_TOOLS, QUERY_TYPES } from "../instructions.types"; describe("System Instructions Getter Tests", () => { const testCity = "Ashdod"; const otherCity = "Tel Aviv"; describe("getters", () => { let instructionId: string; beforeEach(async () => { const id = await createSystemInstruction({ cityName: testCity, instruction: "Find Me", isActive: true, tool: MIS_TOOLS.findSubject, queryType: QUERY_TYPES.any, tags: ["important", "urgent"], }); instructionId = id.toString(); // for filtering tests await createSystemInstruction({ cityName: testCity, instruction: "Inactive One", isActive: false, tool: MIS_TOOLS.findSubject, queryType: QUERY_TYPES.any, }); await createSystemInstruction({ cityName: otherCity, instruction: "Other City Instruction", isActive: true, tool: MIS_TOOLS.findStreet, queryType: QUERY_TYPES.exactlyOne, }); }); describe("getSystemInstructionById", () => { it("given existing id when fetched then return the document", async () => { const result = await getSystemInstructionById(instructionId); expect(result?._id.toString()).toBe(instructionId); }); it("given non-existent id when fetched then return null", async () => { const result = await getSystemInstructionById( new ObjectId().toHexString(), ); expect(result).toBeNull(); }); }); describe("getActiveSystemInstructionsByCity", () => { it("given city with active instructions when fetched then return only active documents for that city", async () => { const result = await getActiveSystemInstructionsByCity(testCity); expect(result.length).toBe(1); expect(result[0].instruction).toBe("Find Me"); }); it("given city with no instructions when fetched then return empty array", async () => { const result = await getActiveSystemInstructionsByCity("NonExistentCity"); expect(result).toEqual([]); }); }); describe("getSystemInstructionsByToolAndCity", () => { it("given valid city and tool when fetched then return matching instructions", async () => { const result = await getSystemInstructionsByToolAndCity( testCity, MIS_TOOLS.findSubject, ); expect(result.length).toBe(2); // One active, one inactive expect( result.every( (r) => r.cityName === testCity && r.tool === MIS_TOOLS.findSubject, ), ).toBe(true); }); it("given valid city but wrong tool when fetched then return empty array", async () => { const result = await getSystemInstructionsByToolAndCity( testCity, MIS_TOOLS.findStreet, ); expect(result).toEqual([]); }); }); describe("findSystemInstructions", () => { it("given filter by tags when searched then return matching documents", async () => { const result = await findSystemInstructions({ tags: { $in: ["important"] }, }); expect(result.length).toBe(1); expect(result[0].instruction).toBe("Find Me"); }); it("given empty filter when searched then return all documents", async () => { const result = await findSystemInstructions({}); expect(result.length).toBe(3); }); }); }); });