Skip to content

Commit 9e77068

Browse files
authored
Merge branch 'main' into t3code/upgrade-electron-43
2 parents 7bd8914 + 5e63aea commit 9e77068

36 files changed

Lines changed: 3513 additions & 370 deletions

‎apps/server/src/bin.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { projectCommand } from "./cli/project.ts";
1717
import { runServerCommand, serveCommand, startCommand } from "./cli/server.ts";
1818
import { serviceCommand } from "./cli/service.ts";
1919
import { servicePreflightCommand } from "./cli/servicePreflight.ts";
20+
import { themeCommand } from "./cli/theme.ts";
2021
import { triageCommand } from "./cli/triage.ts";
2122

2223
const CliRuntimeLayer = Layer.mergeAll(NodeServices.layer, NetService.layer);
@@ -57,6 +58,7 @@ export const makeCli = ({ cloudEnabled = hasCloudPublicConfig } = {}) =>
5758
projectCommand,
5859
serviceCommand,
5960
servicePreflightCommand,
61+
themeCommand,
6062
triageCommand,
6163
cloudEnabled ? connectCommand : connectUnavailableCommand,
6264
]),

‎apps/server/src/cli/theme.test.ts‎

Lines changed: 478 additions & 0 deletions
Large diffs are not rendered by default.

‎apps/server/src/cli/theme.ts‎

Lines changed: 587 additions & 0 deletions
Large diffs are not rendered by default.

