import "./setup_env"; import { afterEach, expect, test } from "bun:test"; import { claimAnyEvalForNode } from "../src/db"; import { db, features, security, tasks } from "../src/db_client"; // 单测不开数据库:把 db.select / db.transaction 换成按表回固定行的假实现,记下查过、改过哪些表。 const original = { select: db.select, transaction: db.transaction }; afterEach(() => Object.assign(db, original)); function fakeDb(rows: Map) { const selected: unknown[] = []; const updated: unknown[] = []; const select = () => ({ from(table: unknown) { selected.push(table); const result = Promise.resolve(rows.get(table) ?? []); const q = { where: () => q, orderBy: () => q, limit: () => q, for: () => result, then: (res: (v: unknown[]) => unknown, rej: (e: unknown) => unknown) => result.then(res, rej), }; return q; }, }); const tx = { select, update: (table: unknown) => ({ set: () => ({ where: async () => { updated.push(table); } }) }), }; Object.assign(db, { select, transaction: async (fn: (t: typeof tx) => unknown) => fn(tx) }); return { selected, updated }; } const secRow = { id: "sec-1", createdAt: 1, watchers: "[]", reviewHistory: "[]", clusters: "[]", ttkbPackages: "[]" }; const bugRow = { id: "bug-1", createdAt: 2 }; test("没带 skipSecurity 的认领照旧:最早的 security 单会被领走", async () => { const { selected, updated } = fakeDb(new Map([[security, [secRow]]])); const picked = await claimAnyEvalForNode("node-a", "android", [], [], []); expect(picked?.kind).toBe("security"); expect(picked?.kind === "security" && picked.security.executorNodeId).toBe("node-a"); expect(selected).toContain(security); expect(updated).toEqual([security]); }); test("skipSecurity 时不查也不绑 security 行:只有 security 时领不到单,有 bug 时领 bug", async () => { let fake = fakeDb(new Map([[security, [secRow]]])); expect(await claimAnyEvalForNode("node-a", "android", [], [], [], undefined, true)).toBeNull(); expect(fake.selected).not.toContain(security); expect(fake.updated).toEqual([]); fake = fakeDb(new Map([[security, [secRow]], [tasks, [bugRow]]])); const picked = await claimAnyEvalForNode("node-a", "android", [], [], [], undefined, true); expect(picked?.kind === "bug" && picked.task.id).toBe("bug-1"); expect(fake.selected).toEqual(expect.arrayContaining([tasks, features])); expect(fake.selected).not.toContain(security); expect(fake.updated).toEqual([tasks]); });