Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3
4import type { toastManager } from "./ui/toast";
5
6const { add, starred } = vi.hoisted(() => ({
7 add: vi.fn<typeof toastManager.add>(),
8 starred: { value: false, failed: false, duringCheck: () => {} },
9}));
10vi.mock("./ui/toast", () => ({ toastManager: { add, close: vi.fn() } }));
11vi.mock("../rpc/atomRegistry", () => ({ appAtomRegistry: {} }));
12vi.mock("../state/sourceControl", () => ({ sourceControlEnvironment: { checkStarred: {} } }));
13vi.mock("@t3tools/client-runtime/state/runtime", () => ({
14 runAtomCommand: async () => {
15 starred.duringCheck();
16 return starred.failed ? { _tag: "Failure" } : { _tag: "Success", value: starred.value };
17 },
18}));
19
20const environmentId = EnvironmentId.make("environment-1");
21
22function setup({ sent = 0, writable = true, alreadyStarred = false } = {}) {
23 add.mockReset();
24 starred.value = alreadyStarred;
25 starred.failed = false;
26 starred.duringCheck = () => {};
27 const store = new Map([["t3code:github-star-prompt:sent", String(sent)]]);
28 vi.stubGlobal("window", {
29 localStorage: {
30 getItem: (key: string) => store.get(key) ?? null,
31 setItem: (key: string, value: string) => {
32 if (!writable) throw new Error("QuotaExceededError");
33 store.set(key, value);
34 },
35 },
36 });
37 return store;
38}
39
40// A fresh module import stands in for an app reload; storage survives it.
41async function startSession() {
42 vi.resetModules();
43 const session = await import("./GitHubStarPrompt");
44 return { send: () => session.recordPromptSent(environmentId) };
45}
46
47describe("recordPromptSent", () => {
48 it("asks after five prompts, once per session, and never again once closed", async () => {
49 setup();
50 let session = await startSession();
51 for (let sent = 0; sent < 5; sent++) await session.send();
52 expect(add).not.toHaveBeenCalled();
53
54 await session.send();
55 await session.send();
56 expect(add).toHaveBeenCalledOnce();
57
58 session = await startSession();
59 await session.send();
60 expect(add).toHaveBeenCalledTimes(2);
61
62 add.mock.lastCall?.[0].onClose?.();
63 session = await startSession();
64 for (let sent = 0; sent < 10; sent++) await session.send();
65 expect(add).toHaveBeenCalledTimes(2);
66 vi.unstubAllGlobals();
67 });
68
69 it("never asks when a dismissal could not be saved", async () => {
70 setup({ sent: 5, writable: false });
71 await (await startSession()).send();
72 expect(add).not.toHaveBeenCalled();
73 vi.unstubAllGlobals();
74 });
75
76 it("never asks once the environment's GitHub account starred the repo", async () => {
77 setup({ sent: 5, alreadyStarred: true });
78 await (await startSession()).send();
79 starred.value = false;
80 await (await startSession()).send();
81 expect(add).not.toHaveBeenCalled();
82 vi.unstubAllGlobals();
83 });
84
85 it("skips an unavailable server check without permanently dismissing", async () => {
86 const store = setup({ sent: 5 });
87 starred.failed = true;
88 await (await startSession()).send();
89 expect(add).not.toHaveBeenCalled();
90 expect(store.has("t3code:github-star-prompt:dismissed")).toBe(false);
91 starred.failed = false;
92 await (await startSession()).send();
93 expect(add).toHaveBeenCalledOnce();
94 vi.unstubAllGlobals();
95 });
96
97 it("never asks when another tab dismisses it during the star check", async () => {
98 const store = setup({ sent: 5 });
99 starred.duringCheck = () => store.set("t3code:github-star-prompt:dismissed", "true");
100 await (await startSession()).send();
101 expect(add).not.toHaveBeenCalled();
102 vi.unstubAllGlobals();
103 });
104});