Open History to see the last commit
1import {
2 AuthFilesystemReadScope,
3 EnvironmentAuthorizationError,
4 EnvironmentId,
5 ThreadId,
6 type AuthSessionState,
7} from "@t3tools/contracts";
8import * as Cause from "effect/Cause";
9import { AsyncResult } from "effect/reactivity";
10import { beforeEach, expect, it, vi } from "vite-plus/test";
11
12const state = vi.hoisted(() => ({
13 session: null as Pick<AuthSessionState, "authenticated" | "scopes"> | null,
14 phase: "connected" as "connected" | "offline",
15 assetAtom: {},
16 mint: vi.fn(),
17 assetQuery: vi.fn(),
18}));
19
20vi.mock("react", () => ({ useCallback: <A>(callback: A) => callback }));
21vi.mock("@effect/atom-react", () => ({
22 useAtomMount: () => undefined,
23 useAtomSubscribe: () => undefined,
24 useAtomValue: (atom: unknown) =>
25 atom === state.assetAtom
26 ? AsyncResult.success({ relativeUrl: "/api/assets/image.png", expiresAt: 1 })
27 : AsyncResult.initial(false),
28}));
29vi.mock("./session", () => ({
30 environmentSession: { sessionStateAtom: () => ({}) },
31 usePreparedConnection: () => ({ _tag: "Some", value: { httpBaseUrl: "https://host.test" } }),
32}));
33vi.mock("./presentation", () => ({
34 useEnvironmentPresentation: () => ({
35 isReady: true,
36 presentation: { connection: { phase: state.phase, error: null } },
37 }),
38}));
39vi.mock("./query", () => ({
40 useEnvironmentQuery: () => ({ data: state.session, error: null }),
41}));
42vi.mock("./projectClones", () => ({ environmentProjectCloneListAtom: () => null }));
43vi.mock("../connection/runtime", () => ({ connectionAtomRuntime: {} }));
44vi.mock("../connection/catalog", () => ({
45 environmentCatalog: { sessionGenerationAtom: () => null },
46}));
47vi.mock("@t3tools/client-runtime/state/assets", async (importOriginal) => ({
48 ...(await importOriginal<typeof import("@t3tools/client-runtime/state/assets")>()),
49 createAssetEnvironmentAtoms: () => ({ createUrl: state.assetQuery }),
50}));
51vi.mock("./use-atom-query-runner", () => ({ useAtomQueryRunner: () => state.mint }));
52
53import { useRefreshAssetUrl, useAssetUrlState } from "./assets";
54
55const environmentId = EnvironmentId.make("asset-environment");
56const threadId = ThreadId.make("asset-thread");
57const resource = { _tag: "media-file", threadId, path: "/repo/image.png" } as const;
58
59beforeEach(() => {
60 state.session = null;
61 state.phase = "connected";
62 state.assetQuery.mockReset().mockReturnValue(state.assetAtom);
63 state.mint
64 .mockReset()
65 .mockResolvedValue(AsyncResult.success({ relativeUrl: "/api/assets/image.png", expiresAt: 1 }));
66});
67
68it.each(["workspace-file", "media-file"] as const)(
69 "keeps %s loading until its file grant resolves",
70 (_tag) => {
71 expect(useAssetUrlState(environmentId, { ...resource, _tag })).toEqual({ _tag: "Loading" });
72 expect(state.assetQuery).not.toHaveBeenCalled();
73
74 state.session = { authenticated: true, scopes: [AuthFilesystemReadScope] };
75 expect(useAssetUrlState(environmentId, { ...resource, _tag })).toEqual({
76 _tag: "Success",
77 expiresAt: 1,
78 url: "https://host.test/api/assets/image.png",
79 });
80 },
81);
82
83it("hides host assets with a denied grant while preserving attachments", () => {
84 state.session = { authenticated: true, scopes: [] };
85 expect(useAssetUrlState(environmentId, resource)).toEqual({
86 _tag: "Failure",
87 reason: "missing-file-access",
88 });
89 expect(state.assetQuery).not.toHaveBeenCalled();
90 expect(useAssetUrlState(environmentId, { _tag: "attachment", attachmentId: "upload" })).toEqual({
91 _tag: "Success",
92 expiresAt: 1,
93 url: "https://host.test/api/assets/image.png",
94 });
95});
96
97it("lets the server authorize an explicit refresh before the client grant loads", async () => {
98 await expect(useRefreshAssetUrl(environmentId, resource)()).resolves.toBe(
99 "https://host.test/api/assets/image.png",
100 );
101 expect(state.mint).toHaveBeenCalledWith({ environmentId, input: { resource } });
102
103 const denied = new EnvironmentAuthorizationError({
104 message: "This connection cannot read host files.",
105 requiredScope: AuthFilesystemReadScope,
106 });
107 state.mint.mockResolvedValue(AsyncResult.failure(Cause.fail(denied)));
108 await expect(useRefreshAssetUrl(environmentId, resource)()).resolves.toBeNull();
109});