Skip to content

Commit 1fee9b1

Browse files
committed
test(server): wait for the settings watcher to listen before editing settings.json
start forks the watcher, so on a busy machine it can return before fs.watch is listening; three tests edited the file in that window and timed out in CI's Test Server shard. Each watcher test now rewrites the same contents until the watcher reloads them before its first edit. With the watcher started 500 ms late, the old tests fail six ways and the new ones pass.
1 parent fc5986a commit 1fee9b1

1 file changed

Lines changed: 51 additions & 34 deletions

File tree

‎apps/server/src/serverSettings.test.ts‎

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ const layerServerSettingsReportingReads = (reads: Queue.Queue<void>) =>
114114
read: (access) => access.pipe(Effect.ensuring(Queue.offer(reads, undefined))),
115115
});
116116

117+
// start forks the watcher, which may not be listening yet when it returns. A test that edits
118+
// settings.json first rewrites the same contents until the watcher reloads them, or a busy
119+
// machine misses the edit. A listening watcher reloads within its 100 ms debounce, so a rewrite
120+
// that gets no reload within a second was made before it listened and leaves nothing behind.
121+
const awaitWatcher = (settingsPath: string, reads: Queue.Queue<void>) =>
122+
Effect.gen(function* () {
123+
const fs = yield* FileSystem.FileSystem;
124+
const contents = yield* fs.readFileString(settingsPath);
125+
yield* Queue.clear(reads);
126+
yield* fs
127+
.writeFileString(settingsPath, contents)
128+
.pipe(
129+
Effect.andThen(Queue.take(reads).pipe(Effect.timeout("1 second"))),
130+
Effect.retry({ times: 5 }),
131+
);
132+
});
133+
117134
const usageScript = {
118135
id: "usage",
119136
name: "Usage",
@@ -364,31 +381,35 @@ it.layer(NodeServices.layer)("server settings", (it) => {
364381
);
365382

366383
it.effect("publishes a default project script that another program adds to settings.json", () =>
367-
Effect.scoped(
368-
Effect.gen(function* () {
369-
const config = yield* ServerConfig.ServerConfig;
370-
const fs = yield* FileSystem.FileSystem;
371-
const service = yield* ServerSettingsModule.ServerSettingsService;
372-
yield* fs.writeFileString(
373-
config.settingsPath,
374-
JSON.stringify({ defaultProjectScripts: [usageScript] }),
375-
);
376-
yield* service.start;
377-
const changes = yield* service.subscribeChanges;
384+
Effect.gen(function* () {
385+
const reads = yield* Queue.unbounded<void>();
386+
yield* Effect.scoped(
387+
Effect.gen(function* () {
388+
const config = yield* ServerConfig.ServerConfig;
389+
const fs = yield* FileSystem.FileSystem;
390+
const service = yield* ServerSettingsModule.ServerSettingsService;
391+
yield* fs.writeFileString(
392+
config.settingsPath,
393+
JSON.stringify({ defaultProjectScripts: [usageScript] }),
394+
);
395+
yield* service.start;
396+
yield* awaitWatcher(config.settingsPath, reads);
397+
const changes = yield* service.subscribeChanges;
378398

379-
yield* fs.writeFileString(
380-
config.settingsPath,
381-
JSON.stringify({ defaultProjectScripts: [usageScript, perfLabScript] }),
382-
);
399+
yield* fs.writeFileString(
400+
config.settingsPath,
401+
JSON.stringify({ defaultProjectScripts: [usageScript, perfLabScript] }),
402+
);
383403

384-
const change = yield* changes.pipe(Stream.runHead, Effect.timeout("2 seconds"));
385-
assert.deepEqual(
386-
Option.getOrUndefined(change)?.defaultProjectScripts.map((script) => script.id),
387-
["usage", "perf-lab"],
388-
);
389-
assert.lengthOf((yield* service.getSettings).defaultProjectScripts, 2);
390-
}),
391-
).pipe(TestClock.withLive, Effect.provide(layerServerSettings())),
404+
const change = yield* changes.pipe(Stream.runHead, Effect.timeout("2 seconds"));
405+
assert.deepEqual(
406+
Option.getOrUndefined(change)?.defaultProjectScripts.map((script) => script.id),
407+
["usage", "perf-lab"],
408+
);
409+
assert.lengthOf((yield* service.getSettings).defaultProjectScripts, 2);
410+
}),
411+
).pipe(Effect.provide(layerServerSettingsReportingReads(reads)));
412+
}).pipe(TestClock.withLive),
392413
);
393414

394415
it.effect("keeps the last good settings while settings.json is invalid and warns once", () => {
@@ -408,7 +429,7 @@ it.layer(NodeServices.layer)("server settings", (it) => {
408429
JSON.stringify({ responseStreamingMode: "turn", defaultProjectScripts: [usageScript] }),
409430
);
410431
yield* service.start;
411-
yield* Queue.clear(reads);
432+
yield* awaitWatcher(config.settingsPath, reads);
412433
const changes = yield* service.subscribeChanges;
413434
assert.isTrue((yield* service.getSettings).continueThreadsAfterServerUpdate);
414435

@@ -469,7 +490,7 @@ it.layer(NodeServices.layer)("server settings", (it) => {
469490
const service = yield* ServerSettingsModule.ServerSettingsService;
470491
yield* fs.writeFileString(config.settingsPath, "{}");
471492
yield* service.start;
472-
yield* Queue.clear(reads);
493+
yield* awaitWatcher(config.settingsPath, reads);
473494
const changes = yield* service.subscribeChanges;
474495
const editPublished = yield* Deferred.make<void>();
475496
const published = yield* Effect.forkChild(
@@ -524,7 +545,7 @@ it.layer(NodeServices.layer)("server settings", (it) => {
524545
const service = yield* ServerSettingsModule.ServerSettingsService;
525546
yield* fs.writeFileString(config.settingsPath, `{ "responseStreamingMode": "turn" }`);
526547
yield* service.start;
527-
yield* Queue.clear(started);
548+
yield* awaitWatcher(config.settingsPath, started);
528549

529550
yield* reloads.close;
530551
yield* fs.writeFileString(config.settingsPath, `{ "responseStreamingMode": `);
@@ -571,11 +592,9 @@ it.layer(NodeServices.layer)("server settings", (it) => {
571592
config.settingsPath,
572593
withServer("http://127.0.0.1:4096", "first-password"),
573594
);
574-
// Startup moves the password into the secret store and rewrites the file, which the
575-
// watcher then reloads.
595+
// Startup moves the password into the secret store and rewrites the file.
576596
yield* service.start;
577-
yield* Queue.take(reads);
578-
yield* Queue.take(reads).pipe(Effect.timeout("2 seconds"));
597+
yield* awaitWatcher(config.settingsPath, reads);
579598
yield* service.withSettingsSnapshot(() => Effect.void);
580599
yield* Queue.clear(replacing);
581600

@@ -623,11 +642,9 @@ it.layer(NodeServices.layer)("server settings", (it) => {
623642
providerInstances: { antigravity: { driver: "antigravity", config: { apiKey } } },
624643
});
625644
yield* fs.writeFileString(config.settingsPath, withApiKey("first-key"));
626-
// Startup moves the key into the secret store and rewrites the file, which the
627-
// watcher then reloads.
645+
// Startup moves the key into the secret store and rewrites the file.
628646
yield* service.start;
629-
yield* Queue.take(reads);
630-
yield* Queue.take(reads).pipe(Effect.timeout("2 seconds"));
647+
yield* awaitWatcher(config.settingsPath, reads);
631648
yield* service.withSettingsSnapshot(() => Effect.void);
632649
const changes = yield* service.subscribeChanges;
633650

0 commit comments

Comments
 (0)