1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
import { beforeEach, describe, expect, test } from "vitest";
import { z } from "zod";
import { zNewPromptSchema } from "@karakeep/shared/types/prompts";
import type { CustomTestContext } from "../testUtils";
import { defaultBeforeEach } from "../testUtils";
beforeEach<CustomTestContext>(defaultBeforeEach(true));
describe("Prompts Routes", () => {
test<CustomTestContext>("create prompt", async ({ apiCallers }) => {
const api = apiCallers[0].prompts;
const newPromptInput: z.infer<typeof zNewPromptSchema> = {
text: "Test prompt text",
appliesTo: "summary",
};
const createdPrompt = await api.create({ ...newPromptInput });
expect(createdPrompt).toMatchObject({
text: newPromptInput.text,
appliesTo: newPromptInput.appliesTo,
enabled: true,
});
const prompts = await api.list();
const promptFromList = prompts.find((p) => p.id === createdPrompt.id);
expect(promptFromList).toBeDefined();
expect(promptFromList?.text).toEqual(newPromptInput.text);
});
test<CustomTestContext>("update prompt", async ({ apiCallers }) => {
const api = apiCallers[0].prompts;
// First, create a prompt
const createdPrompt = await api.create({
text: "Original text",
appliesTo: "summary",
});
// Update it
const updatedPrompt = await api.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 verify
const prompts = await api.list();
const promptFromList = 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 prompt
await expect(() =>
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 }) => {
const api = apiCallers[0].prompts;
const emptyPrompts = await api.list();
expect(emptyPrompts).toEqual([]);
const prompt1Input: z.infer<typeof zNewPromptSchema> = {
text: "Prompt 1",
appliesTo: "summary",
};
await api.create(prompt1Input);
const prompt2Input: z.infer<typeof zNewPromptSchema> = {
text: "Prompt 2",
appliesTo: "summary",
};
await api.create(prompt2Input);
const prompts = await api.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 }) => {
const api = apiCallers[0].prompts;
// Create a prompt
const createdPromptInput: z.infer<typeof zNewPromptSchema> = {
text: "To be deleted",
appliesTo: "summary",
};
const createdPrompt = await api.create(createdPromptInput);
// Delete it
await api.delete({ promptId: createdPrompt.id });
// Instead of api.getPrompt, use api.list() to verify
const prompts = await api.list();
expect(prompts.some((p) => p.id === createdPrompt.id)).toBeFalsy();
});
test<CustomTestContext>("privacy for prompts", async ({ apiCallers }) => {
const user1PromptInput: z.infer<typeof zNewPromptSchema> = {
text: "User 1 prompt",
appliesTo: "summary",
};
const user1Prompt = await apiCallers[0].prompts.create(user1PromptInput);
const user2PromptInput: z.infer<typeof zNewPromptSchema> = {
text: "User 2 prompt",
appliesTo: "summary",
};
const user2Prompt = await apiCallers[1].prompts.create(user2PromptInput);
// User 1 should not access User 2's prompt
await expect(() =>
apiCallers[0].prompts.delete({ promptId: user2Prompt.id }),
).rejects.toThrow(/User is not allowed to access resource/);
// List should only show the correct user's prompts
const user1Prompts = await apiCallers[0].prompts.list();
expect(user1Prompts.length).toEqual(1);
expect(user1Prompts[0].id).toEqual(user1Prompt.id);
const user2Prompts = await apiCallers[1].prompts.list();
expect(user2Prompts.length).toEqual(1);
expect(user2Prompts[0].id).toEqual(user2Prompt.id);
});
});
|