import assert from "node:assert/strict"; import test from "node:test"; import { readFileSync } from "node:fs"; const actionRoute = readFileSync( new URL("../app/api/action/route.ts", import.meta.url), "utf8", ); const mvpDb = readFileSync( new URL("../lib/mvp-db.ts", import.meta.url), "utf8", ); const adminApp = readFileSync( new URL("../app/admin-app.tsx", import.meta.url), "utf8", ); const migration = readFileSync( new URL("../mysql/0018_partner_resource_binding.sql", import.meta.url), "utf8", ); function section(source, action) { const start = source.indexOf(`"${action}"`); assert.ok(start >= 0, `action ${action} 不存在于路由源码`); const next = source.indexOf("} else if (body.action ===", start); return source.slice(start, next === -1 ? undefined : next); } test("partner_bind_account enforces one partner per resource account", () => { const body = section(actionRoute, "partner_bind_account"); assert.match( body, /SELECT id, name FROM partners WHERE account_id = \? AND id != \? LIMIT 1/, "绑定前必须检查资源账号是否已被其它合作方占用", ); assert.match(body, /该 KOC 资源已绑定合作方/, "占用时返回可读错误"); assert.match( body, /UPDATE partners SET account_id = '' WHERE id = \?/, "空 accountId 表示解绑", ); assert.match(body, /群主合作方不绑定 KOC 资源/); }); test("partner_create_from_account creates external_koc bound to account", () => { const body = section(actionRoute, "partner_create_from_account"); assert.match( body, /SELECT id, name FROM partners WHERE account_id = \? LIMIT 1/, "一键创建前必须确认资源未被绑定", ); assert.match( body, /partner-koc-/, "合作方 id 使用 partner-koc- 前缀,避免与领取流程冲突", ); assert.match(body, /'external_koc'/); assert.match( body, /INSERT INTO partners[\s\S]*account_id[\s\S]*VALUES/, "INSERT 必须写入 account_id", ); }); test("dashboard partners query joins accounts for contact display", () => { assert.match( mvpDb, /FROM partners p\s+LEFT JOIN accounts a ON a\.id = p\.account_id/, "partners 列表需带出资源昵称与联系方式", ); assert.match(mvpDb, /a\.current_contact AS account_contact/); assert.match( mvpDb, /ensureColumn\(\s*"partners",\s*"account_id"/, "自托管路径也要补 account_id 列", ); }); test("migration 0018 adds partners.account_id with index", () => { assert.match(migration, /ADD COLUMN `account_id`/); assert.match(migration, /partners_account_idx/); }); test("admin app renders resource binding affordances", () => { assert.match(adminApp, /partner_bind_account/); assert.match(adminApp, /partner_create_from_account/); assert.match(adminApp, /绑定资源/, "未绑合作方有绑定入口"); assert.match(adminApp, /换绑/, "已绑合作方可换绑"); assert.match(adminApp, /解绑中…/, "已绑合作方可解绑"); assert.match(adminApp, /设为合作方/, "资源页支持一键创建合作方"); });