Files
poli0981andClaude Opus 5 1b6ad7be47 feat(admin): read-only decided and published rows, reopen, reconcile sweep, retention
The server half of the admin rework. The pages still run on it unchanged -
every change here is additive - until the new UI replaces them.

Read-only rows. web/shared/queue-rules.ts holds the queue's rules once, for the
Worker and the UI: a row is locked when it is approved (its request is already
committed to Git), committed (the game was observed in data/) or rejected, and
also when its GAME is already published, since one appid can have several
rows. /api/admin/decide refuses locked rows when it reads them and again in
every UPDATE's WHERE, and reports each refused id with a reason - not-found,
not-decidable, published, open-row-exists, duplicate-in-request, no-op,
changed-concurrently. It used to drop them silently, so a mixed selection
reported fewer decisions than it was sent. The queue listing carries
`published` per row, and status=all searches every tab when there is a query.

Bugs fixed on the way:
- Requeueing or deferring a failed row while another row for the appid was
  open broke uq_queue_open_appid inside the batch: a 500 for the whole request.
  It is now a skip.
- When an approval's commit landed and the queue update failed, the job's
  status was in the same rolled-back batch and stayed 'pending' forever, and
  the message promised reconcile would promote rows it never reads. The job is
  recorded first, and the message says what is actually true.
- The health endpoint returned 503 without a body the page could render.

New: POST /api/admin/reopen returns one rejected row to pending and deletes
only its 'rejected' decision, in one transaction whose guards refuse a
published game or an appid with another open row - replacing a raw-SQL step in
the runbook. Jobs and audit page with totals and filters. Approval reasons are
recorded in the audit row and the commit body.

Reconcile sweeps pending, deferred and failed rows whose game is already in
data/ to committed (a game published after its approval aged out stayed
'failed' forever), holds a cross-isolate D1 lease, and skips the ~6 MB dataset
fetch while index.json's generation is unchanged. Migration 0002 adds the two
tables; lib/locks.ts treats a missing table as unavailable, so the Worker and
the migration can ship in either order.

Overrides: the Worker can now delete a SETTLED override file - no active field,
every retirement already restored - through createCommitOnBranch deletions,
and every override commit writes a commit_jobs row.

audit_log and commit_jobs are pruned daily after ADMIN_RETENTION_DAYS (180),
which the privacy policy now states.

Tests: the real handlers against the real migrations on node:sqlite
(worker/testing/sqlite-d1.ts), with GitHub and the dataset faked - 57 new
cases across decide, reopen, reconcile, leases, retention, override deletion
and the createCommitOnBranch payload.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-17 22:03:39 +07:00

80 lines
2.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import {
allowedActions,
DECIDABLE,
isSelectable,
lockReason,
OPEN_STATUSES,
QUEUE_STATUSES,
sqlList,
} from "./queue-rules";
describe("lockReason", () => {
it("locks every decided status, whatever else is true", () => {
for (const status of ["approved", "committed", "rejected"]) {
expect(lockReason({ status })).toBe("decided");
expect(lockReason({ status, published: true })).toBe("decided");
expect(isSelectable({ status })).toBe(false);
}
});
it("locks an undecided row whose game is already published", () => {
for (const status of DECIDABLE) {
expect(lockReason({ status, published: true })).toBe("published");
expect(lockReason({ status, published: 1 })).toBe("published");
expect(isSelectable({ status, published: true })).toBe(false);
}
});
it("leaves an undecided, unpublished row selectable", () => {
for (const status of DECIDABLE) {
expect(lockReason({ status, published: false })).toBeNull();
expect(lockReason({ status, published: 0 })).toBeNull();
expect(isSelectable({ status })).toBe(true);
}
});
});
describe("allowedActions", () => {
it("offers no action on a locked row", () => {
expect(allowedActions({ status: "committed" })).toEqual([]);
expect(allowedActions({ status: "pending", published: true })).toEqual([]);
});
it("never offers a move to where the row already is", () => {
expect(allowedActions({ status: "pending" })).toEqual(["approve", "reject", "defer"]);
expect(allowedActions({ status: "deferred" })).toEqual(["approve", "reject", "requeue"]);
expect(allowedActions({ status: "failed" })).toEqual(["approve", "reject", "defer", "requeue"]);
});
});
describe("sqlList", () => {
it("quotes closed word lists", () => {
expect(sqlList(DECIDABLE)).toBe("'pending','deferred','failed'");
});
it("refuses anything that is not a lowercase word", () => {
expect(() => sqlList(["pending'); DROP TABLE x; --"])).toThrow();
expect(() => sqlList(["Pending"])).toThrow();
});
});
describe("the constants match the schema", () => {
const sql = readFileSync(join(process.cwd(), "worker", "migrations", "0001_init.sql"), "utf-8");
const words = (list: string) => [...list.matchAll(/'([a-z]+)'/g)].map((m) => m[1]);
it("QUEUE_STATUSES is the status CHECK constraint", () => {
const check = /status\s+TEXT[\s\S]*?CHECK \(status IN \(([^)]+)\)\)/.exec(sql);
expect(check).not.toBeNull();
expect(words(check![1])).toEqual([...QUEUE_STATUSES]);
});
it("OPEN_STATUSES is the uq_queue_open_appid predicate", () => {
const index = /uq_queue_open_appid[\s\S]*?WHERE status IN \(([^)]+)\)/.exec(sql);
expect(index).not.toBeNull();
expect(words(index![1])).toEqual([...OPEN_STATUSES]);
});
});