|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { upsertOrbInstallation } from "../../src/orb/installations"; |
| 3 | +import { createTestEnv, type TestD1Database } from "../helpers/d1"; |
| 4 | + |
| 5 | +const created = (id: number) => ({ action: "created", installation: { id, account: { login: "acme", type: "Organization" }, repository_selection: "selected" } }); |
| 6 | +const get = (e: Env, id: number) => |
| 7 | + (e.DB as unknown as TestD1Database) |
| 8 | + .prepare("SELECT account_login, account_type, repository_selection, registered, suspended_at, removed_at FROM orb_github_installations WHERE installation_id=?") |
| 9 | + .bind(id) |
| 10 | + .first<{ account_login: string; account_type: string; repository_selection: string; registered: number; suspended_at: string | null; removed_at: string | null }>(); |
| 11 | + |
| 12 | +describe("upsertOrbInstallation", () => { |
| 13 | + it("'created' registers the install (registered=0 — the manual-onboarding gate)", async () => { |
| 14 | + const e = createTestEnv(); |
| 15 | + await upsertOrbInstallation(e, "installation", created(100)); |
| 16 | + expect(await get(e, 100)).toMatchObject({ account_login: "acme", account_type: "Organization", repository_selection: "selected", registered: 0, suspended_at: null, removed_at: null }); |
| 17 | + }); |
| 18 | + |
| 19 | + it("'created' with a minimal installation stores null account/type/selection", async () => { |
| 20 | + const e = createTestEnv(); |
| 21 | + await upsertOrbInstallation(e, "installation", { action: "created", installation: { id: 300 } }); |
| 22 | + expect(await get(e, 300)).toMatchObject({ account_login: null, account_type: null, repository_selection: null, registered: 0 }); |
| 23 | + }); |
| 24 | + |
| 25 | + it("'suspend' then 'unsuspend' toggle suspended_at", async () => { |
| 26 | + const e = createTestEnv(); |
| 27 | + await upsertOrbInstallation(e, "installation", created(101)); |
| 28 | + await upsertOrbInstallation(e, "installation", { action: "suspend", installation: { id: 101 } }); |
| 29 | + expect((await get(e, 101))?.suspended_at).not.toBeNull(); |
| 30 | + await upsertOrbInstallation(e, "installation", { action: "unsuspend", installation: { id: 101 } }); |
| 31 | + expect((await get(e, 101))?.suspended_at).toBeNull(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("'deleted' sets removed_at; 'new_permissions_accepted' re-activates (clears removed_at)", async () => { |
| 35 | + const e = createTestEnv(); |
| 36 | + await upsertOrbInstallation(e, "installation", created(102)); |
| 37 | + await upsertOrbInstallation(e, "installation", { action: "deleted", installation: { id: 102 } }); |
| 38 | + expect((await get(e, 102))?.removed_at).not.toBeNull(); |
| 39 | + await upsertOrbInstallation(e, "installation", { action: "new_permissions_accepted", installation: { id: 102, account: { login: "acme", type: "Organization" }, repository_selection: "all" } }); |
| 40 | + const row = await get(e, 102); |
| 41 | + expect(row?.removed_at).toBeNull(); |
| 42 | + expect(row?.repository_selection).toBe("all"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("does nothing for a non-installation event, a missing installation id, or an unknown action", async () => { |
| 46 | + const e = createTestEnv(); |
| 47 | + await upsertOrbInstallation(e, "pull_request", created(200)); // wrong event |
| 48 | + await upsertOrbInstallation(e, "installation", { action: "created" }); // no installation object |
| 49 | + await upsertOrbInstallation(e, "installation", { action: "created", installation: { id: 0 } }); // falsy id |
| 50 | + expect(await get(e, 200)).toBeFalsy(); // never inserted |
| 51 | + const e2 = createTestEnv(); |
| 52 | + await upsertOrbInstallation(e2, "installation", created(201)); |
| 53 | + await upsertOrbInstallation(e2, "installation", { action: "labeled", installation: { id: 201 } }); // unknown action → no change |
| 54 | + expect((await get(e2, 201))?.removed_at).toBeNull(); |
| 55 | + }); |
| 56 | +}); |
0 commit comments