Files
kandev/apps/web/hooks/domains/github/use-pr-info-by-url.test.ts
T

624 lines
22 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { act, cleanup, renderHook, waitFor } from "@testing-library/react";
const fetchPRInfoMock = vi.fn();
const fetchIssueInfoMock = vi.fn();
vi.mock("@/lib/api/domains/github-api", () => ({
fetchPRInfo: (...args: unknown[]) => fetchPRInfoMock(...args),
fetchIssueInfo: (...args: unknown[]) => fetchIssueInfoMock(...args),
}));
// Import after mocks so the hook picks up the mocked module.
import { usePRInfoByURL, parseGitHubIssueUrl, parseGitHubPrUrl } from "./use-pr-info-by-url";
import { pluginRegistry } from "@/lib/plugins/registry";
import type { RepositoryInspection } from "@/lib/plugins/types";
afterEach(() => {
cleanup();
pluginRegistry.unregisterPlugin(BITBUCKET_PLUGIN_ID);
pluginRegistry.unregisterPlugin("test-coarse-provider");
pluginRegistry.unregisterPlugin("test-exact-provider");
fetchPRInfoMock.mockReset();
fetchIssueInfoMock.mockReset();
vi.useRealTimers();
});
const PR_URL_A = "https://github.com/acme/site/pull/42";
const PR_URL_B = "https://github.com/acme/api/pull/7";
const ISSUE_URL_A = "https://github.com/acme/site/issues/1456";
const REPO_URL = "https://github.com/acme/site";
const WORKSPACE_ID = "workspace-1";
const BITBUCKET_PLUGIN_ID = "test-bitbucket-provider";
function makePR(overrides: { number: number; title?: string; head?: string; base?: string }) {
return {
number: overrides.number,
title: overrides.title ?? "Test PR",
head_branch: overrides.head ?? "feature/x",
base_branch: overrides.base ?? "main",
body: "",
url: "",
html_url: "",
state: "open" as const,
author_login: "",
repo_owner: "",
repo_name: "",
draft: false,
};
}
function makeIssue(overrides: { number: number; title?: string; body?: string }) {
return {
number: overrides.number,
title: overrides.title ?? "Test issue",
body: overrides.body ?? "Issue body",
url: "",
html_url: "",
state: "open" as const,
author_login: "octocat",
repo_owner: "acme",
repo_name: "site",
labels: [],
assignees: [],
created_at: "",
updated_at: "",
closed_at: null,
};
}
describe("parseGitHubPrUrl", () => {
it("returns owner/repo/prNumber for a canonical PR URL", () => {
expect(parseGitHubPrUrl(PR_URL_A)).toEqual({
owner: "acme",
repo: "site",
prNumber: 42,
});
});
it("tolerates trailing path/hash (e.g. /files#diff-…)", () => {
expect(parseGitHubPrUrl(`${PR_URL_A}/files#diff-abc`)).toEqual({
owner: "acme",
repo: "site",
prNumber: 42,
});
});
it("returns null for non-PR URLs (plain repo, invalid, empty)", () => {
expect(parseGitHubPrUrl(REPO_URL)).toBeNull();
expect(parseGitHubPrUrl("not a url")).toBeNull();
expect(parseGitHubPrUrl("")).toBeNull();
});
});
describe("parseGitHubIssueUrl", () => {
it("returns owner/repo/issueNumber for a canonical issue URL", () => {
expect(parseGitHubIssueUrl(ISSUE_URL_A)).toEqual({
owner: "acme",
repo: "site",
issueNumber: 1456,
});
});
it("tolerates trailing path/hash", () => {
expect(parseGitHubIssueUrl(`${ISSUE_URL_A}#issuecomment-1`)).toEqual({
owner: "acme",
repo: "site",
issueNumber: 1456,
});
});
it("returns null for PR URLs, plain repo URLs, invalid input, and empty input", () => {
expect(parseGitHubIssueUrl(PR_URL_A)).toBeNull();
expect(parseGitHubIssueUrl(REPO_URL)).toBeNull();
expect(parseGitHubIssueUrl("not a url")).toBeNull();
expect(parseGitHubIssueUrl("")).toBeNull();
});
});
describe("usePRInfoByURL", () => {
it("inspects every coarse URL match and selects the one exact provider", async () => {
const url = "https://code.example.test/projects/PLATFORM/repos/web/pull-requests/42";
const coarseInspect = vi.fn().mockResolvedValue(null);
const exactInspect = vi.fn().mockResolvedValue({
providerId: "exact",
providerHost: "https://code.example.test",
ownerOrProject: "PLATFORM",
repositoryId: "web-42",
repositoryName: "web",
cloneUrl: "https://code.example.test/scm/PLATFORM/web.git",
baseBranch: "main",
headBranch: "feature/exact",
pullRequest: { number: 42, title: "Exact provider" },
});
pluginRegistry.forPlugin("test-coarse-provider").registerRepositoryProvider({
id: "coarse",
label: "Coarse",
listRepositories: async () => [],
matchesURL: () => true,
listBranches: async () => [],
inspectURL: coarseInspect,
});
pluginRegistry.forPlugin("test-exact-provider").registerRepositoryProvider({
id: "exact",
label: "Exact",
listRepositories: async () => [],
matchesURL: () => true,
listBranches: async () => [],
inspectURL: exactInspect,
});
const { result } = renderHook(() => usePRInfoByURL(WORKSPACE_ID));
act(() => result.current.ensure(url));
await waitFor(() => expect(result.current.info(url)?.prHeadBranch).toBe("feature/exact"));
expect(coarseInspect).toHaveBeenCalledOnce();
expect(exactInspect).toHaveBeenCalledOnce();
});
});
describe("usePRInfoByURL registered provider metadata", () => {
it("falls back to the built-in owner after a matcherless provider returns no match", async () => {
const inspectURL = vi.fn().mockResolvedValue(null);
fetchPRInfoMock.mockResolvedValue(makePR({ number: 42, head: "github-head" }));
pluginRegistry.forPlugin(BITBUCKET_PLUGIN_ID).registerRepositoryProvider({
id: "bitbucket",
label: "Bitbucket",
listRepositories: async () => [],
listBranches: async () => [],
inspectURL,
});
const { result } = renderHook(() => usePRInfoByURL(WORKSPACE_ID));
act(() => result.current.ensure(PR_URL_A));
await waitFor(() => expect(result.current.settled(PR_URL_A)).toBe(true));
expect(result.current.info(PR_URL_A)?.prHeadBranch).toBe("github-head");
expect(result.current.inspection?.(PR_URL_A)).toBeUndefined();
expect(inspectURL).toHaveBeenCalledOnce();
expect(fetchPRInfoMock).toHaveBeenCalledOnce();
});
it("imports pull-request metadata from a registered provider inspection", async () => {
const url =
"https://bitbucket.example.test/bitbucket/projects/PLATFORM/repos/web/pull-requests/42";
const inspection: RepositoryInspection = {
providerId: "bitbucket",
providerHost: "https://bitbucket.example.test/bitbucket",
ownerOrProject: "PLATFORM",
repositoryId: "web-42",
repositoryName: "web",
cloneUrl: "https://bitbucket.example.test/bitbucket/scm/PLATFORM/web.git",
baseBranch: "main",
headBranch: "feature/dc",
pullRequest: { number: 42, title: "Data Center pull request" },
};
const inspectURL = vi.fn().mockResolvedValue(inspection);
pluginRegistry.forPlugin(BITBUCKET_PLUGIN_ID).registerRepositoryProvider({
id: "bitbucket",
label: "Bitbucket",
listRepositories: async () => [],
matchesURL: (candidate) => candidate.includes("bitbucket.example.test/bitbucket/"),
listBranches: async () => [],
inspectURL,
});
const { result } = renderHook(() => usePRInfoByURL(WORKSPACE_ID));
act(() => result.current.ensure(url));
await waitFor(() =>
expect(result.current.info(url)).toEqual({
prHeadBranch: "feature/dc",
prBaseBranch: "main",
prNumber: 42,
suggestedTitle: "PR #42: Data Center pull request",
}),
);
expect(inspectURL).toHaveBeenCalledWith(
expect.objectContaining({ workspaceId: WORKSPACE_ID, url }),
);
expect(fetchPRInfoMock).not.toHaveBeenCalled();
});
it("retries an unsupported provider URL after its plugin finishes booting", async () => {
const url = "https://bitbucket.example.test/projects/PLATFORM/repos/web/pull-requests/42";
const { result } = renderHook(() => usePRInfoByURL(WORKSPACE_ID));
act(() => result.current.ensure(url));
expect(result.current.info(url)).toBeUndefined();
const inspectURL = vi.fn().mockResolvedValue({
providerId: "bitbucket",
providerHost: "https://bitbucket.example.test",
ownerOrProject: "PLATFORM",
repositoryId: "web-42",
repositoryName: "web",
cloneUrl: "https://bitbucket.example.test/scm/PLATFORM/web.git",
baseBranch: "main",
headBranch: "feature/plugin-ready",
pullRequest: { number: 42, title: "Plugin ready" },
});
act(() => {
pluginRegistry.forPlugin(BITBUCKET_PLUGIN_ID).registerRepositoryProvider({
id: "bitbucket",
label: "Bitbucket",
listRepositories: async () => [],
matchesURL: (candidate) => candidate.startsWith("https://bitbucket.example.test/"),
listBranches: async () => [],
inspectURL,
});
});
act(() => result.current.ensure(url));
await waitFor(() =>
expect(result.current.info(url)?.prHeadBranch).toBe("feature/plugin-ready"),
);
expect(inspectURL).toHaveBeenCalledOnce();
});
});
describe("usePRInfoByURL — GitHub pull requests", () => {
it("fetches PR info once per unique PR URL when ensure() is called", async () => {
fetchPRInfoMock.mockImplementation((_ws: string, _o: string, _r: string, n: number) => {
return Promise.resolve(makePR({ number: n, head: n === 42 ? "feat-a" : "feat-b" }));
});
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(PR_URL_A);
result.current.ensure(PR_URL_B);
});
await waitFor(() => {
expect(result.current.info(PR_URL_A)).toBeDefined();
expect(result.current.info(PR_URL_B)).toBeDefined();
});
expect(fetchPRInfoMock).toHaveBeenCalledTimes(2);
expect(result.current.info(PR_URL_A)).toMatchObject({
prHeadBranch: "feat-a",
prBaseBranch: "main",
prNumber: 42,
suggestedTitle: "PR #42: Test PR",
});
expect(result.current.info(PR_URL_B)).toMatchObject({
prHeadBranch: "feat-b",
prNumber: 7,
});
});
it("dedupes concurrent ensure() calls for the same URL into a single fetch", async () => {
fetchPRInfoMock.mockResolvedValue(makePR({ number: 42 }));
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(PR_URL_A);
result.current.ensure(PR_URL_A);
result.current.ensure(PR_URL_A);
});
await waitFor(() => expect(result.current.info(PR_URL_A)).toBeDefined());
expect(fetchPRInfoMock).toHaveBeenCalledTimes(1);
});
it("reports loading(url) true during fetch and false after settle", async () => {
let resolveFetch: ((v: ReturnType<typeof makePR>) => void) | null = null;
fetchPRInfoMock.mockImplementation(
() =>
new Promise((resolve) => {
resolveFetch = resolve;
}),
);
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(PR_URL_A);
});
await waitFor(() => expect(result.current.loading(PR_URL_A)).toBe(true));
act(() => {
resolveFetch?.(makePR({ number: 42 }));
});
await waitFor(() => expect(result.current.loading(PR_URL_A)).toBe(false));
expect(result.current.info(PR_URL_A)).toBeDefined();
});
});
describe("usePRInfoByURL — non-PR and issue URLs", () => {
it("no-ops (no fetch, no cached info) for a non-PR repo URL", async () => {
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(REPO_URL);
});
// Nothing to wait for — the call should be synchronous. Assert no fetch.
expect(fetchPRInfoMock).not.toHaveBeenCalled();
expect(result.current.info(REPO_URL)).toBeUndefined();
expect(result.current.loading(REPO_URL)).toBe(false);
// Repeated ensure() for the same non-PR URL also no-ops (dedup via loadedRef).
act(() => {
result.current.ensure(REPO_URL);
});
expect(fetchPRInfoMock).not.toHaveBeenCalled();
});
it("clear() is a no-op after ensure() records a non-GitHub URL as loaded", () => {
const url = "https://example.com/not-github";
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(url);
result.current.clear(url);
});
expect(fetchPRInfoMock).not.toHaveBeenCalled();
expect(fetchIssueInfoMock).not.toHaveBeenCalled();
expect(result.current.info(url)).toBeUndefined();
expect(result.current.loading(url)).toBe(false);
});
it("fetches issue info and exposes a suggested title for GitHub issue URLs", async () => {
fetchIssueInfoMock.mockResolvedValue(makeIssue({ number: 1456, title: "Fix remote picker" }));
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(ISSUE_URL_A);
});
await waitFor(() => expect(result.current.info(ISSUE_URL_A)).toBeDefined());
expect(fetchIssueInfoMock).toHaveBeenCalledTimes(1);
expect(fetchIssueInfoMock).toHaveBeenCalledWith(
"ws-1",
"acme",
"site",
1456,
expect.any(Object),
);
expect(fetchPRInfoMock).not.toHaveBeenCalled();
expect(result.current.info(ISSUE_URL_A)).toMatchObject({
issueNumber: 1456,
suggestedTitle: "Issue #1456: Fix remote picker",
});
});
it("ignores ensure() with empty string", () => {
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure("");
});
expect(fetchPRInfoMock).not.toHaveBeenCalled();
});
});
describe("usePRInfoByURL — cache invalidation", () => {
it("retries failed metadata only after clear()", async () => {
fetchPRInfoMock.mockRejectedValueOnce(new Error("GitHub is unavailable"));
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(PR_URL_A);
});
await waitFor(() => expect(result.current.loading(PR_URL_A)).toBe(false));
fetchPRInfoMock.mockResolvedValueOnce(makePR({ number: 42 }));
act(() => {
result.current.ensure(PR_URL_A);
});
expect(fetchPRInfoMock).toHaveBeenCalledTimes(1);
act(() => {
result.current.clear(PR_URL_A);
result.current.ensure(PR_URL_A);
});
await waitFor(() => expect(result.current.info(PR_URL_A)?.prNumber).toBe(42));
});
it("retains a metadata failure for its URL without losing a successful sibling", async () => {
const failure = new Error("GitHub is unavailable");
fetchPRInfoMock.mockResolvedValueOnce(makePR({ number: 42 })).mockRejectedValueOnce(failure);
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(PR_URL_A);
result.current.ensure(PR_URL_B);
});
await waitFor(() => expect(result.current.loading(PR_URL_B)).toBe(false));
expect(result.current.info(PR_URL_A)?.prNumber).toBe(42);
expect(
(result.current as unknown as { error: (url: string) => Error | undefined }).error(PR_URL_B),
).toBe(failure);
});
it("does not re-fetch when ensure() is called for an already-loaded URL", async () => {
fetchPRInfoMock.mockResolvedValue(makePR({ number: 42 }));
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(PR_URL_A);
});
await waitFor(() => expect(result.current.info(PR_URL_A)).toBeDefined());
act(() => {
result.current.ensure(PR_URL_A);
});
expect(fetchPRInfoMock).toHaveBeenCalledTimes(1);
});
it("clear(url) forgets the cached entry so the next ensure() re-fetches", async () => {
fetchPRInfoMock.mockResolvedValue(makePR({ number: 42 }));
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(PR_URL_A);
});
await waitFor(() => expect(result.current.info(PR_URL_A)).toBeDefined());
expect(fetchPRInfoMock).toHaveBeenCalledTimes(1);
act(() => {
result.current.clear(PR_URL_A);
});
expect(result.current.info(PR_URL_A)).toBeUndefined();
act(() => {
result.current.ensure(PR_URL_A);
});
await waitFor(() => expect(fetchPRInfoMock).toHaveBeenCalledTimes(2));
});
it("returns undefined info / false loading for an unknown URL", () => {
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
expect(result.current.info("https://github.com/who/what/pull/1")).toBeUndefined();
expect(result.current.loading("https://github.com/who/what/pull/1")).toBe(false);
});
});
describe("usePRInfoByURL — stale callback protection", () => {
it("ignores a stale fetch resolved after clear() + ensure() restarted the request", async () => {
// Simulates the race the per-URL sequence counter guards: the first
// fetch is still in flight when the caller clear()s and re-ensure()s;
// the SECOND fetch resolves first; the FIRST (stale) fetch then
// resolves and must NOT clobber the state.
let resolveFirst: ((v: ReturnType<typeof makePR>) => void) | null = null;
let resolveSecond: ((v: ReturnType<typeof makePR>) => void) | null = null;
fetchPRInfoMock
.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveFirst = resolve;
}),
)
.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveSecond = resolve;
}),
);
const { result } = renderHook(() => usePRInfoByURL("ws-1"));
act(() => {
result.current.ensure(PR_URL_A);
});
await waitFor(() => expect(fetchPRInfoMock).toHaveBeenCalledTimes(1));
// Clear forgets the cached entry AND aborts the in-flight request;
// immediately re-ensure() to kick off a fresh request for the same URL.
act(() => {
result.current.clear(PR_URL_A);
result.current.ensure(PR_URL_A);
});
await waitFor(() => expect(fetchPRInfoMock).toHaveBeenCalledTimes(2));
// Second fetch resolves first with the up-to-date PR (title "Fresh").
act(() => {
resolveSecond?.(makePR({ number: 99, title: "Fresh" }));
});
await waitFor(() => expect(result.current.info(PR_URL_A)?.prNumber).toBe(99));
// Now the stale first fetch resolves — its callback must observe that
// its sequence number is no longer current and bail without overwriting
// the fresh state.
act(() => {
resolveFirst?.(makePR({ number: 42, title: "Stale" }));
});
// Give the microtask queue a chance to drain.
await act(async () => {
await Promise.resolve();
});
expect(result.current.info(PR_URL_A)?.prNumber).toBe(99);
expect(result.current.info(PR_URL_A)?.suggestedTitle).toBe("PR #99: Fresh");
});
});
describe("usePRInfoByURL — workspace scope", () => {
it("ignores metadata from a previous workspace after the replacement request starts", async () => {
let resolveWorkspaceA: ((value: ReturnType<typeof makePR>) => void) | undefined;
let resolveWorkspaceB: ((value: ReturnType<typeof makePR>) => void) | undefined;
fetchPRInfoMock
.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveWorkspaceA = resolve;
}),
)
.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveWorkspaceB = resolve;
}),
);
const { result, rerender } = renderHook(
({ workspaceId }: { workspaceId: string }) => usePRInfoByURL(workspaceId),
{ initialProps: { workspaceId: WORKSPACE_ID } },
);
act(() => result.current.ensure(PR_URL_A));
await waitFor(() => expect(fetchPRInfoMock).toHaveBeenCalledTimes(1));
rerender({ workspaceId: "workspace-2" });
act(() => result.current.ensure(PR_URL_A));
await waitFor(() => expect(fetchPRInfoMock).toHaveBeenCalledTimes(2));
act(() => resolveWorkspaceB?.(makePR({ number: 99, title: "Workspace B" })));
await waitFor(() => expect(result.current.info(PR_URL_A)?.prNumber).toBe(99));
act(() => resolveWorkspaceA?.(makePR({ number: 42, title: "Workspace A" })));
await act(async () => {
await Promise.resolve();
});
expect(result.current.info(PR_URL_A)?.prNumber).toBe(99);
expect(result.current.info(PR_URL_A)?.suggestedTitle).toBe("PR #99: Workspace B");
});
it("ignores a rejected request from a previous workspace after the replacement succeeds", async () => {
let rejectWorkspaceA: ((reason?: unknown) => void) | undefined;
let resolveWorkspaceB: ((value: ReturnType<typeof makePR>) => void) | undefined;
fetchPRInfoMock
.mockImplementationOnce(
() =>
new Promise((_resolve, reject) => {
rejectWorkspaceA = reject;
}),
)
.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveWorkspaceB = resolve;
}),
);
const { result, rerender } = renderHook(
({ workspaceId }: { workspaceId: string }) => usePRInfoByURL(workspaceId),
{ initialProps: { workspaceId: WORKSPACE_ID } },
);
act(() => result.current.ensure(PR_URL_A));
await waitFor(() => expect(fetchPRInfoMock).toHaveBeenCalledTimes(1));
rerender({ workspaceId: "workspace-2" });
act(() => result.current.ensure(PR_URL_A));
await waitFor(() => expect(fetchPRInfoMock).toHaveBeenCalledTimes(2));
act(() => resolveWorkspaceB?.(makePR({ number: 99, title: "Workspace B" })));
await waitFor(() => expect(result.current.info(PR_URL_A)?.prNumber).toBe(99));
act(() => rejectWorkspaceA?.(new Error("workspace A failed")));
await act(async () => {
await Promise.resolve();
});
expect(result.current.info(PR_URL_A)?.prNumber).toBe(99);
expect(result.current.info(PR_URL_A)?.suggestedTitle).toBe("PR #99: Workspace B");
expect(result.current.error(PR_URL_A)).toBeUndefined();
expect(result.current.loading(PR_URL_A)).toBe(false);
});
});