-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprompts-config.test.js
More file actions
183 lines (159 loc) · 6.77 KB
/
Copy pathprompts-config.test.js
File metadata and controls
183 lines (159 loc) · 6.77 KB
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
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
BUILTIN_PROMPTS,
CUSTOM_NAME_RE,
deleteCustomPrompt,
mergePrompts,
promptsConfigPath,
readPromptsConfig,
resetBuiltin,
substituteTemplate,
upsertPrompt,
writePromptsConfig,
} from "../src/prompts-config.js";
const tmpDirs = [];
function freshDir() {
const dir = mkdtempSync(join(tmpdir(), "perplexity-prompts-"));
tmpDirs.push(dir);
return dir;
}
afterEach(() => {
delete process.env.PERPLEXITY_CONFIG_DIR;
for (const dir of tmpDirs.splice(0)) {
rmSync(dir, { recursive: true, force: true });
}
});
const sample = (name = "my-cmd") => ({
name,
description: "desc",
arguments: [{ name: "x", description: "the x", required: true }],
template: "value: {x}",
});
describe("substituteTemplate", () => {
it("replaces a single placeholder", () => {
expect(substituteTemplate("hi {topic}", { topic: "AI" })).toBe("hi AI");
});
it("replaces multiple placeholders", () => {
expect(substituteTemplate("{a} vs {b}", { a: "x", b: "y" })).toBe("x vs y");
});
it("substitutes a missing arg with an empty string", () => {
expect(substituteTemplate("hi {topic}!", {})).toBe("hi !");
});
it("coerces non-string values", () => {
expect(substituteTemplate("n={count}", { count: 3 })).toBe("n=3");
});
it("leaves non-arg braces untouched (uppercase-leading / spaces)", () => {
expect(substituteTemplate("{Field Name} and {SUM}", { Field: "x" })).toBe("{Field Name} and {SUM}");
});
});
describe("CUSTOM_NAME_RE", () => {
it("accepts lowercase, digits, hyphens with a leading letter", () => {
expect(CUSTOM_NAME_RE.test("my-prompt-2")).toBe(true);
});
it("rejects leading digit, uppercase, spaces, and empty", () => {
for (const bad of ["2cmd", "Cmd", "my cmd", "", "_x", "my_cmd"]) {
expect(CUSTOM_NAME_RE.test(bad)).toBe(false);
}
});
});
describe("mergePrompts", () => {
it("returns built-ins flagged isBuiltin, not modified, by default", () => {
const merged = mergePrompts({ overrides: {}, custom: [] });
expect(merged).toHaveLength(BUILTIN_PROMPTS.length);
expect(merged.every((p) => p.isBuiltin && !p.isModified)).toBe(true);
expect(merged.map((p) => p.name)).toEqual(BUILTIN_PROMPTS.map((b) => b.name));
});
it("applies an override: keeps the built-in name, marks modified, uses override content", () => {
const name = BUILTIN_PROMPTS[0].name;
const merged = mergePrompts({
overrides: { [name]: { name, description: "new", arguments: [], template: "T" } },
custom: [],
});
const target = merged.find((p) => p.name === name);
expect(target).toMatchObject({ name, description: "new", template: "T", isBuiltin: true, isModified: true });
});
it("appends custom prompts after built-ins, flagged not-builtin", () => {
const merged = mergePrompts({ overrides: {}, custom: [sample("zed-cmd")] });
const custom = merged.find((p) => p.name === "zed-cmd");
expect(custom).toMatchObject({ isBuiltin: false, isModified: false });
expect(merged[merged.length - 1].name).toBe("zed-cmd");
});
it("filters a custom prompt that shadows a built-in name", () => {
const name = BUILTIN_PROMPTS[0].name;
const merged = mergePrompts({ overrides: {}, custom: [{ ...sample(), name }] });
expect(merged.filter((p) => p.name === name)).toHaveLength(1);
expect(merged.find((p) => p.name === name)?.isBuiltin).toBe(true);
});
});
describe("upsert / delete / reset (pure)", () => {
it("upsert of a built-in name writes an override, not a custom entry", () => {
const name = BUILTIN_PROMPTS[0].name;
const next = upsertPrompt({ overrides: {}, custom: [] }, { ...sample(), name });
expect(next.overrides[name]).toBeDefined();
expect(next.custom).toHaveLength(0);
});
it("upsert of a new name appends to custom", () => {
const next = upsertPrompt({ overrides: {}, custom: [] }, sample("new-one"));
expect(next.custom.map((c) => c.name)).toEqual(["new-one"]);
});
it("upsert of an existing custom name replaces it in place", () => {
const base = { overrides: {}, custom: [sample("dup")] };
const next = upsertPrompt(base, { ...sample("dup"), description: "changed" });
expect(next.custom).toHaveLength(1);
expect(next.custom[0].description).toBe("changed");
});
it("upsert strips unknown fields down to the stored shape", () => {
const next = upsertPrompt({ overrides: {}, custom: [] }, {
...sample("clean"),
isBuiltin: true,
isModified: true,
junk: 1,
});
expect(Object.keys(next.custom[0]).sort()).toEqual(["arguments", "description", "name", "template"]);
});
it("deleteCustomPrompt removes only the named custom prompt", () => {
const base = { overrides: {}, custom: [sample("a"), sample("b")] };
expect(deleteCustomPrompt(base, "a").custom.map((c) => c.name)).toEqual(["b"]);
});
it("resetBuiltin clears an override and is a no-op when absent", () => {
const name = BUILTIN_PROMPTS[0].name;
const withOverride = { overrides: { [name]: sample(name) }, custom: [] };
expect(resetBuiltin(withOverride, name).overrides[name]).toBeUndefined();
const empty = { overrides: {}, custom: [] };
expect(resetBuiltin(empty, name)).toEqual(empty);
});
});
describe("read / write round-trip", () => {
it("writes atomically and reads back", () => {
const dir = freshDir();
const config = upsertPrompt({ overrides: {}, custom: [] }, sample("round-trip"));
writePromptsConfig(config, dir);
expect(readPromptsConfig(dir)).toEqual(config);
// trailing newline + pretty-printed
expect(readFileSync(promptsConfigPath(dir), "utf8").endsWith("\n")).toBe(true);
});
it("returns an empty config when the file is missing", () => {
expect(readPromptsConfig(freshDir())).toEqual({ overrides: {}, custom: [] });
});
it("returns an empty config on malformed JSON", () => {
const dir = freshDir();
writeFileSync(promptsConfigPath(dir), "{ not json", "utf8");
expect(readPromptsConfig(dir)).toEqual({ overrides: {}, custom: [] });
});
it("normalizes a config with wrong-typed fields", () => {
const dir = freshDir();
writeFileSync(promptsConfigPath(dir), JSON.stringify({ overrides: [], custom: "nope" }), "utf8");
expect(readPromptsConfig(dir)).toEqual({ overrides: {}, custom: [] });
});
it("honors PERPLEXITY_CONFIG_DIR as the default location", () => {
const dir = freshDir();
process.env.PERPLEXITY_CONFIG_DIR = dir;
const config = upsertPrompt({ overrides: {}, custom: [] }, sample("env-default"));
writePromptsConfig(config);
expect(promptsConfigPath()).toBe(join(dir, "prompts.json"));
expect(readPromptsConfig()).toEqual(config);
});
});