|
| 1 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 2 | +import { createElement } from "react"; |
| 3 | +import { act, create } from "react-test-renderer"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import type { Task, TaskRun } from "../types"; |
| 6 | + |
| 7 | +const { mockUseAuthStore, mockGetImages, mockGetEnvironments } = vi.hoisted( |
| 8 | + () => ({ |
| 9 | + mockUseAuthStore: vi.fn(), |
| 10 | + mockGetImages: vi.fn(), |
| 11 | + mockGetEnvironments: vi.fn(), |
| 12 | + }), |
| 13 | +); |
| 14 | + |
| 15 | +vi.mock("@/features/auth", () => ({ useAuthStore: mockUseAuthStore })); |
| 16 | + |
| 17 | +vi.mock("../api", () => ({ |
| 18 | + getSandboxCustomImages: mockGetImages, |
| 19 | + getSandboxEnvironments: mockGetEnvironments, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("phosphor-react-native", () => ({ |
| 23 | + Cube: (props: Record<string, unknown>) => createElement("Cube", props), |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("@/lib/theme", () => ({ |
| 27 | + toRgba: (hex: string, alpha: number) => `${hex}/${alpha}`, |
| 28 | +})); |
| 29 | + |
| 30 | +import { CustomImageBadge } from "./CustomImageBadge"; |
| 31 | + |
| 32 | +function makeTask(run: Partial<TaskRun> | undefined): Task { |
| 33 | + return { |
| 34 | + id: "task-1", |
| 35 | + task_number: 1, |
| 36 | + slug: "task-1", |
| 37 | + title: "Task", |
| 38 | + description: "", |
| 39 | + created_at: "2026-01-01T00:00:00Z", |
| 40 | + updated_at: "2026-01-01T00:00:00Z", |
| 41 | + origin_product: "user_created", |
| 42 | + latest_run: run |
| 43 | + ? ({ |
| 44 | + id: "run-1", |
| 45 | + task: "task-1", |
| 46 | + team: 1, |
| 47 | + branch: null, |
| 48 | + status: "completed", |
| 49 | + log_url: "", |
| 50 | + error_message: null, |
| 51 | + output: null, |
| 52 | + state: {}, |
| 53 | + created_at: "2026-01-01T00:00:00Z", |
| 54 | + updated_at: "2026-01-01T00:00:00Z", |
| 55 | + completed_at: null, |
| 56 | + ...run, |
| 57 | + } as TaskRun) |
| 58 | + : undefined, |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +async function render(task: Task) { |
| 63 | + const queryClient = new QueryClient({ |
| 64 | + defaultOptions: { queries: { retry: false, gcTime: 0 } }, |
| 65 | + }); |
| 66 | + let renderer: ReturnType<typeof create> | null = null; |
| 67 | + await act(async () => { |
| 68 | + renderer = create( |
| 69 | + createElement( |
| 70 | + QueryClientProvider, |
| 71 | + { client: queryClient }, |
| 72 | + createElement(CustomImageBadge, { task }), |
| 73 | + ), |
| 74 | + ); |
| 75 | + }); |
| 76 | + // Flush pending react-query resolutions so a resolved image name renders. |
| 77 | + await act(async () => { |
| 78 | + await Promise.resolve(); |
| 79 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 80 | + }); |
| 81 | + if (!renderer) throw new Error("Renderer not created"); |
| 82 | + return renderer as ReturnType<typeof create>; |
| 83 | +} |
| 84 | + |
| 85 | +function label(renderer: ReturnType<typeof create>): string | undefined { |
| 86 | + const node = renderer.root.findAll( |
| 87 | + (n) => typeof n.props?.accessibilityLabel === "string", |
| 88 | + )[0]; |
| 89 | + return node?.props.accessibilityLabel as string | undefined; |
| 90 | +} |
| 91 | + |
| 92 | +describe("CustomImageBadge", () => { |
| 93 | + beforeEach(() => { |
| 94 | + mockUseAuthStore.mockReturnValue({ |
| 95 | + projectId: 1, |
| 96 | + oauthAccessToken: "token", |
| 97 | + }); |
| 98 | + mockGetImages.mockReset(); |
| 99 | + mockGetEnvironments.mockReset(); |
| 100 | + mockGetImages.mockResolvedValue([]); |
| 101 | + mockGetEnvironments.mockResolvedValue([]); |
| 102 | + }); |
| 103 | + |
| 104 | + it("renders nothing for a local run", async () => { |
| 105 | + const r = await render( |
| 106 | + makeTask({ environment: "local", state: { custom_image_id: "img-1" } }), |
| 107 | + ); |
| 108 | + expect(r.toJSON()).toBeNull(); |
| 109 | + expect(mockGetImages).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + |
| 112 | + it("renders nothing for a cloud run without a custom image id", async () => { |
| 113 | + const r = await render(makeTask({ environment: "cloud", state: {} })); |
| 114 | + expect(r.toJSON()).toBeNull(); |
| 115 | + expect(mockGetImages).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it("resolves the image name from a direct custom_image_id", async () => { |
| 119 | + mockGetImages.mockResolvedValue([{ id: "img-1", name: "My Image" }]); |
| 120 | + const r = await render( |
| 121 | + makeTask({ environment: "cloud", state: { custom_image_id: "img-1" } }), |
| 122 | + ); |
| 123 | + expect(label(r)).toBe('Runs on custom base image "My Image"'); |
| 124 | + }); |
| 125 | + |
| 126 | + it("resolves the image name via sandbox_environment_id", async () => { |
| 127 | + mockGetEnvironments.mockResolvedValue([ |
| 128 | + { id: "env-1", custom_image_id: "img-2", custom_image_name: null }, |
| 129 | + ]); |
| 130 | + mockGetImages.mockResolvedValue([{ id: "img-2", name: "Env Image" }]); |
| 131 | + const r = await render( |
| 132 | + makeTask({ |
| 133 | + environment: "cloud", |
| 134 | + state: { sandbox_environment_id: "env-1" }, |
| 135 | + }), |
| 136 | + ); |
| 137 | + expect(label(r)).toBe('Runs on custom base image "Env Image"'); |
| 138 | + }); |
| 139 | + |
| 140 | + it("renders nothing when the image cannot be resolved", async () => { |
| 141 | + mockGetImages.mockResolvedValue([{ id: "other", name: "Other" }]); |
| 142 | + const r = await render( |
| 143 | + makeTask({ environment: "cloud", state: { custom_image_id: "missing" } }), |
| 144 | + ); |
| 145 | + expect(r.toJSON()).toBeNull(); |
| 146 | + }); |
| 147 | +}); |
0 commit comments