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