|
| 1 | +import { EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 2 | +import type { |
| 3 | + EnvironmentGitHubIssueListEntry, |
| 4 | + MergedGitHubIssueList, |
| 5 | +} from "@t3tools/client-runtime/state/github-issues"; |
| 6 | +import { describe, expect, it } from "vite-plus/test"; |
| 7 | + |
| 8 | +import { |
| 9 | + gitHubIssueListReadFailed, |
| 10 | + holdGitHubIssueList, |
| 11 | + keepGitHubIssueListRows, |
| 12 | +} from "./githubIssueListKeep.logic"; |
| 13 | + |
| 14 | +function entry(number: number, title: string): EnvironmentGitHubIssueListEntry { |
| 15 | + return { |
| 16 | + environmentId: "env", |
| 17 | + projectId: "project", |
| 18 | + projectTitle: "Project", |
| 19 | + repository: "acme/app", |
| 20 | + number, |
| 21 | + title, |
| 22 | + url: `https://github.com/acme/app/issues/${number}`, |
| 23 | + author: { login: "octo" }, |
| 24 | + assignees: [], |
| 25 | + labels: [], |
| 26 | + state: "open", |
| 27 | + createdAt: "2026-01-01T00:00:00.000Z", |
| 28 | + updatedAt: "2026-01-01T00:00:00.000Z", |
| 29 | + } as unknown as EnvironmentGitHubIssueListEntry; |
| 30 | +} |
| 31 | + |
| 32 | +const goodList: MergedGitHubIssueList = { |
| 33 | + entries: [entry(1, "Fork fold absorbs repairs"), entry(2, "Sync blocked on nightly")], |
| 34 | + errors: [], |
| 35 | + environmentErrors: [], |
| 36 | + truncated: true, |
| 37 | +}; |
| 38 | + |
| 39 | +const allFailed: MergedGitHubIssueList = { |
| 40 | + entries: [], |
| 41 | + errors: [ |
| 42 | + { |
| 43 | + environmentId: EnvironmentId.make("env"), |
| 44 | + projectId: ProjectId.make("project"), |
| 45 | + projectTitle: "Project", |
| 46 | + message: "acme/app could not be read: gh: auth failed", |
| 47 | + }, |
| 48 | + ], |
| 49 | + environmentErrors: [], |
| 50 | + truncated: false, |
| 51 | +}; |
| 52 | + |
| 53 | +describe("gitHubIssueListReadFailed", () => { |
| 54 | + it("reads a no-rows answer that carries project errors as a failure", () => { |
| 55 | + expect(gitHubIssueListReadFailed(allFailed)).toBe(true); |
| 56 | + }); |
| 57 | + |
| 58 | + it("leaves a good list alone", () => { |
| 59 | + expect(gitHubIssueListReadFailed(goodList)).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + it("leaves an environment-level failure alone, which the failed-read path already keeps", () => { |
| 63 | + expect( |
| 64 | + gitHubIssueListReadFailed({ |
| 65 | + ...goodList, |
| 66 | + entries: [], |
| 67 | + environmentErrors: [ |
| 68 | + { environmentId: EnvironmentId.make("env"), message: "The environment did not answer." }, |
| 69 | + ], |
| 70 | + }), |
| 71 | + ).toBe(false); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe("keepGitHubIssueListRows", () => { |
| 76 | + it("keeps the held rows under the failed answer's current errors", () => { |
| 77 | + const kept = keepGitHubIssueListRows(allFailed, goodList); |
| 78 | + expect(kept?.entries.map((issue) => issue.number)).toEqual([1, 2]); |
| 79 | + expect(kept?.errors).toEqual(allFailed.errors); |
| 80 | + expect(kept?.environmentErrors).toEqual([]); |
| 81 | + }); |
| 82 | + |
| 83 | + it("keeps the truncation the held rows were read with", () => { |
| 84 | + expect(keepGitHubIssueListRows(allFailed, goodList)?.truncated).toBe(true); |
| 85 | + expect(keepGitHubIssueListRows(allFailed, { ...goodList, truncated: false })?.truncated).toBe( |
| 86 | + false, |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it("keeps nothing before any rows were read, leaving the failure as the answer", () => { |
| 91 | + expect(keepGitHubIssueListRows(allFailed, null)).toBeNull(); |
| 92 | + expect(keepGitHubIssueListRows(allFailed, { ...goodList, entries: [] })).toBeNull(); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe("holdGitHubIssueList", () => { |
| 97 | + const scope = `[{"environmentId":"env","input":{"projectId":"p"}}]`; |
| 98 | + |
| 99 | + it("keeps the held answer when the read failed for every project in a held scope", () => { |
| 100 | + const store = new Map([[scope, goodList]]); |
| 101 | + holdGitHubIssueList(store, allFailed, scope); |
| 102 | + expect(store.get(scope)).toBe(goodList); |
| 103 | + expect(store.size).toBe(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it("stores a read that answered, and a failure before anything is held", () => { |
| 107 | + const store = new Map<string, MergedGitHubIssueList>(); |
| 108 | + holdGitHubIssueList(store, goodList, scope); |
| 109 | + expect(store.get(scope)).toBe(goodList); |
| 110 | + const empty = new Map<string, MergedGitHubIssueList>(); |
| 111 | + holdGitHubIssueList(empty, allFailed, scope); |
| 112 | + expect(empty.get(scope)).toBe(allFailed); |
| 113 | + }); |
| 114 | + |
| 115 | + it("stores a failure for a scope nothing is held for, leaving other scopes alone", () => { |
| 116 | + const store = new Map([["other", goodList]]); |
| 117 | + holdGitHubIssueList(store, allFailed, scope); |
| 118 | + expect(store.get(scope)).toBe(allFailed); |
| 119 | + expect(store.get("other")).toBe(goodList); |
| 120 | + }); |
| 121 | + |
| 122 | + it("writes nothing when the answer is the one already held, so a re-render stays idle", () => { |
| 123 | + const store = new Map<string, MergedGitHubIssueList>([[scope, allFailed]]); |
| 124 | + holdGitHubIssueList(store, allFailed, scope); |
| 125 | + expect(store.get(scope)).toBe(allFailed); |
| 126 | + }); |
| 127 | +}); |
0 commit comments