Skip to content

Commit 14b5cf3

Browse files
test(server): share the save gate across profile persistence tests
Drop the separate interrupted-cache test; the interrupted-report test covers the same uninterruptible save. AI-assisted; verified with the tests and tools listed above.
1 parent 7719afa commit 14b5cf3

2 files changed

Lines changed: 36 additions & 90 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 31 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,25 +2677,37 @@ it.live(
26772677
).pipe(Effect.provide(layer)),
26782678
);
26792679

2680-
it.live("an agent opens a reported profile after a restart, before any client reports again", () =>
2681-
Effect.gen(function* () {
2682-
const fs = yield* FileSystem.FileSystem;
2683-
const cleanupStarted = yield* Deferred.make<void>();
2684-
const releaseCleanup = yield* Deferred.make<void>();
2685-
let pauseCleanup = false;
2686-
const gatedFs = FileSystem.FileSystem.of({
2680+
// Holds the next profile save at its temp-file cleanup, after the rename, until released.
2681+
const gateSaveCleanup = Effect.gen(function* () {
2682+
const fs = yield* FileSystem.FileSystem;
2683+
const cleanupStarted = yield* Deferred.make<void>();
2684+
const releaseCleanup = yield* Deferred.make<void>();
2685+
let paused = false;
2686+
return {
2687+
cleanupStarted,
2688+
releaseCleanup,
2689+
pauseNext: () => {
2690+
paused = true;
2691+
},
2692+
fs: FileSystem.FileSystem.of({
26872693
...fs,
26882694
remove: (target, options) =>
26892695
fs.remove(target, options).pipe(
26902696
Effect.tap(() => {
2691-
if (!pauseCleanup) return Effect.void;
2692-
pauseCleanup = false;
2697+
if (!paused) return Effect.void;
2698+
paused = false;
26932699
return Deferred.succeed(cleanupStarted, undefined).pipe(
26942700
Effect.andThen(Deferred.await(releaseCleanup)),
26952701
);
26962702
}),
26972703
),
2698-
});
2704+
}),
2705+
};
2706+
});
2707+
2708+
it.live("an agent opens a reported profile after a restart, before any client reports again", () =>
2709+
Effect.gen(function* () {
2710+
const gate = yield* gateSaveCleanup;
26992711
const persistent = (name: string) =>
27002712
({ id: `profile-${name.toLowerCase()}`, name, kind: "persistent" }) as const;
27012713
// Together the two clients report more profiles than one report may hold.
@@ -2716,14 +2728,14 @@ it.live("an agent opens a reported profile after a restart, before any client re
27162728
const reportFromB = yield* browser.profileReporter;
27172729
const reportFromC = yield* browser.profileReporter;
27182730
yield* reportFromA({ profiles: fromA, defaultProfileId: "profile-work" });
2719-
pauseCleanup = true;
2731+
gate.pauseNext();
27202732
const savingB = yield* reportFromB({
27212733
profiles: fromB,
27222734
defaultProfileId: "profile-shared",
27232735
}).pipe(Effect.forkScoped({ startImmediately: true }));
27242736
// A save that ends without reaching the gate fails the test instead of hanging it.
27252737
yield* Effect.raceFirst(
2726-
Deferred.await(cleanupStarted),
2738+
Deferred.await(gate.cleanupStarted),
27272739
Fiber.join(savingB).pipe(
27282740
Effect.andThen(Effect.die("save finished before the cleanup gate")),
27292741
),
@@ -2733,14 +2745,14 @@ it.live("an agent opens a reported profile after a restart, before any client re
27332745
Effect.forkScoped({ startImmediately: true }),
27342746
);
27352747
yield* Scope.close(clientA, Exit.void);
2736-
yield* Deferred.succeed(releaseCleanup, undefined);
2748+
yield* Deferred.succeed(gate.releaseCleanup, undefined);
27372749
yield* Fiber.join(savingB);
27382750
yield* Fiber.join(savingC);
27392751
}).pipe(
27402752
Effect.scoped,
27412753
Effect.provide(
27422754
Layer.fresh(ServerBrowser.layer).pipe(
2743-
Layer.provide(Layer.succeed(FileSystem.FileSystem, gatedFs)),
2755+
Layer.provide(Layer.succeed(FileSystem.FileSystem, gate.fs)),
27442756
),
27452757
),
27462758
);
@@ -2807,86 +2819,19 @@ it.live("warns at startup about a saved profile catalog it cannot read or decode
28072819
}).pipe(Effect.provide(dependencies)),
28082820
);
28092821

2810-
it.live("an interrupted profile save cannot leave the unchanged-report cache stale", () =>
2811-
Effect.gen(function* () {
2812-
const fs = yield* FileSystem.FileSystem;
2813-
const path = yield* Path.Path;
2814-
const { stateDir } = yield* ServerConfig.ServerConfig;
2815-
const cleanupStarted = yield* Deferred.make<void>();
2816-
const releaseCleanup = yield* Deferred.make<void>();
2817-
let pauseCleanup = false;
2818-
const gatedFs = FileSystem.FileSystem.of({
2819-
...fs,
2820-
remove: (target, options) =>
2821-
fs.remove(target, options).pipe(
2822-
Effect.tap(() => {
2823-
if (!pauseCleanup) return Effect.void;
2824-
pauseCleanup = false;
2825-
return Deferred.succeed(cleanupStarted, undefined).pipe(
2826-
Effect.andThen(Deferred.await(releaseCleanup)),
2827-
);
2828-
}),
2829-
),
2830-
});
2831-
yield* Effect.gen(function* () {
2832-
const report = yield* (yield* ServerBrowser.ServerBrowser).profileReporter;
2833-
const original = {
2834-
profiles: [{ id: "profile-work", name: "Work", kind: "persistent" as const }],
2835-
defaultProfileId: "profile-work",
2836-
};
2837-
yield* report(original);
2838-
pauseCleanup = true;
2839-
const saving = yield* report({ ...original, defaultProfileId: "default" }).pipe(
2840-
Effect.forkScoped,
2841-
);
2842-
yield* Deferred.await(cleanupStarted);
2843-
saving.interruptUnsafe();
2844-
yield* Deferred.succeed(releaseCleanup, undefined);
2845-
yield* Fiber.await(saving);
2846-
yield* report(original);
2847-
const saved = yield* fs.readFileString(
2848-
path.join(stateDir, "server-browser", "reported-profiles.json"),
2849-
);
2850-
expect(JSON.parse(saved)).toMatchObject(original);
2851-
}).pipe(
2852-
Effect.provide(
2853-
Layer.fresh(ServerBrowser.layer).pipe(
2854-
Layer.provide(Layer.succeed(FileSystem.FileSystem, gatedFs)),
2855-
),
2856-
),
2857-
);
2858-
}).pipe(Effect.provide(dependencies)),
2859-
);
2860-
28612822
it.live("a report interrupted while an earlier save holds the lock is still saved", () =>
28622823
Effect.gen(function* () {
2863-
const fs = yield* FileSystem.FileSystem;
2864-
const cleanupStarted = yield* Deferred.make<void>();
2865-
const releaseCleanup = yield* Deferred.make<void>();
2866-
let pauseCleanup = false;
2867-
const gatedFs = FileSystem.FileSystem.of({
2868-
...fs,
2869-
remove: (target, options) =>
2870-
fs.remove(target, options).pipe(
2871-
Effect.tap(() => {
2872-
if (!pauseCleanup) return Effect.void;
2873-
pauseCleanup = false;
2874-
return Deferred.succeed(cleanupStarted, undefined).pipe(
2875-
Effect.andThen(Deferred.await(releaseCleanup)),
2876-
);
2877-
}),
2878-
),
2879-
});
2824+
const gate = yield* gateSaveCleanup;
28802825
const work = { id: "profile-work", name: "Work", kind: "persistent" } as const;
28812826
const late = { id: "profile-late", name: "Late", kind: "persistent" } as const;
28822827
yield* Effect.gen(function* () {
28832828
const report = yield* (yield* ServerBrowser.ServerBrowser).profileReporter;
2884-
pauseCleanup = true;
2829+
gate.pauseNext();
28852830
const saving = yield* report({ profiles: [work], defaultProfileId: "profile-work" }).pipe(
28862831
Effect.forkScoped({ startImmediately: true }),
28872832
);
28882833
yield* Effect.raceFirst(
2889-
Deferred.await(cleanupStarted),
2834+
Deferred.await(gate.cleanupStarted),
28902835
Fiber.join(saving).pipe(
28912836
Effect.andThen(Effect.die("save finished before the cleanup gate")),
28922837
),
@@ -2897,14 +2842,14 @@ it.live("a report interrupted while an earlier save holds the lock is still save
28972842
defaultProfileId: "profile-late",
28982843
}).pipe(Effect.forkScoped({ startImmediately: true }));
28992844
waiting.interruptUnsafe();
2900-
yield* Deferred.succeed(releaseCleanup, undefined);
2845+
yield* Deferred.succeed(gate.releaseCleanup, undefined);
29012846
yield* Fiber.join(saving);
29022847
yield* Fiber.await(waiting);
29032848
}).pipe(
29042849
Effect.scoped,
29052850
Effect.provide(
29062851
Layer.fresh(ServerBrowser.layer).pipe(
2907-
Layer.provide(Layer.succeed(FileSystem.FileSystem, gatedFs)),
2852+
Layer.provide(Layer.succeed(FileSystem.FileSystem, gate.fs)),
29082853
),
29092854
),
29102855
);

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,10 +1693,11 @@ const make = Effect.gen(function* () {
16931693
if (contents === savedProfiles) return;
16941694
yield* writeFileStringAtomically({ filePath: savedProfilesPath, contents }).pipe(
16951695
Effect.provideContext(fileServices),
1696-
Effect.tap(() => {
1697-
savedProfiles = contents;
1698-
return Effect.void;
1699-
}),
1696+
Effect.tap(() =>
1697+
Effect.sync(() => {
1698+
savedProfiles = contents;
1699+
}),
1700+
),
17001701
Effect.catch((cause) =>
17011702
Effect.logWarning("failed to save reported browser profiles", { cause }),
17021703
),

0 commit comments

Comments
 (0)