Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { defaultThemeGeneration, defaultThemeToApply } from "./useDefaultTheme";
4
5const BASE = {
6 environmentId: "env-1",
7 defaultTheme: "nightfall",
8 defaultThemeSetAt: "2026-08-28T00:00:00.000Z",
9 appliedGeneration: null,
10 resolves: true,
11} as const;
12
13describe("default theme adoption", () => {
14 it("applies a set this client has not seen", () => {
15 expect(defaultThemeToApply(BASE)).toBe(
16 defaultThemeGeneration(BASE.defaultTheme, BASE.defaultThemeSetAt),
17 );
18 });
19
20 it("does not replay a generation it already applied", () => {
21 const generation = defaultThemeGeneration(BASE.defaultTheme, BASE.defaultThemeSetAt);
22 expect(defaultThemeToApply({ ...BASE, appliedGeneration: generation })).toBe(null);
23 });
24
25 // `t3 theme set` of a theme this client already wears must still act, which
26 // is why the generation carries the set time and not just the value.
27 it("applies the same theme again when the environment re-sets it", () => {
28 const applied = defaultThemeGeneration(BASE.defaultTheme, "2026-08-28T00:00:00.000Z");
29 const next = defaultThemeToApply({
30 ...BASE,
31 defaultThemeSetAt: "2026-08-29T00:00:00.000Z",
32 appliedGeneration: applied,
33 });
34 expect(next).not.toBe(null);
35 expect(next).not.toBe(applied);
36 });
37
38 // Environments provisioned before the timestamp existed.
39 it("falls back to once-per-value without a set time", () => {
40 const generation = defaultThemeGeneration("nightfall", "");
41 expect(defaultThemeToApply({ ...BASE, defaultThemeSetAt: "", appliedGeneration: null })).toBe(
42 generation,
43 );
44 expect(
45 defaultThemeToApply({ ...BASE, defaultThemeSetAt: "", appliedGeneration: generation }),
46 ).toBe(null);
47 });
48
49 // The setting and the palette it names arrive independently.
50 it("waits for a theme that has not arrived yet", () => {
51 expect(defaultThemeToApply({ ...BASE, resolves: false })).toBe(null);
52 });
53
54 it("leaves a client alone with no environment or no theme set", () => {
55 expect(defaultThemeToApply({ ...BASE, environmentId: null })).toBe(null);
56 expect(defaultThemeToApply({ ...BASE, defaultTheme: "" })).toBe(null);
57 });
58});