Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/shared/src/repository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { getTaskRepository, parseRepository } from "./repository";

describe("getTaskRepository", () => {
it.each([
{ name: "undefined task", task: undefined, expected: null },
{ name: "null task", task: null, expected: null },
{ name: "missing repository", task: {}, expected: null },
{ name: "null repository", task: { repository: null }, expected: null },
{
name: "populated repository",
task: { repository: "posthog/code" },
expected: "posthog/code",
},
])("returns $expected for $name", ({ task, expected }) => {
expect(getTaskRepository(task)).toBe(expected);
});
});

describe("parseRepository", () => {
it("splits an org/repo string", () => {
expect(parseRepository("posthog/code")).toEqual({
organization: "posthog",
repoName: "code",
});
});

it("returns null for malformed input", () => {
expect(parseRepository("code")).toBeNull();
expect(parseRepository("a/b/c")).toBeNull();
});
});
8 changes: 4 additions & 4 deletions packages/shared/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export const parseRepository = (
return { organization: result[0], repoName: result[1] };
};

export function getTaskRepository(task: {
repository?: string | null;
}): string | null {
return task.repository ?? null;
export function getTaskRepository(
task: { repository?: string | null } | null | undefined,
): string | null {
return task?.repository ?? null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ import type { PropsWithChildren } from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { useRefreshedTask } from "./useRefreshedTask";

const mocks = vi.hoisted(() => ({ getTask: vi.fn() }));
const mocks = vi.hoisted(() => ({
getTask: vi.fn(),
useQuery: vi.fn(),
actualUseQuery: undefined as unknown,
}));

vi.mock("@posthog/ui/features/auth/authClientImperative", () => ({
getAuthenticatedClient: vi.fn(async () => ({ getTask: mocks.getTask })),
}));

vi.mock("@tanstack/react-query", async (importOriginal) => {
const actual = await importOriginal<typeof import("@tanstack/react-query")>();
// Keep a handle on the real useQuery so beforeEach can restore delegation
// after a test overrides the mock's return value.
mocks.actualUseQuery = actual.useQuery;
return { ...actual, useQuery: mocks.useQuery };
});

function task(runId: string, status: "failed" | "in_progress"): Task {
return {
id: "task-123",
Expand All @@ -30,6 +42,27 @@ function task(runId: string, status: "failed" | "in_progress"): Task {
describe("useRefreshedTask", () => {
beforeEach(() => {
mocks.getTask.mockReset();
mocks.useQuery.mockReset();
// Delegate to the real useQuery by default; a test can override the return
// value to simulate a specific cache state.
mocks.useQuery.mockImplementation(
mocks.actualUseQuery as typeof import("@tanstack/react-query").useQuery,
);
});

it("falls back to initialTask when the query yields undefined data", () => {
// A separate observer subscribing to the same query key without initialData
// can create the cache entry first while the fetch is in flight, so React
// Query hands this hook `data: undefined` despite the initialData option.
// The hook must never surface undefined (it flows into getTaskRepository).
mocks.useQuery.mockReturnValue({ data: undefined });
const initialTask = task("run-parent", "in_progress");

const { result } = renderHook(() =>
useRefreshedTask("task-123", initialTask),
);

expect(result.current).toBe(initialTask);
});

it("replaces a cached failed run with the authoritative resumed run", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ export function useRefreshedTask(taskId: string, initialTask: Task): Task {
refetchOnMount: "always",
});

return data;
// Guard against `data` being undefined: another observer subscribing to the
// same query key without `initialData` can create the cache entry first while
// the fetch is in flight, causing React Query to drop this hook's initialData.
return data ?? initialTask;
}
Loading