Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { expect, it, vi } from "vite-plus/test";
3
4import { useFileDownload } from "./useFileDownload";
5
6const state = vi.hoisted(() => ({
7 canRead: true,
8 createUrl: vi.fn(async () => ({
9 _tag: "Success",
10 value: { relativeUrl: "/api/assets/t/a.zip" },
11 })),
12 download: vi.fn(async () => {}),
13}));
14
15vi.mock("gt-react", async (importOriginal) => ({
16 ...(await importOriginal<typeof import("gt-react")>()),
17 useGT: () => (text: string) => text,
18}));
19vi.mock(import("~/state/session"), async (original) => ({
20 ...(await original()),
21 useEnvironmentScope: () => state.canRead,
22 readPreparedConnection: () => ({ httpBaseUrl: "https://host.example/" }) as never,
23}));
24vi.mock("~/state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => state.createUrl }));
25vi.mock("../media/mediaContent", () => ({ downloadMedia: state.download }));
26
27const environmentId = EnvironmentId.make("env");
28
29it("saves the host file through an attachment asset URL, and only with file access", async () => {
30 const downloaded = Promise.withResolvers<void>();
31 state.download.mockImplementationOnce(async () => downloaded.resolve());
32 useFileDownload(environmentId)!("/repo", Promise.resolve("/repo/dist/a.zip"));
33 await downloaded.promise;
34 expect(state.download).toHaveBeenCalledWith("https://host.example/api/assets/t/a.zip", "a.zip");
35 expect(state.createUrl).toHaveBeenCalledWith({
36 environmentId,
37 input: {
38 resource: {
39 _tag: "draft-workspace-file",
40 cwd: "/repo",
41 path: "/repo/dist/a.zip",
42 disposition: "attachment",
43 },
44 },
45 });
46 state.canRead = false;
47 expect(useFileDownload(environmentId)).toBeNull();
48});