Files
Jyotisha/frontend/tests/local-postgres-order.test.ts
T
jesse-ux e60a3a4d14
Independent Staging Quality Gate / validate (push) Failing after 9m55s
Independent Staging Quality Gate / publish (push) Skipped
fix(db): 会话列表按 updated_at 再按 id 排
兼容层 order() 改为追加,不再只留最后一键。
GET /api/sessions 与套餐列表的两次 order() 都会进 SQL。
2026-09-17 20:12:22 +08:00

47 lines
1.9 KiB
TypeScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
import { formatOrderClause } from "../src/lib/db/local-postgres-client-core.ts";
const listRoute = readFileSync(new URL("../src/app/api/sessions/route.ts", import.meta.url), "utf8");
const cursor = readFileSync(new URL("../src/lib/session-cursor.ts", import.meta.url), "utf8");
const packagesRoute = readFileSync(new URL("../src/app/api/payment/packages/route.ts", import.meta.url), "utf8");
const core = readFileSync(new URL("../src/lib/db/local-postgres-client-core.ts", import.meta.url), "utf8");
test("a single order() still emits one key", () => {
assert.equal(
formatOrderClause([{ column: "id", ascending: false }]),
' order by "id" desc',
);
assert.equal(
formatOrderClause([{ column: "sort_order", ascending: true }]),
' order by "sort_order" asc',
);
});
test("two order() calls keep both keys in call order", () => {
assert.equal(
formatOrderClause([
{ column: "updated_at", ascending: false },
{ column: "id", ascending: false },
]),
' order by "updated_at" desc, "id" desc',
);
assert.equal(
formatOrderClause([
{ column: "sort_order", ascending: true },
{ column: "created_at", ascending: true },
]),
' order by "sort_order" asc, "created_at" asc',
);
assert.match(core, /this\.ordering\.push\(\{ column, ascending: options\.ascending !== false \}\)/);
assert.match(core, /sql \+= formatOrderClause\(this\.ordering\);/);
});
test("session list sort keys match the cursor filter keys", () => {
assert.match(listRoute, /\.order\("updated_at", \{ ascending: false \}\)\s*\.order\("id", \{ ascending: false \}\)/);
assert.match(cursor, /updated_at\.lt\.\$\{updatedAt\},and\(updated_at\.eq\.\$\{updatedAt\},id\.lt\.\$\{id\}\)/);
assert.match(packagesRoute, /\.order\("sort_order"\)\s*\.order\("created_at"\)/);
});