import assert from 'node:assert/strict'; import { after, before, beforeEach, test } from 'node:test'; import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { emptyFields, type Demand } from '../lib/demand.ts'; import { canTakeOverDevelopment } from '../lib/server/default-developer.ts'; import { applyReviewAction } from '../lib/server/demand-review.ts'; import { applyWorkflowAction } from '../lib/server/workflow.ts'; import { planConfirmed } from '../lib/workflow.ts'; import { closeDatabases, getDatabase, type AccountRow } from '../lib/server/database.ts'; import type { AuthUser } from '../lib/server/auth.ts'; const directory = mkdtempSync(join(tmpdir(), 'demand-takeover-test-')); const environment = { ...process.env }; let original: AuthUser; const admin: AuthUser = { userId: 'replacement', username: 'replacement_admin', displayName: '接手管理员', role: 'admin', mustChangePassword: false }; const business: AuthUser = { userId: 'business', username: 'business', displayName: '业务', role: 'user', mustChangePassword: false }; const now = '2026-09-23T08:00:00Z'; before(() => { Object.assign(process.env, { DATA_DIR: directory, APP_URL: 'https://test.example', INITIAL_ADMIN_USERNAME: 'original_admin', INITIAL_ADMIN_PASSWORD: 'Local test password 9274' }); const row = getDatabase().prepare('SELECT * FROM accounts').first()!; original = { userId: row.id, username: row.username, displayName: row.display_name, role: 'admin', mustChangePassword: false }; getDatabase().prepare(`INSERT INTO accounts (id, username, display_name, password_hash, role, must_change_password, created_at, updated_at) VALUES (?, ?, ?, ?, 'admin', 0, ?, ?)`).bind(admin.userId, admin.username, admin.displayName, row.password_hash, now, now).run(); }); beforeEach(() => { getDatabase().prepare('UPDATE accounts SET disabled = 0, must_change_password = 0').run(); getDatabase().prepare('UPDATE accounts SET disabled = 1 WHERE id = ?').bind(original.userId).run(); }); after(() => { closeDatabases(); rmSync(directory, { recursive: true, force: true }); process.env = environment; }); function fixture(status = '待验收'): Demand { const testing = { round: 1, testVersion: 'build-1', environment: 'https://test.example', deadline: '2026-10-01', items: [], instructions: '说明' }; return { id: 'handoff', revision: 4, fields: { ...emptyFields }, status, reviewReady: true, createdAt: now, updatedAt: now, history: [], changes: [], review: { developer: original, proposals: [{ version: 1, scope: '原方案', outOfScope: '无', developer: original, submittedAt: now, businessFields: { ...emptyFields }, decision: { result: 'confirmed', actor: business, time: now, comment: '确认原方案' } }] }, plan: { developer: original.displayName, testDate: '2026-10-01', releaseDate: '2026-10-03', version: 1, proposalVersion: 1, proposedBy: original, proposedAt: now, decision: { actor: business, time: now, approved: true, comment: '确认原计划' } }, acceptance: structuredClone(testing), delivery: { testing: [testing] }, }; } const takeOver = (d: Demand, user = admin, confirm = true) => applyReviewAction(d, business.userId, user, { action: 'take-over-development', confirm }, now); test('only initialized enabled administrators can recover an unavailable developer', () => { const d = fixture(); assert.equal(canTakeOverDevelopment(d, admin), true); assert.equal(canTakeOverDevelopment(d, business), false); assert.equal(canTakeOverDevelopment(d, original), false); assert.throws(() => takeOver(d, business), /仅启用的管理员/); for (const status of ['草稿', '已取消']) assert.throws(() => takeOver(fixture(status)), /仅启用的管理员/); for (const field of ['disabled', 'must_change_password']) { getDatabase().prepare(`UPDATE accounts SET ${field} = 1 WHERE id = ?`).bind(admin.userId).run(); assert.throws(() => takeOver(d), /仅启用的管理员/); getDatabase().prepare(`UPDATE accounts SET ${field} = 0 WHERE id = ?`).bind(admin.userId).run(); } }); test('handoff preserves evidence and invalidates old approvals and tests across formal states', () => { for (const status of ['待评审', '已排期', '开发中', '待验收', '待发布', '已上线', '已完成']) { const d = fixture(status); const oldPlan = structuredClone(d.plan), oldDecision = structuredClone(d.review!.proposals[0].decision); assert.equal(planConfirmed(d), true); const event = takeOver(d); assert.equal(d.status, '待评审'); assert.equal(d.review!.developer.userId, admin.userId); assert.equal(d.review!.proposals[0].developer.userId, original.userId); assert.deepEqual(d.review!.proposals[0].decision, oldDecision); assert.equal(d.review!.proposals[0].invalidated?.time, now); assert.deepEqual(d.plan, oldPlan); assert.equal(planConfirmed(d), false); assert.equal(d.acceptance?.withdrawnAt, now); assert.equal(d.delivery?.testing?.[0].withdrawnAt, now); assert.equal(event.label, '接手开发职责'); assert(event.detail.includes(original.username) && event.detail.includes(admin.username)); assert.throws(() => applyWorkflowAction(d, business.userId, original, { action: 'add-note', notes: '旧开发无权限' }, now), /对应责任人/); } }); test('stale availability, missing confirmation and pending changes cannot mutate the record', () => { const d = fixture(); let before = JSON.stringify(d); assert.throws(() => takeOver(d, admin, false), /请确认/); assert.equal(JSON.stringify(d), before); d.changes.push({ id: 'pending', time: now, content: '新范围', reason: '业务调整', actor: '业务', status: '待开发评估', originalVersion: 'v1.0' }); before = JSON.stringify(d); assert.throws(() => takeOver(d), /处理或撤回/); assert.equal(JSON.stringify(d), before); d.changes[0].status = '已撤回'; getDatabase().prepare('UPDATE accounts SET disabled = 0 WHERE id = ?').bind(original.userId).run(); before = JSON.stringify(d); assert.throws(() => takeOver(d), /仅启用的管理员/); assert.equal(JSON.stringify(d), before); }); test('incomplete material can be handed over but still requires business completion', () => { const d = fixture('待评审'); d.reviewReady = false; takeOver(d); assert.equal(d.reviewReady, false); assert.throws(() => applyReviewAction(d, business.userId, admin, { action: 'propose-solution', scope: '新方案', outOfScope: '无' }, now), /补齐业务材料/); }); test('replacement submits a new version and business must confirm it again', () => { const d = fixture(); takeOver(d); applyReviewAction(d, business.userId, admin, { action: 'propose-solution', scope: '接手后的方案', outOfScope: '无' }, now); assert.equal(d.review!.proposals.at(-1)?.version, 2); assert.throws(() => applyReviewAction(d, business.userId, admin, { action: 'confirm-solution', proposalVersion: 2, confirm: true }, now), /登记账号/); applyReviewAction(d, business.userId, business, { action: 'confirm-solution', proposalVersion: 2, confirm: true }, now); assert.equal(d.review!.proposals.at(-1)?.decision?.actor.userId, business.userId); assert.equal(planConfirmed(d), false); });