import{beforeEach,describe,expect,test}from"vitest";import{z}from"zod";import{zNewPromptSchema}from"@karakeep/shared/types/prompts";importtype{CustomTestContext}from"../testUtils";import{defaultBeforeEach}from"../testUtils";beforeEach<CustomTestContext>(defaultBeforeEach(true));describe("Prompts Routes",()=>{test<CustomTestContext>("create prompt",async({ apiCallers })=>{constapi=apiCallers[0].prompts;constnewPromptInput: z.infer<typeofzNewPromptSchema>={text: "Test prompt text",appliesTo: "summary",};constcreatedPrompt=awaitapi.create({ ...newPromptInput});expect(createdPrompt).toMatchObject({text: newPromptInput.text,appliesTo: newPromptInput.appliesTo,enabled: true,});constprompts=awaitapi.list();constpromptFromList=prompts.find((p)=>p.id===createdPrompt.id);expect(promptFromList).toBeDefined();expect(promptFromList?.text).toEqual(newPromptInput.text);});test<CustomTestContext>("update prompt",async({ apiCallers })=>{constapi=apiCallers[0].prompts;// First, create a promptconstcreatedPrompt=awaitapi.create({text: "Original text",appliesTo: "summary",});// Update itconstupdatedPrompt=awaitapi.update({promptId: createdPrompt.id,text: "Updated text",appliesTo: "summary",enabled: false,});expect(updatedPrompt.text).toEqual("Updated text");expect(updatedPrompt.appliesTo).toEqual("summary");expect(updatedPrompt.enabled).toEqual(false);// Instead of api.getPrompt, use api.list() to verifyconstprompts=awaitapi.list();constpromptFromList=prompts.find((p)=>p.id===createdPrompt.id);expect(promptFromList).toBeDefined();expect(promptFromList?.text).toEqual("Updated text");expect(promptFromList?.enabled).toEqual(false);// Test updating a non-existent promptawaitexpect(()=>api.update({promptId: "non-existent-id",text: "Should fail",appliesTo: "summary",enabled: true,// Assuming this matches the schema}),).rejects.toThrow(/Prompt not found/);});test<CustomTestContext>("list prompts",async({ apiCallers })=>{constapi=apiCallers[0].prompts;constemptyPrompts=awaitapi.list();expect(emptyPrompts).toEqual([]);constprompt1Input: z.infer<typeofzNewPromptSchema>={text: "Prompt 1",appliesTo: "summary",};awaitapi.create(prompt1Input);constprompt2Input: z.infer<typeofzNewPromptSchema>={text: "Prompt 2",appliesTo: "summary",};awaitapi.create(prompt2Input);constprompts=awaitapi.list();expect(prompts.length).toEqual(2);expect(prompts.some((p)=>p.text==="Prompt 1")).toBeTruthy();expect(prompts.some((p)=>p.text==="Prompt 2")).toBeTruthy();});test<CustomTestContext>("delete prompt",async({ apiCallers })=>{constapi=apiCallers[0].prompts;// Create a promptconstcreatedPromptInput: z.infer<typeofzNewPromptSchema>={text: "To be deleted",appliesTo: "summary",};constcreatedPrompt=awaitapi.create(createdPromptInput);// Delete itawaitapi.delete({promptId: createdPrompt.id});// Instead of api.getPrompt, use api.list() to verifyconstprompts=awaitapi.list();expect(prompts.some((p)=>p.id===createdPrompt.id)).toBeFalsy();});test<CustomTestContext>("privacy for prompts",async({ apiCallers })=>{constuser1PromptInput: z.infer<typeofzNewPromptSchema>={text: "User 1 prompt",appliesTo: "summary",};constuser1Prompt=awaitapiCallers[0].prompts.create(user1PromptInput);constuser2PromptInput: z.infer<typeofzNewPromptSchema>={text: "User 2 prompt",appliesTo: "summary",};constuser2Prompt=awaitapiCallers[1].prompts.create(user2PromptInput);// User 1 should not access User 2's promptawaitexpect(()=>apiCallers[0].prompts.delete({promptId: user2Prompt.id}),).rejects.toThrow(/User is not allowed to access resource/);// List should only show the correct user's promptsconstuser1Prompts=awaitapiCallers[0].prompts.list();expect(user1Prompts.length).toEqual(1);expect(user1Prompts[0].id).toEqual(user1Prompt.id);constuser2Prompts=awaitapiCallers[1].prompts.list();expect(user2Prompts.length).toEqual(1);expect(user2Prompts[0].id).toEqual(user2Prompt.id);});});