47 lines
1.9 KiB
TypeScript
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"\)/);
|
|
});
|