‎apps/server/src/config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export interface ServerDerivedPaths {
3333
readonly dbPath: string;
3434
readonly keybindingsConfigPath: string;
3535
readonly settingsPath: string;
36+
/** Palettes this machine publishes for clients to follow, one file per theme. */
37+
readonly environmentThemesDir: string;
3638
readonly providerStatusCacheDir: string;
3739
readonly worktreesDir: string;
3840
readonly attachmentsDir: string;
@@ -119,6 +121,7 @@ export const deriveServerPaths = Effect.fn(function* (
119121
dbPath,
120122
keybindingsConfigPath: join(stateDir, "keybindings.json"),
121123
settingsPath: join(stateDir, "settings.json"),
124+
environmentThemesDir: join(stateDir, "themes"),
122125
providerStatusCacheDir,
123126
worktreesDir: join(baseDir, "worktrees"),
124127
attachmentsDir,

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export const make = Effect.gen(function* () {
155155
pullRequests: true,
156156
threadSettlement: true,
157157
threadSnooze: true,
158+
environmentThemes: true,
158159
threadPinning: true,
159160
threadPinReorder: true,
160161
threadTitleRegeneration: true,
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
import { EnvironmentThemeFile } from "@t3tools/contracts";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import { assert, describe, it } from "@effect/vitest";
4+
import * as Effect from "effect/Effect";
5+
import * as FileSystem from "effect/FileSystem";
6+
import * as Layer from "effect/Layer";
7+
import * as Path from "effect/Path";
8+
import * as Option from "effect/Option";
9+
import * as Queue from "effect/Queue";
10+
import * as Schema from "effect/Schema";
11+
import * as Scope from "effect/Scope";
12+
import * as Stream from "effect/Stream";
13+
14+
import * as ServerConfig from "./config.ts";
15+
import * as EnvironmentTheme from "./environmentTheme.ts";
16+
17+
const encodeThemeFile = Schema.encodeSync(Schema.fromJsonString(EnvironmentThemeFile));
18+
19+
const NIGHTFALL_THEME: EnvironmentThemeFile = {
20+
name: "Nightfall",
21+
appearance: "dark",
22+
canvas: "#1a1b26",
23+
accent: "#7aa2f7",
24+
};
25+
26+
/** The standard exported form: a full palette, no seeds. */
27+
const SHARED_THEME: EnvironmentThemeFile = {
28+
version: 1,
29+
name: "Shared Light",
30+
appearance: "light",
31+
colors: { canvas: "#eff1f5", accent: "#1e66f5" },
32+
};
33+
34+
/** Seeds theme files before the service starts, as a real machine would. */
35+
const withEnvironmentThemes = <A, E>(
36+
seeds: Readonly<Record<string, string>>,
37+
body: Effect.Effect<
38+
A,
39+
E,
40+
| EnvironmentTheme.EnvironmentThemeService
41+
| ServerConfig.ServerConfig
42+
| FileSystem.FileSystem
43+
| Path.Path
44+
| Scope.Scope
45+
>,
46+
) =>
47+
Effect.gen(function* () {
48+
const fs = yield* FileSystem.FileSystem;
49+
const path = yield* Path.Path;
50+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-environment-theme-" });
51+
const themesDir = path.join(baseDir, "userdata", "themes");
52+
yield* fs.makeDirectory(themesDir, { recursive: true });
53+
for (const [filename, contents] of Object.entries(seeds)) {
54+
yield* fs.writeFileString(path.join(themesDir, filename), contents);
55+
}
56+
57+
return yield* body.pipe(
58+
Effect.provide(
59+
EnvironmentTheme.layer.pipe(
60+
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), baseDir)),
61+
),
62+
),
63+
);
64+
}).pipe(Effect.scoped);
65+
66+
const currentThemes = Effect.gen(function* () {
67+
const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService;
68+
return yield* environmentTheme.current;
69+
});
70+
71+
it.layer(NodeServices.layer)("environment theme", (it) => {
72+
it.effect("publishes nothing when the machine has no theme files", () =>
73+
withEnvironmentThemes(
74+
{},
75+
Effect.gen(function* () {
76+
assert.deepEqual(yield* currentThemes, []);
77+
}),
78+
),
79+
);
80+
81+
it.effect("publishes each file under its filename as the id", () =>
82+
withEnvironmentThemes(
83+
{
84+
"nightfall.json": encodeThemeFile(NIGHTFALL_THEME),
85+
"shared-light.json": encodeThemeFile(SHARED_THEME),
86+
},
87+
Effect.gen(function* () {
88+
const themes = yield* currentThemes;
89+
assert.deepEqual(
90+
themes.map((theme) => theme.id),
91+
["nightfall", "shared-light"],
92+
);
93+
assert.deepEqual(themes[0], { id: "nightfall", ...NIGHTFALL_THEME });
94+
assert.deepEqual(themes[1], { id: "shared-light", ...SHARED_THEME });
95+
}),
96+
),
97+
);
98+
99+
// Read from disk rather than from the watcher's last observation, so a
100+
// client connecting after a missed filesystem event still sees the truth.
101+
it.effect("follows the directory rather than the set read at start", () =>
102+
withEnvironmentThemes(
103+
{ "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) },
104+
Effect.gen(function* () {
105+
const { environmentThemesDir } = yield* ServerConfig.ServerConfig;
106+
const fs = yield* FileSystem.FileSystem;
107+
const path = yield* Path.Path;
108+
109+
yield* fs.writeFileString(
110+
path.join(environmentThemesDir, "shared-light.json"),
111+
encodeThemeFile(SHARED_THEME),
112+
);
113+
assert.equal((yield* currentThemes).length, 2);
114+
115+
yield* fs.remove(path.join(environmentThemesDir, "nightfall.json"));
116+
assert.deepEqual(
117+
(yield* currentThemes).map((theme) => theme.id),
118+
["shared-light"],
119+
);
120+
}),
121+
),
122+
);
123+
124+
// One bad file must not take down the machine's other themes: a theme
125+
// script that leaves a template placeholder unresolved, a half-written
126+
// file, or a stray name are each that file's problem alone.
127+
// The subscription is acquired before the current set is read, so nothing
128+
// published while a client connects can fall between snapshot and stream.
129+
it.effect("streams the current set first", () =>
130+
withEnvironmentThemes(
131+
{ "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) },
132+
Effect.gen(function* () {
133+
const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService;
134+
const first = yield* environmentTheme.streamChanges.pipe(Stream.runHead);
135+
assert.deepEqual(Option.getOrNull(first), [{ id: "nightfall", ...NIGHTFALL_THEME }]);
136+
}),
137+
),
138+
);
139+
140+
// Subscribing happens before the snapshot read, so a publish landing in
141+
// between is queued. It must not replay after the newer snapshot and walk
142+
// clients back onto colors the machine has already moved past.
143+
it.effect("never replays a set older than the snapshot it started from", () =>
144+
withEnvironmentThemes(
145+
{ "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) },
146+
Effect.gen(function* () {
147+
const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService;
148+
const { environmentThemesDir } = yield* ServerConfig.ServerConfig;
149+
const fs = yield* FileSystem.FileSystem;
150+
const path = yield* Path.Path;
151+
152+
// Advance the directory twice without the watcher running, so the
153+
// second read is strictly newer than anything already observed.
154+
yield* fs.writeFileString(
155+
path.join(environmentThemesDir, "shared-light.json"),
156+
encodeThemeFile(SHARED_THEME),
157+
);
158+
const first = yield* environmentTheme.streamChanges.pipe(Stream.runHead);
159+
assert.deepEqual(
160+
Option.getOrNull(first)?.map((theme) => theme.id),
161+
["nightfall", "shared-light"],
162+
);
163+
}),
164+
),
165+
);
166+
167+
it.effect("skips invalid files while keeping valid ones", () =>
168+
withEnvironmentThemes(
169+
{
170+
"nightfall.json": encodeThemeFile(NIGHTFALL_THEME),
171+
"unresolved.json":
172+
'{ "name": "X", "appearance": "dark", "canvas": "{{ background }}", "accent": "#7aa2f7" }',
173+
"malformed.json": "{ not json",
174+
"no-colors.json": '{ "name": "Empty", "appearance": "dark" }',
175+
"Bad Name.json": encodeThemeFile(SHARED_THEME),
176+
"ocean.json": encodeThemeFile(SHARED_THEME),
177+
"dark.json": encodeThemeFile(SHARED_THEME),
178+
"notes.txt": "not a theme",
179+
},
180+
Effect.gen(function* () {
181+
assert.deepEqual(
182+
(yield* currentThemes).map((theme) => theme.id),
183+
["nightfall"],
184+
);
185+
}),
186+
),
187+
);
188+
189+
// A symlinked themes directory stays usable, but a symlinked file inside it
190+
// must not publish whatever it points at.
191+
it.effect("ignores a symlinked theme file", () =>
192+
withEnvironmentThemes(
193+
{},
194+
Effect.gen(function* () {
195+
const { environmentThemesDir } = yield* ServerConfig.ServerConfig;
196+
const fs = yield* FileSystem.FileSystem;
197+
const path = yield* Path.Path;
198+
const outside = path.join(environmentThemesDir, "..", "outside.json");
199+
yield* fs.writeFileString(outside, encodeThemeFile(NIGHTFALL_THEME));
200+
yield* fs.symlink(outside, path.join(environmentThemesDir, "nightfall.json"));
201+
assert.deepEqual(yield* currentThemes, []);
202+
}),
203+
),
204+
);
205+
206+
// The aggregate size cap charges only accepted themes, so a pile of
207+
// malformed files cannot spend the budget and hide a valid theme sorted
208+
// after them.
209+
it.effect("does not charge skipped files against the total size limit", () =>
210+
withEnvironmentThemes(
211+
{
212+
...Object.fromEntries(
213+
Array.from({ length: 7 }, (_, index) => [`junk-${index}.json`, "{".repeat(30_000)]),
214+
),
215+
"zz-valid.json": encodeThemeFile(NIGHTFALL_THEME),
216+
},
217+
Effect.gen(function* () {
218+
assert.deepEqual(
219+
(yield* currentThemes).map((theme) => theme.id),
220+
["zz-valid"],
221+
);
222+
}),
223+
),
224+
);
225+
});
226+
227+
// The feature's headline claim: rewrite a file and connected clients retint
228+
// without a restart. Live clock and a real filesystem event, so this proves
229+
// the watcher rather than a direct read. Kept outside the it.layer block above
230+
// because only the top-level `it` exposes `live`.
231+
describe("environment theme watching", () => {
232+
it.live("streams a set for every change to the directory", () =>
233+
Effect.gen(function* () {
234+
const fs = yield* FileSystem.FileSystem;
235+
const path = yield* Path.Path;
236+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-theme-watch-" });
237+
const themesDir = path.join(baseDir, "userdata", "themes");
238+
yield* fs.makeDirectory(themesDir, { recursive: true });
239+
240+
yield* Effect.gen(function* () {
241+
const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService;
242+
const seen = yield* Queue.unbounded<ReadonlyArray<{ readonly id: string }>>();
243+
yield* Stream.runForEach(environmentTheme.streamChanges, (themes) =>
244+
Queue.offer(seen, themes),
245+
).pipe(Effect.forkScoped);
246+
247+
// Empty to start.
248+
assert.deepEqual(yield* Queue.take(seen), []);
249+
250+
// Published atomically, the way a theme hook writes it.
251+
const staging = path.join(baseDir, "staged.json");
252+
yield* fs.writeFileString(staging, encodeThemeFile(NIGHTFALL_THEME));
253+
yield* fs.rename(staging, path.join(themesDir, "nightfall.json"));
254+
assert.deepEqual(
255+
(yield* Queue.take(seen)).map((theme) => theme.id),
256+
["nightfall"],
257+
);
258+
259+
// Removed again, and the set empties without a restart.
260+
yield* fs.remove(path.join(themesDir, "nightfall.json"));
261+
assert.deepEqual(yield* Queue.take(seen), []);
262+
}).pipe(
263+
Effect.provide(
264+
EnvironmentTheme.layer.pipe(
265+
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), baseDir)),
266+
),
267+
),
268+
Effect.timeout("30 seconds"),
269+
);
270+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
271+
);
272+
});

0 commit comments

Comments
 (0)