The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 2 | import { type AssetResource, EnvironmentId } from "@t3tools/contracts"; |
| 3 | import { expect, it, vi } from "vite-plus/test"; |
| 4 | |
| 5 | import { render } from "../../../test/render"; |
| 6 | import { PullRequestMarkdown, PullRequestMarkdownContext } from "./PullRequestMarkdown"; |
| 7 | |
| 8 | const requested = vi.hoisted(() => [] as Array<AssetResource | null>); |
| 9 | vi.mock(import("~/assets/assetUrls"), async (original) => ({ |
| 10 | ...(await original()), |
| 11 | useAssetUrlState: (_environmentId, resource) => { |
| 12 | requested.push(resource); |
| 13 | return { _tag: "Loading" }; |
| 14 | }, |
| 15 | })); |
| 16 | |
| 17 | function setup() { |
| 18 | requested.length = 0; |
| 19 | } |
| 20 | |
| 21 | async function renderBody( |
| 22 | host: string, |
| 23 | options: { lineBreaks?: boolean; repository?: string; text?: string } = {}, |
| 24 | ) { |
| 25 | const { repository = null, text = "first line\nsecond line", ...props } = options; |
| 26 | const context = { |
| 27 | remarkPlugins: [], |
| 28 | lineBreaks: sourceControlClients.get(host).hardLineBreaks ?? false, |
| 29 | threadRef: null, |
| 30 | repository, |
| 31 | }; |
| 32 | return render( |
| 33 | <PullRequestMarkdownContext value={context}> |
| 34 | <PullRequestMarkdown |
| 35 | text={text} |
| 36 | cwd="/repo" |
| 37 | environmentId={EnvironmentId.make("local")} |
| 38 | {...props} |
| 39 | /> |
| 40 | </PullRequestMarkdownContext>, |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | it.each(["github", "forgejo", "azure-devops"])("%s breaks a single newline", async (host) => { |
| 45 | await using screen = await renderBody(host); |
| 46 | await expect.element(screen.getByText(/first line/)).toBeInTheDocument(); |
| 47 | expect(screen.container.querySelectorAll("p br")).toHaveLength(1); |
| 48 | }); |
| 49 | |
| 50 | it.each(["gitlab", "bitbucket", "gitcafe", "unknown"])( |
| 51 | "%s joins a single newline", |
| 52 | async (host) => { |
| 53 | await using screen = await renderBody(host); |
| 54 | await expect.element(screen.getByText(/first line/)).toBeInTheDocument(); |
| 55 | expect(screen.container.querySelectorAll("p br")).toHaveLength(0); |
| 56 | }, |
| 57 | ); |
| 58 | |
| 59 | it("lets text the host did not write opt out of the host's breaks", async () => { |
| 60 | await using screen = await renderBody("github", { lineBreaks: false }); |
| 61 | await expect.element(screen.getByText(/first line/)).toBeInTheDocument(); |
| 62 | expect(screen.container.querySelectorAll("p br")).toHaveLength(0); |
| 63 | }); |
| 64 | |
| 65 | it("loads the body's host media in the pull request's repository, not the checkout's", async () => { |
| 66 | setup(); |
| 67 | const image = "https://github.com/user-attachments/assets/7195f963-51a9-4331-be74-3e06be760422"; |
| 68 | const video = "https://github.com/user-attachments/assets/2f8c1a90-1b2c-4d5e-8f90-abcdef123456"; |
| 69 | await using _screen = await renderBody("github", { |
| 70 | repository: "enterprise/private", |
| 71 | text: `\n\n${video}`, |
| 72 | }); |
| 73 | await vi.waitFor(() => |
| 74 | expect(requested).toEqual( |
| 75 | expect.arrayContaining([ |
| 76 | expect.objectContaining({ |
| 77 | cwd: "/repo", |
| 78 | url: expect.stringContaining("7195f963"), |
| 79 | repository: "enterprise/private", |
| 80 | }), |
| 81 | expect.objectContaining({ |
| 82 | cwd: "/repo", |
| 83 | url: expect.stringContaining("2f8c1a90"), |
| 84 | repository: "enterprise/private", |
| 85 | }), |
| 86 | ]), |
| 87 | ), |
| 88 | ); |
| 89 | }); |