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

PermissionUpdateNotice.browser.test.tsx

113 lines3.8 KB

1import { act } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
4​
5type Session = {
6 _tag: "Success" | "Failure";
7 waiting: boolean;
8 value: { authenticated: boolean; permissions: string[] };
9};
10const state = vi.hoisted(() => ({
11 id: 0,
12 session: null as unknown as Session,
13 add: vi.fn((_notice: unknown) => "notice"),
14 close: vi.fn(),
15 navigate: vi.fn(),
16}));
17vi.mock(import("@effect/atom-react"), async (original) => ({
18 ...(await original()),
19 useAtomValue: (() => state.session) as never,
20}));
21vi.mock(import("@tanstack/react-router"), async (original) => ({
22 ...(await original()),
23 useNavigate: (() => state.navigate) as never,
24}));
25vi.mock(import("../state/environments"), async (original) => ({
26 ...(await original()),
27 useEnvironments: (() => ({
28 environments: [{ environmentId: `env-${state.id}`, label: "Work laptop" }],
29 })) as never,
30}));
31vi.mock(import("./ui/toast"), async (original) => {
32 const actual = await original();
33 return {
34 ...actual,
35 toastManager: { ...actual.toastManager, add: state.add, close: state.close } as never,
36 };
37});
38import { PermissionUpdateNotice } from "./PermissionUpdateNotice";
39​
40let root: Root;
41let container: HTMLDivElement;
42const storageKey = () => `t3code:permission-update:v1:env-${state.id}`;
43​
44beforeEach(() => {
45 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
46 state.id++;
47 state.session = {
48 _tag: "Success",
49 waiting: false,
50 value: { authenticated: true, permissions: ["orchestration:operate"] },
51 };
52 vi.clearAllMocks();
53 container = document.createElement("div");
54 root = createRoot(container);
55});
56afterEach(async () => {
57 await act(async () => root.unmount());
58 localStorage.clear();
59 vi.unstubAllGlobals();
60});
61const render = () => act(async () => root.render(<PermissionUpdateNotice />));
62type Notice = {
63 timeout: number;
64 actionProps: { onClick: () => void };
65 data: { secondaryActionProps: { onClick: () => void } };
66};
67const notice = () => state.add.mock.calls[0]![0] as Notice;
68​
69it("keeps the notice visible and persists dismissal, with a route to pairing settings", async () => {
70 await render();
71 expect(notice().timeout).toBe(0);
72 expect(localStorage.getItem(storageKey())).toBeNull();
73 notice().actionProps.onClick();
74 expect(state.navigate).toHaveBeenCalledWith({ to: "/settings/connections" });
75 expect(state.close).toHaveBeenCalledWith("notice");
76 expect(localStorage.getItem(storageKey())).toBe("true");
77});
78it("does not repeat on session refresh or remount", async () => {
79 await render();
80 await act(async () => root.unmount());
81 root = createRoot(container);
82 state.session = { ...state.session };
83 await render();
84 expect(state.add).toHaveBeenCalledTimes(1);
85});
86it("skips a notice dismissed on an earlier launch", async () => {
87 localStorage.setItem(storageKey(), "true");
88 await render();
89 expect(state.add).not.toHaveBeenCalled();
90});
91it("waits for a successful, settled session check", async () => {
92 state.session._tag = "Failure";
93 await render();
94 expect(state.add).not.toHaveBeenCalled();
95 state.session = { ...state.session, _tag: "Success", waiting: true };
96 await render();
97 expect(state.add).not.toHaveBeenCalled();
98 state.session = { ...state.session, waiting: false };
99 await render();
100 expect(state.add).toHaveBeenCalledTimes(1);
101});
102it("does not warn for a granular grant", async () => {
103 state.session.value.permissions.push("filesystem:read");
104 await render();
105 expect(state.add).not.toHaveBeenCalled();
106});
107​
108it("persists the secondary dismissal without relying on the toast close callback", async () => {
109 await render();
110 notice().data.secondaryActionProps.onClick();
111 expect(localStorage.getItem(storageKey())).toBe("true");
112 expect(state.close).toHaveBeenCalledWith("notice");
113});