fix: session tab leak, PR icon crash, and PR review prompt scope (#897)
This commit is contained in:
@@ -3,7 +3,40 @@ import { create } from "zustand";
|
||||
import { immer } from "zustand/middleware/immer";
|
||||
import { createGitHubSlice } from "./github-slice";
|
||||
import type { GitHubSlice } from "./types";
|
||||
import type { GitHubStatus } from "@/lib/types/github";
|
||||
import type { GitHubStatus, TaskPR } from "@/lib/types/github";
|
||||
|
||||
function makePR(overrides: Partial<TaskPR> = {}): TaskPR {
|
||||
return {
|
||||
id: "id",
|
||||
task_id: "task-1",
|
||||
owner: "o",
|
||||
repo: "r",
|
||||
pr_number: 1,
|
||||
pr_url: "",
|
||||
pr_title: "Test PR",
|
||||
head_branch: "feat",
|
||||
base_branch: "main",
|
||||
author_login: "alice",
|
||||
state: "open",
|
||||
review_state: "",
|
||||
checks_state: "",
|
||||
mergeable_state: "",
|
||||
review_count: 0,
|
||||
pending_review_count: 0,
|
||||
comment_count: 0,
|
||||
unresolved_review_threads: 0,
|
||||
checks_total: 0,
|
||||
checks_passing: 0,
|
||||
additions: 0,
|
||||
deletions: 0,
|
||||
created_at: "",
|
||||
merged_at: null,
|
||||
closed_at: null,
|
||||
last_synced_at: null,
|
||||
updated_at: "",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function makeStore() {
|
||||
return create<GitHubSlice>()(immer((...a) => createGitHubSlice(...a)));
|
||||
@@ -104,3 +137,46 @@ describe("applyGitHubRateLimitUpdate", () => {
|
||||
expect(store.getState().githubStatus.status).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("setTaskPR", () => {
|
||||
it("appends a PR when the task has no rows yet", () => {
|
||||
const store = makeStore();
|
||||
const pr = makePR({ repository_id: "repo-a" });
|
||||
|
||||
store.getState().setTaskPR("task-1", pr);
|
||||
|
||||
expect(store.getState().taskPRs.byTaskId["task-1"]).toEqual([pr]);
|
||||
});
|
||||
|
||||
it("upserts by repository_id so multi-repo PRs coexist", () => {
|
||||
const store = makeStore();
|
||||
const prA = makePR({ id: "a", repository_id: "repo-a", pr_number: 1 });
|
||||
const prB = makePR({ id: "b", repository_id: "repo-b", pr_number: 2 });
|
||||
const prAUpdated = makePR({ id: "a", repository_id: "repo-a", pr_number: 1, additions: 10 });
|
||||
|
||||
store.getState().setTaskPR("task-1", prA);
|
||||
store.getState().setTaskPR("task-1", prB);
|
||||
store.getState().setTaskPR("task-1", prAUpdated);
|
||||
|
||||
const list = store.getState().taskPRs.byTaskId["task-1"];
|
||||
expect(list).toHaveLength(2);
|
||||
expect(list.find((p) => p.repository_id === "repo-a")?.additions).toBe(10);
|
||||
expect(list.find((p) => p.repository_id === "repo-b")?.id).toBe("b");
|
||||
});
|
||||
|
||||
it("heals a corrupted non-array entry instead of throwing", () => {
|
||||
// Simulates a stray payload landing in byTaskId[taskId] as something other
|
||||
// than an array (e.g. a partial hydration). The next setTaskPR call must
|
||||
// recover rather than propagate the bad shape, otherwise downstream
|
||||
// renderers crash with `prs is not iterable`.
|
||||
const store = makeStore();
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
store.getState().setTaskPRs({ "task-1": {} as any });
|
||||
|
||||
const pr = makePR({ repository_id: "repo-a" });
|
||||
expect(() => store.getState().setTaskPR("task-1", pr)).not.toThrow();
|
||||
|
||||
expect(Array.isArray(store.getState().taskPRs.byTaskId["task-1"])).toBe(true);
|
||||
expect(store.getState().taskPRs.byTaskId["task-1"]).toEqual([pr]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user