|
1 | 1 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | +import type { AssetUrlState } from "../assets/assetUrls"; |
2 | 3 | import { AsyncResult } from "effect/reactivity"; |
3 | 4 | import { act, type ComponentProps, type ReactNode } from "react"; |
4 | 5 | import { create, type ReactTestRenderer } from "react-test-renderer"; |
5 | 6 | import { expect, it, vi } from "vite-plus/test"; |
6 | 7 |
|
7 | 8 | const mint = vi.hoisted(() => vi.fn()); |
| 9 | +const asset = vi.hoisted(() => ({ |
| 10 | + state: { _tag: "Loading" } as AssetUrlState, |
| 11 | + listeners: new Set<() => void>(), |
| 12 | +})); |
| 13 | +vi.mock("../assets/assetUrls", async (importOriginal) => { |
| 14 | + const { useSyncExternalStore } = await import("react"); |
| 15 | + return { |
| 16 | + ...(await importOriginal<typeof import("../assets/assetUrls")>()), |
| 17 | + useAssetUrlState: () => |
| 18 | + useSyncExternalStore( |
| 19 | + (listener) => { |
| 20 | + asset.listeners.add(listener); |
| 21 | + return () => { |
| 22 | + asset.listeners.delete(listener); |
| 23 | + }; |
| 24 | + }, |
| 25 | + () => asset.state, |
| 26 | + ), |
| 27 | + }; |
| 28 | +}); |
| 29 | +vi.mock("./media/MediaActions", () => ({ MediaActions: () => null })); |
8 | 30 | vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null })); |
9 | 31 | vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) })); |
10 | 32 | vi.mock("../hooks/useFileMetadata", () => ({ useFileMetadata: () => undefined })); |
@@ -58,7 +80,95 @@ vi.mock("~/lib/openPullRequestLink", () => ({ |
58 | 80 | useOpenChangeRequestLink: () => vi.fn(), |
59 | 81 | })); |
60 | 82 |
|
61 | | -import ChatMarkdown from "./ChatMarkdown"; |
| 83 | +import ChatMarkdown, { ChatMarkdownAssetImage } from "./ChatMarkdown"; |
| 84 | + |
| 85 | +const assetProps = { |
| 86 | + environmentId: EnvironmentId.make("media-environment"), |
| 87 | + resource: { |
| 88 | + _tag: "media-file", |
| 89 | + threadId: ThreadId.make("media-thread"), |
| 90 | + path: "/repo/diagram.svg", |
| 91 | + }, |
| 92 | + alt: "Diagram", |
| 93 | +} satisfies ComponentProps<typeof ChatMarkdownAssetImage>; |
| 94 | + |
| 95 | +async function publishAssetState(state: AssetUrlState) { |
| 96 | + await act(async () => { |
| 97 | + asset.state = state; |
| 98 | + for (const listener of asset.listeners) listener(); |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +it("does not mint a second URL when the initial file preview request completes", async () => { |
| 103 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 104 | + mint.mockClear(); |
| 105 | + asset.state = { _tag: "Loading" }; |
| 106 | + let renderer: ReactTestRenderer | undefined; |
| 107 | + const image = <ChatMarkdownAssetImage {...assetProps} workspaceMutationId="initial" />; |
| 108 | + try { |
| 109 | + await act(async () => { |
| 110 | + renderer = create(image); |
| 111 | + }); |
| 112 | + await publishAssetState({ |
| 113 | + _tag: "Success", |
| 114 | + url: "https://host.test/diagram.svg", |
| 115 | + expiresAt: 1, |
| 116 | + }); |
| 117 | + expect(mint).not.toHaveBeenCalled(); |
| 118 | + } finally { |
| 119 | + await act(async () => renderer?.unmount()); |
| 120 | + vi.unstubAllGlobals(); |
| 121 | + } |
| 122 | +}); |
| 123 | + |
| 124 | +it.each( |
| 125 | + (["Loading", "Success", "Failure"] as const).flatMap((initial) => |
| 126 | + (["Success", "Failure"] as const).map((outcome) => [initial, outcome] as const), |
| 127 | + ), |
| 128 | +)( |
| 129 | + "refreshes a file changed during a pending %s request after it ends in %s", |
| 130 | + async (initial, outcome) => { |
| 131 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 132 | + mint.mockReset(); |
| 133 | + mint.mockResolvedValue( |
| 134 | + AsyncResult.success({ relativeUrl: "/api/assets/updated.svg", expiresAt: 1 }), |
| 135 | + ); |
| 136 | + asset.state = |
| 137 | + initial === "Success" |
| 138 | + ? { _tag: "Success", url: "https://host.test/stale.svg", expiresAt: 1, waiting: true } |
| 139 | + : { _tag: initial, waiting: true }; |
| 140 | + let renderer: ReactTestRenderer | undefined; |
| 141 | + try { |
| 142 | + await act(async () => { |
| 143 | + renderer = create(<ChatMarkdownAssetImage {...assetProps} workspaceMutationId="initial" />); |
| 144 | + }); |
| 145 | + for (const mutation of ["edit-1", "edit-2"]) { |
| 146 | + await act(async () => { |
| 147 | + renderer!.update( |
| 148 | + <ChatMarkdownAssetImage {...assetProps} workspaceMutationId={mutation} />, |
| 149 | + ); |
| 150 | + }); |
| 151 | + } |
| 152 | + expect(mint).not.toHaveBeenCalled(); |
| 153 | + await publishAssetState( |
| 154 | + outcome === "Failure" |
| 155 | + ? { _tag: "Failure" } |
| 156 | + : { _tag: "Success", url: "https://host.test/stale.svg", expiresAt: 1 }, |
| 157 | + ); |
| 158 | + expect(mint).toHaveBeenCalledExactlyOnceWith({ |
| 159 | + environmentId: assetProps.environmentId, |
| 160 | + input: { resource: assetProps.resource }, |
| 161 | + }); |
| 162 | + await act(async () => { |
| 163 | + renderer!.update(<ChatMarkdownAssetImage {...assetProps} workspaceMutationId="edit-2" />); |
| 164 | + }); |
| 165 | + expect(mint).toHaveBeenCalledTimes(1); |
| 166 | + } finally { |
| 167 | + await act(async () => renderer?.unmount()); |
| 168 | + vi.unstubAllGlobals(); |
| 169 | + } |
| 170 | + }, |
| 171 | +); |
62 | 172 |
|
63 | 173 | it("opens host media through server authorization before the client grant loads", async () => { |
64 | 174 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
|
0 commit comments