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

branding.test.ts

189 lines5.4 KB

1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2import {
3 resolveServerBackedAppDisplayName,
4 resolveServerBackedAppStageLabel,
5 resolveSidebarV2Default,
6 resolveSidebarV2Enabled,
7} from "./branding.logic";
8​
9const originalWindow = globalThis.window;
10​
11afterEach(() => {
12 vi.resetModules();
13​
14 if (originalWindow === undefined) {
15 Reflect.deleteProperty(globalThis, "window");
16 return;
17 }
18​
19 globalThis.window = originalWindow;
20});
21​
22describe("branding", () => {
23 it("uses injected desktop branding when available", async () => {
24 Object.defineProperty(globalThis, "window", {
25 configurable: true,
26 value: {
27 desktopBridge: {
28 getAppBranding: () => ({
29 baseName: "T3 Code",
30 stageLabel: "Nightly",
31 displayName: "T3 Code (Nightly)",
32 }),
33 },
34 },
35 });
36​
37 const branding = await import("./branding");
38​
39 expect(branding.APP_BASE_NAME).toBe("T3 Code");
40 expect(branding.APP_STAGE_LABEL).toBe("Nightly");
41 expect(branding.APP_DISPLAY_NAME).toBe("T3 Code (Nightly)");
42 });
43​
44 it("normalizes hosted app channel metadata", async () => {
45 vi.stubEnv("VITE_HOSTED_APP_CHANNEL", "nightly");
46​
47 const branding = await import("./branding");
48​
49 expect(branding.HOSTED_APP_CHANNEL).toBe("nightly");
50 expect(branding.HOSTED_APP_CHANNEL_LABEL).toBe("Nightly");
51 expect(branding.APP_STAGE_LABEL).toBe("Nightly");
52 expect(branding.APP_DISPLAY_NAME).toBe("T3 Code (Nightly)");
53 });
54​
55 it("does not label the latest hosted app channel", async () => {
56 vi.stubEnv("VITE_HOSTED_APP_CHANNEL", "latest");
57​
58 const branding = await import("./branding");
59​
60 expect(branding.HOSTED_APP_CHANNEL).toBe("latest");
61 expect(branding.HOSTED_APP_CHANNEL_LABEL).toBe("Latest");
62 expect(branding.APP_STAGE_LABEL).toBe("Latest");
63 expect(branding.APP_DISPLAY_NAME).toBe("T3 Code");
64 });
65​
66 it("ignores unknown hosted app channels", async () => {
67 vi.stubEnv("VITE_HOSTED_APP_CHANNEL", "preview");
68​
69 const branding = await import("./branding");
70​
71 expect(branding.HOSTED_APP_CHANNEL).toBeNull();
72 expect(branding.HOSTED_APP_CHANNEL_LABEL).toBeNull();
73 });
74});
75​
76describe("branding logic", () => {
77 it("returns Nightly for nightly primary server versions", () => {
78 expect(
79 resolveServerBackedAppStageLabel({
80 primaryServerVersion: "0.0.28-nightly.20260616.12",
81 fallbackStageLabel: "Alpha",
82 }),
83 ).toBe("Nightly");
84 });
85​
86 it("updates the display name for nightly primary server versions", () => {
87 expect(
88 resolveServerBackedAppDisplayName({
89 baseName: "T3 Code",
90 fallbackDisplayName: "T3 Code (Alpha)",
91 fallbackStageLabel: "Alpha",
92 primaryServerVersion: "0.0.28-nightly.20260616.12",
93 }),
94 ).toBe("T3 Code (Nightly)");
95 });
96​
97 it("keeps the fallback display name for stable primary server versions", () => {
98 expect(
99 resolveServerBackedAppDisplayName({
100 baseName: "T3 Code",
101 fallbackDisplayName: "T3 Code (Alpha)",
102 fallbackStageLabel: "Alpha",
103 primaryServerVersion: "0.0.27",
104 }),
105 ).toBe("T3 Code (Alpha)");
106 });
107​
108 it("keeps the fallback display name for malformed nightly primary server versions", () => {
109 expect(
110 resolveServerBackedAppDisplayName({
111 baseName: "T3 Code",
112 fallbackDisplayName: "T3 Code (Alpha)",
113 fallbackStageLabel: "Alpha",
114 primaryServerVersion: "0.0.28-nightly.20260616",
115 }),
116 ).toBe("T3 Code (Alpha)");
117 });
118});
119​
120describe("resolveSidebarV2Default", () => {
121 it.each(["Nightly", "Dev", "nightly", " dev "])("enables the beta for %s builds", (stage) => {
122 expect(resolveSidebarV2Default(stage)).toBe(true);
123 });
124​
125 it.each(["Alpha", "Latest", ""])("leaves the beta off for %s builds", (stage) => {
126 expect(resolveSidebarV2Default(stage)).toBe(false);
127 });
128});
129​
130describe("resolveSidebarV2Enabled", () => {
131 const hydrated = { settingsHydrated: true } as const;
132​
133 it.each(["Alpha", "Latest"])(
134 "keeps a legacy opt-in on %s builds even without the companion flag",
135 (stageLabel) => {
136 // `true` was never the schema default, so it can only be an explicit
137 // opt-in from settings written before `sidebarV2ConfiguredByUser` existed.
138 expect(
139 resolveSidebarV2Enabled({
140 ...hydrated,
141 enabled: true,
142 configuredByUser: false,
143 stageLabel,
144 }),
145 ).toBe(true);
146 },
147 );
148​
149 it("applies the stage default when the beta was never enabled or configured", () => {
150 expect(
151 resolveSidebarV2Enabled({
152 ...hydrated,
153 enabled: false,
154 configuredByUser: false,
155 stageLabel: "Nightly",
156 }),
157 ).toBe(true);
158 expect(
159 resolveSidebarV2Enabled({
160 ...hydrated,
161 enabled: false,
162 configuredByUser: false,
163 stageLabel: "Latest",
164 }),
165 ).toBe(false);
166 });
167​
168 it("honors an explicit opt-out over the stage default", () => {
169 expect(
170 resolveSidebarV2Enabled({
171 ...hydrated,
172 enabled: false,
173 configuredByUser: true,
174 stageLabel: "Nightly",
175 }),
176 ).toBe(false);
177 });
178​
179 it("holds v1 until settings hydrate so the sidebar does not remount", () => {
180 expect(
181 resolveSidebarV2Enabled({
182 enabled: true,
183 configuredByUser: true,
184 settingsHydrated: false,
185 stageLabel: "Nightly",
186 }),
187 ).toBe(false);
188 });
189});