summaryrefslogtreecommitdiffhomepage
path: root/packages/system-prompt/src/service.test.ts
blob: 37c1c0dbd428ac390518601736367039d8384787 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import type { StorageNamespace } from "@dispatch/kernel";
import { describe, expect, it } from "vitest";
import type { GitSpawnResult, ResolverAdapters, ResolverFs } from "./resolver.js";
import { createSystemPromptService, DEFAULT_TEMPLATE } from "./service.js";

/** In-memory StorageNamespace for tests. */
function memoryStorage(): StorageNamespace {
	const store = new Map<string, string>();
	return {
		get: async (key: string) => store.get(key) ?? null,
		set: async (key: string, value: string) => {
			store.set(key, value);
		},
		delete: async (key: string) => {
			store.delete(key);
		},
		has: async (key: string) => store.has(key),
		keys: async (prefix?: string) =>
			[...store.keys()].filter((k) => (prefix === undefined ? true : k.startsWith(prefix))),
	};
}

function fakeFs(files: ReadonlyMap<string, string>): ResolverFs {
	return {
		readText: async (path: string) => files.get(path) ?? "",
		exists: async (path: string) => files.has(path),
	};
}

const failSpawn = async (): Promise<GitSpawnResult> => ({
	stdout: "",
	stderr: "",
	exitCode: 128,
});

function adapters(files: ReadonlyMap<string, string>): ResolverAdapters {
	return {
		spawn: failSpawn,
		fs: fakeFs(files),
		now: () => new Date("2024-06-15T12:30:00.000Z"),
		platform: () => "linux",
		hostname: () => "myhost",
	};
}

