Files
nocobase-mcp/test/helpers.test.ts
T

62 lines
2.1 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import {
fieldsWithProjections,
normalizePagination,
prune,
relationRefs,
toZonedIso,
} from "../src/crm-helpers.js";
import {
buildAndFilter,
buildDateRangeCondition,
buildRelationIdCondition,
buildRelationIdInCondition,
buildSearchFilter,
} from "../src/filters.js";
test("builds nested NocoBase filters", () => {
assert.deepEqual(buildRelationIdCondition("owner", 7), { owner: { id: { $eq: 7 } } });
assert.deepEqual(buildRelationIdCondition(["customer", "owner", "id"], 9), {
customer: { owner: { id: { $eq: 9 } } },
});
assert.deepEqual(buildRelationIdInCondition("owner", [1, 2]), { owner: { id: { $in: [1, 2] } } });
assert.deepEqual(buildDateRangeCondition("createdAt", "2026-01-01", "2026-01-31"), {
createdAt: { $dateBetween: ["2026-01-01", "2026-01-31"] },
});
assert.deepEqual(buildSearchFilter("Acme", ["name", "code"]), {
$or: [{ name: { $includes: "Acme" } }, { code: { $includes: "Acme" } }],
});
assert.deepEqual(buildAndFilter([undefined]), {});
});
test("normalizes pagination and relation projections", () => {
assert.deepEqual(normalizePagination(500, -3), [100, 0]);
assert.deepEqual(fieldsWithProjections(["id", "owner"], ["owner.id", "owner.nickname"]), [
"id",
"owner.id",
"owner.nickname",
]);
});
test("renders relation references without treating an id as a real name", () => {
const refs = relationRefs(
{ customers: [{ id: 42 }, { id: 43, title: "Acme" }, { id: 43, title: "duplicate" }] },
["customers"],
{ idField: "id", nameFields: ["title"] },
);
assert.deepEqual(refs, [
{ id: 42, name: "#42" },
{ id: 43, name: "Acme" },
]);
assert.deepEqual(prune({ id: 1, name: "", rows: [], value: null, active: false }), {
id: 1,
active: false,
});
});
test("formats timestamps in the configured timezone while preserving dates", () => {
assert.equal(toZonedIso("2026-07-08T09:00:00Z", "Asia/Shanghai"), "2026-07-08T17:00:00+08:00");
assert.equal(toZonedIso("2026-07-08", "Asia/Shanghai"), "2026-07-08");
});