Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

watchDirectoryReplacements.test.ts

105 lines3.8 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
3import { assert, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import * as Queue from "effect/Queue";
8import * as Stream from "effect/Stream";
9​
10import { watchDirectoryPath } from "./watchDirectoryReplacements.ts";
11​
12it.live.skipIf(!symlinksSupported)(
13 "backs off stopped watches for an unchanged linked directory",
14 () =>
15 Effect.gen(function* () {
16 const fs = yield* FileSystem.FileSystem;
17 const path = yield* Path.Path;
18 const base = yield* fs.makeTempDirectoryScoped();
19 const entry = path.join(base, "linked");
20 const first = path.join(base, "first", "themes");
21 const second = path.join(base, "second", "themes");
22 yield* fs.makeDirectory(first, { recursive: true });
23 yield* fs.makeDirectory(second, { recursive: true });
24 yield* fs.symlink(first, entry);
25 const firstIdentity = yield* fs.stat(first);
26 const secondIdentity = yield* fs.stat(second);
27 let identity = firstIdentity;
28 const events = yield* Queue.unbounded<"probe" | "refresh">();
29​
30 // An ended child emits a final refresh, as it does when registration fails.
31 yield* watchDirectoryPath(entry, Stream.succeed(undefined), Effect.succeed(false)).pipe(
32 Stream.runForEach(() => Queue.offer(events, "refresh")),
33 Effect.provideService(
34 FileSystem.FileSystem,
35 FileSystem.FileSystem.of({
36 ...fs,
37 stat: (file) =>
38 file === entry
39 ? Queue.offer(events, "probe").pipe(Effect.map(() => identity))
40 : fs.stat(file),
41 }),
42 ),
43 Effect.forkScoped,
44 );
45 assert.deepEqual(
46 yield* Queue.takeN(events, 10),
47 // Each acquire stats too: the first, a retry after 1 tick, then after 2 more.
48 [
49 "probe",
50 "probe",
51 "refresh",
52 "probe",
53 "probe",
54 "refresh",
55 "probe",
56 "probe",
57 "probe",
58 "refresh",
59 ],
60 );
61​
62 identity = secondIdentity;
63 assert.deepEqual(yield* Queue.takeN(events, 3), ["probe", "probe", "refresh"]);
64 }).pipe(Effect.provide(NodeServices.layer)),
65);
66​
67it.live.skipIf(!symlinksSupported)(
68 "rebuilds when a probe sees a tree other than the one the watches were acquired on",
69 () =>
70 Effect.gen(function* () {
71 const fs = yield* FileSystem.FileSystem;
72 const path = yield* Path.Path;
73 const base = yield* fs.makeTempDirectoryScoped();
74 const entry = path.join(base, "linked");
75 const first = path.join(base, "first");
76 const second = path.join(base, "second");
77 yield* fs.makeDirectory(first);
78 yield* fs.makeDirectory(second);
79 yield* fs.symlink(first, entry);
80 const a = yield* fs.stat(first);
81 const b = yield* fs.stat(second);
82 // Probe A, acquire A, probe B, swap back and acquire A, then B returns for the next probe.
83 const identities = [a, a, b, a, b, b];
84 const refreshes = yield* Queue.unbounded<void>();
85​
86 yield* watchDirectoryPath(
87 entry,
88 Stream.concat(Stream.succeed(undefined), Stream.never),
89 Effect.succeed(false),
90 ).pipe(
91 Stream.runForEach(() => Queue.offer(refreshes, undefined)),
92 Effect.provideService(
93 FileSystem.FileSystem,
94 FileSystem.FileSystem.of({
95 ...fs,
96 stat: (file) =>
97 file === entry ? Effect.sync(() => identities.shift() ?? b) : fs.stat(file),
98 }),
99 ),
100 Effect.forkScoped,
101 );
102 yield* Queue.takeN(refreshes, 3);
103 assert.deepEqual(identities, []);
104 }).pipe(Effect.provide(NodeServices.layer)),
105);