describe("system-prompt service", () => {
	it("construct persists and returns the resolved string", async () => {
		// 14. construct writes to storage and returns the resolved string.
		const storage = memoryStorage();
		const service = createSystemPromptService({
			storage,
			adapters: adapters(new Map([["/proj/AGENTS.md", "RULES"]])),
		});

		const result = await service.construct("conv-1", "/proj", { model: "gpt-4" });

		expect(result).toContain("You are a helpful coding assistant.");
		expect(result).toContain("RULES");
		expect(result).toContain("/proj");
		// persisted under resolved:<conversationId>
		expect(await storage.get("resolved:conv-1")).toBe(result);
	});

	it("get returns persisted value after construct", async () => {
		// 15. after construct, get returns the same string.
		const service = createSystemPromptService({
			storage: memoryStorage(),
			adapters: adapters(new Map()),
		});

		// before construct → null
		expect(await service.get("conv-2")).toBeNull();

		const result = await service.construct("conv-2", "/proj");
		expect(await service.get("conv-2")).toBe(result);
	});

	it("get returns null before construct", async () => {
		const service = createSystemPromptService({
			storage: memoryStorage(),
			adapters: adapters(new Map()),
		});

		expect(await service.get("never-constructed")).toBeNull();
	});

	it("empty/no template stored → default template → non-empty", async () => {
		// 16. no template stored → default template used → resolves to non-empty.
		const service = createSystemPromptService({
			storage: memoryStorage(),
			adapters: adapters(new Map()), // no AGENTS.md
		});

		const result = await service.construct("conv-3", "/proj");

		expect(result.length).toBeGreaterThan(0);
		expect(result).toContain("You are a helpful coding assistant.");
		expect(result).toContain("/proj");
		// no AGENTS.md file → the [if file:AGENTS.md] block is omitted
		expect(result).not.toContain("AGENTS.md");
	});

	it("stored template is used instead of default", async () => {
		const storage = memoryStorage();
		await storage.set("template", "cwd=[prompt:cwd] os=[system:os]");
		const service = createSystemPromptService({
			storage,
			adapters: adapters(new Map()),
		});

		const result = await service.construct("conv-4", "/work");
		expect(result).toBe("cwd=/work os=linux");
	});

	it("empty stored template → empty string", async () => {
		const storage = memoryStorage();
		await storage.set("template", "");
		const service = createSystemPromptService({
			storage,
			adapters: adapters(new Map()),
		});

		const result = await service.construct("conv-5", "/proj");
		expect(result).toBe("");
		expect(await service.get("conv-5")).toBe("");
	});

	it("construct is independent per conversation", async () => {
		const storage = memoryStorage();
		await storage.set("template", "[prompt:cwd]");
		const service = createSystemPromptService({
			storage,
			adapters: adapters(new Map()),
		});

		const a = await service.construct("conv-a", "/dir-a");
		const b = await service.construct("conv-b", "/dir-b");

		expect(a).toBe("/dir-a");
		expect(b).toBe("/dir-b");
		expect(await service.get("conv-a")).toBe("/dir-a");
		expect(await service.get("conv-b")).toBe("/dir-b");
	});

	it("DEFAULT_TEMPLATE contains the expected structure", () => {
		expect(DEFAULT_TEMPLATE).toContain("You are a helpful coding assistant.");
		expect(DEFAULT_TEMPLATE).toContain("[if file:AGENTS.md]");
		expect(DEFAULT_TEMPLATE).toContain("[file:AGENTS.md]");
		expect(DEFAULT_TEMPLATE).toContain("[prompt:cwd]");
	});

	it("getWithMeta on a never-constructed conversation returns { prompt: null, cwd: null }", async () => {
		// 1. never constructed → both fields null.
		const service = createSystemPromptService({
			storage: memoryStorage(),
			adapters: adapters(new Map()),
		});

		const meta = await service.getWithMeta("never-constructed");
		expect(meta).toEqual({ prompt: null, cwd: null, computerId: null });
	});

	it("getWithMeta after construct returns the resolved prompt and the exact cwd", async () => {
		// 2. after construct → prompt + exact cwd passed to construct.
		const service = createSystemPromptService({
			storage: memoryStorage(),
			adapters: adapters(new Map([["/proj/AGENTS.md", "RULES"]])),
		});

		const result = await service.construct("conv-meta", "/proj", { model: "gpt-4" });
		const meta = await service.getWithMeta("conv-meta");

		expect(meta.prompt).toBe(result);
		expect(meta.cwd).toBe("/proj");
	});

	it("get still returns the same value as before (backward compat)", async () => {
		// 3. get() behavior is unchanged by the additive getWithMeta.
		const service = createSystemPromptService({
			storage: memoryStorage(),
			adapters: adapters(new Map()),
		});

		// before construct → null
		expect(await service.get("conv-bc")).toBeNull();

		const result = await service.construct("conv-bc", "/proj");
		expect(await service.get("conv-bc")).toBe(result);
	});

	it("construct called twice with different cwds stores the latest cwd", async () => {
		// 4. second construct overwrites the cwd (not the first).
		const storage = memoryStorage();
		await storage.set("template", "[prompt:cwd]");
		const service = createSystemPromptService({
			storage,
			adapters: adapters(new Map()),
		});

		await service.construct("conv-twice", "/first");
		expect(await storage.get("resolved-cwd:conv-twice")).toBe("/first");

		const second = await service.construct("conv-twice", "/second");
		expect(second).toBe("/second");
		expect(await storage.get("resolved-cwd:conv-twice")).toBe("/second");
		expect(await storage.get("resolved-cwd:conv-twice")).not.toBe("/first");
	});

	it("getWithMeta after a second construct with a different cwd returns the new cwd and new prompt", async () => {
		// 5. getWithMeta reflects the latest construct, not the first.
		const storage = memoryStorage();
		await storage.set("template", "[prompt:cwd]");
		const service = createSystemPromptService({
			storage,
			adapters: adapters(new Map()),
		});

		const first = await service.construct("conv-second", "/dir-a");
		const firstMeta = await service.getWithMeta("conv-second");
		expect(firstMeta).toEqual({ prompt: first, cwd: "/dir-a", computerId: null });

		const second = await service.construct("conv-second", "/dir-b");
		const secondMeta = await service.getWithMeta("conv-second");
		expect(secondMeta).toEqual({ prompt: second, cwd: "/dir-b", computerId: null });
		expect(secondMeta.cwd).not.toBe("/dir-a");
	});
});