-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsources-config.test.ts
More file actions
300 lines (259 loc) · 11 KB
/
Copy pathsources-config.test.ts
File metadata and controls
300 lines (259 loc) · 11 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { afterEach, describe, expect, it } from "bun:test";
import { mkdirSync, mkdtempSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
DEFAULT_OBSIDIAN_EXCLUDE_GLOBS,
addDiscordSource,
addObsidianSource,
getSourcesConfigPath,
loadSourcesConfig,
markSourceIndexed,
parseDiscordSettings,
removeSource,
} from "./sources-config";
let dir = "";
afterEach(() => {
dir = "";
});
function tmp(): string {
dir = mkdtempSync(join(tmpdir(), "signet-sources-"));
return dir;
}
describe("sources-config", () => {
it("adds an Obsidian vault source as read-only config", () => {
const agentsDir = tmp();
const vault = join(agentsDir, "vault");
mkdirSync(vault, { recursive: true });
const result = addObsidianSource(
{ root: vault, name: "Research Vault", now: "2026-01-01T00:00:00.000Z" },
agentsDir,
);
expect(result.ok).toBe(true);
if (result.ok === false) throw new Error(result.error);
expect(result.created).toBe(true);
expect(result.source.kind).toBe("obsidian");
expect(result.source.mode).toBe("read-only");
expect(result.source.enabled).toBe(true);
expect(result.source.name).toBe("Research Vault");
const config = loadSourcesConfig(agentsDir);
expect(config.sources).toHaveLength(1);
expect(config.sources[0]?.root).toBe(vault);
expect(JSON.parse(readFileSync(getSourcesConfigPath(agentsDir), "utf8")).sources[0].mode).toBe("read-only");
});
it("merges custom Obsidian excludes with default privacy excludes", () => {
const agentsDir = tmp();
const vault = join(agentsDir, "vault");
mkdirSync(vault, { recursive: true });
const result = addObsidianSource({ root: vault, excludeGlobs: ["private/**", "**/.obsidian/**"] }, agentsDir);
expect(result.ok).toBe(true);
if (result.ok === false) throw new Error(result.error);
expect(result.source.excludeGlobs).toEqual([...DEFAULT_OBSIDIAN_EXCLUDE_GLOBS, "private/**"]);
});
it("updates an existing Obsidian source instead of duplicating it", () => {
const agentsDir = tmp();
const vault = join(agentsDir, "vault");
mkdirSync(vault, { recursive: true });
const first = addObsidianSource({ root: vault, name: "Vault A", now: "2026-01-01T00:00:00.000Z" }, agentsDir);
const second = addObsidianSource({ root: vault, name: "Vault B", now: "2026-01-02T00:00:00.000Z" }, agentsDir);
expect(first.ok).toBe(true);
expect(second.ok).toBe(true);
if (second.ok === false) throw new Error(second.error);
expect(second.created).toBe(false);
expect(second.source.name).toBe("Vault B");
expect(loadSourcesConfig(agentsDir).sources).toHaveLength(1);
});
it("removes a source by id from the config", () => {
const agentsDir = tmp();
const vault = join(agentsDir, "vault");
mkdirSync(vault, { recursive: true });
const added = addObsidianSource({ root: vault, name: "Vault A", now: "2026-01-01T00:00:00.000Z" }, agentsDir);
expect(added.ok).toBe(true);
if (added.ok === false) throw new Error(added.error);
const removed = removeSource(added.source.id, agentsDir);
expect(removed.ok).toBe(true);
if (removed.ok === false) throw new Error(removed.error);
expect(removed.source.id).toBe(added.source.id);
expect(loadSourcesConfig(agentsDir).sources).toEqual([]);
});
it("uses unique temp files and leaves no stale lock or tmp files after sequential mutations", () => {
const agentsDir = tmp();
const vaultA = join(agentsDir, "vault-a");
const vaultB = join(agentsDir, "vault-b");
mkdirSync(vaultA, { recursive: true });
mkdirSync(vaultB, { recursive: true });
const first = addObsidianSource({ root: vaultA, name: "Vault A" }, agentsDir);
const second = addObsidianSource({ root: vaultB, name: "Vault B" }, agentsDir);
expect(first.ok).toBe(true);
expect(second.ok).toBe(true);
if (first.ok === false || second.ok === false) throw new Error("expected sources to be added");
markSourceIndexed(first.source.id, "2026-01-03T00:00:00.000Z", agentsDir);
const removed = removeSource(second.source.id, agentsDir);
expect(removed.ok).toBe(true);
const cfg = loadSourcesConfig(agentsDir);
expect(cfg.sources.map((source) => source.id)).toEqual([first.source.id]);
expect(cfg.sources[0]?.lastIndexedAt).toBe("2026-01-03T00:00:00.000Z");
expect(
readdirSync(agentsDir).some((name) => name.includes("sources.json.tmp") || name === "sources.json.lock"),
).toBe(false);
});
it("refuses to overwrite a corrupt sources config during mutating operations", () => {
const agentsDir = tmp();
const vault = join(agentsDir, "vault");
mkdirSync(vault, { recursive: true });
const configPath = getSourcesConfigPath(agentsDir);
writeFileSync(configPath, "{ not valid json", "utf8");
expect(loadSourcesConfig(agentsDir).sources).toEqual([]);
const result = addObsidianSource({ root: vault, name: "Vault A" }, agentsDir);
expect(result.ok).toBe(false);
if (result.ok === true) throw new Error("expected addObsidianSource to fail");
expect(result.error).toContain("refusing to overwrite");
expect(readFileSync(configPath, "utf8")).toBe("{ not valid json");
});
it("refuses to remove sources when the config is corrupt", () => {
const agentsDir = tmp();
const configPath = getSourcesConfigPath(agentsDir);
writeFileSync(configPath, "{ not valid json", "utf8");
const removed = removeSource("obsidian:any", agentsDir);
expect(removed.ok).toBe(false);
if (removed.ok === true) throw new Error("expected removeSource to fail");
expect(removed.error).toContain("refusing to overwrite");
expect(readFileSync(configPath, "utf8")).toBe("{ not valid json");
});
it("returns a not-found result when removing an unknown source", () => {
const agentsDir = tmp();
const removed = removeSource("obsidian:missing", agentsDir);
expect(removed.ok).toBe(false);
if (removed.ok === true) throw new Error("expected removeSource to fail");
expect(removed.error).toContain("not found");
});
describe("discord source", () => {
it("adds a Discord source with guild IDs and token ref", () => {
const agentsDir = tmp();
const result = addDiscordSource(
{
guildIds: ["123456789012345678"],
tokenRef: "DISCORD_BOT_TOKEN",
name: "My Discord Server",
now: "2026-01-01T00:00:00.000Z",
},
agentsDir,
);
expect(result.ok).toBe(true);
if (result.ok === false) throw new Error(result.error);
expect(result.created).toBe(true);
expect(result.source.kind).toBe("discord");
expect(result.source.mode).toBe("read-only");
expect(result.source.name).toBe("My Discord Server");
expect(result.source.root).toBe("");
expect(result.source.settings).toBeDefined();
expect((result.source.settings as { guildIds: string[] }).guildIds).toEqual(["123456789012345678"]);
expect((result.source.settings as { tokenRef: string }).tokenRef).toBe("DISCORD_BOT_TOKEN");
});
it("rejects missing guild IDs", () => {
const agentsDir = tmp();
const result = addDiscordSource({ guildIds: [], tokenRef: "TOKEN" }, agentsDir);
expect(result.ok).toBe(false);
if (result.ok === true) throw new Error("expected failure");
expect(result.error).toContain("guild ID");
});
it("rejects missing token ref", () => {
const agentsDir = tmp();
const result = addDiscordSource({ guildIds: ["123456789012345678"], tokenRef: "" }, agentsDir);
expect(result.ok).toBe(false);
if (result.ok === true) throw new Error("expected failure");
expect(result.error).toContain("token");
});
it("rejects obvious raw Discord bot tokens", () => {
const agentsDir = tmp();
const result = addDiscordSource(
{
guildIds: ["123456789012345678"],
tokenRef: "MTEyMzQ1Njc4OTAxMjM0NTY3OA.c29tZWJvdA.abcdefghijklmnopqrstuvwxYZ0123456789",
},
agentsDir,
);
expect(result.ok).toBe(false);
if (result.ok === true) throw new Error("expected failure");
expect(result.error).toContain("secret reference");
});
it("rejects invalid guild ID format", () => {
const agentsDir = tmp();
const result = addDiscordSource({ guildIds: ["not-a-number"], tokenRef: "TOKEN" }, agentsDir);
expect(result.ok).toBe(false);
if (result.ok === true) throw new Error("expected failure");
expect(result.error).toContain("Invalid Discord guild ID");
});
it("deduplicates by sorted guild IDs", () => {
const agentsDir = tmp();
const first = addDiscordSource(
{ guildIds: ["111111111111111111"], tokenRef: "TOKEN", name: "Server A", now: "2026-01-01T00:00:00.000Z" },
agentsDir,
);
const second = addDiscordSource(
{ guildIds: ["111111111111111111"], tokenRef: "TOKEN2", name: "Server B", now: "2026-01-02T00:00:00.000Z" },
agentsDir,
);
expect(first.ok).toBe(true);
expect(second.ok).toBe(true);
if (second.ok === false) throw new Error(second.error);
expect(second.created).toBe(false);
expect(second.source.name).toBe("Server B");
expect(loadSourcesConfig(agentsDir).sources).toHaveLength(1);
});
it("parseDiscordSettings returns defaults for empty input", () => {
const settings = parseDiscordSettings(undefined);
expect(settings.guildIds).toEqual([]);
expect(settings.tokenRef).toBe("");
expect(settings.maxMessagesPerChannel).toBe(1000);
expect(settings.includeThreads).toBe(true);
});
it("parseDiscordSettings preserves valid fields", () => {
const settings = parseDiscordSettings({
guildIds: ["123"],
tokenRef: "MY_TOKEN",
channelFilter: ["general"],
maxMessagesPerChannel: 500,
includeThreads: false,
since: "2026-01-01",
});
expect(settings.guildIds).toEqual(["123"]);
expect(settings.tokenRef).toBe("MY_TOKEN");
expect(settings.channelFilter).toEqual(["general"]);
expect(settings.maxMessagesPerChannel).toBe(500);
expect(settings.includeThreads).toBe(false);
expect(settings.since).toBe("2026-01-01T00:00:00.000Z");
});
it("rejects invalid Discord message bounds and since dates", () => {
const agentsDir = tmp();
const invalidMax = addDiscordSource(
{ guildIds: ["123456789012345678"], tokenRef: "TOKEN", maxMessagesPerChannel: -1 },
agentsDir,
);
expect(invalidMax.ok).toBe(false);
if (invalidMax.ok === true) throw new Error("expected max message validation failure");
expect(invalidMax.error).toContain("maxMessagesPerChannel");
const invalidSince = addDiscordSource(
{ guildIds: ["123456789012345678"], tokenRef: "TOKEN", since: "not-a-date" },
agentsDir,
);
expect(invalidSince.ok).toBe(false);
if (invalidSince.ok === true) throw new Error("expected since validation failure");
expect(invalidSince.error).toContain("since");
});
it("removes a Discord source by id", () => {
const agentsDir = tmp();
const added = addDiscordSource(
{ guildIds: ["123456789012345678"], tokenRef: "TOKEN", now: "2026-01-01T00:00:00.000Z" },
agentsDir,
);
expect(added.ok).toBe(true);
if (added.ok === false) throw new Error(added.error);
const removed = removeSource(added.source.id, agentsDir);
expect(removed.ok).toBe(true);
if (removed.ok === false) throw new Error(removed.error);
expect(removed.source.id).toBe(added.source.id);
expect(loadSourcesConfig(agentsDir).sources).toEqual([]);
});
});
});