67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
|
|
import { applySparseFields, parsePublicApiListQuery, publicApiResources, serializePublicApiCursor } from "./resources.js";
|
|
|
|
describe("public API resources", () => {
|
|
test("parses bounded cursor list queries", () => {
|
|
const cursor = serializePublicApiCursor({ fieldValue: "2026-07-03T00:00:00.000Z", id: "row_1" });
|
|
const result = parsePublicApiListQuery(
|
|
publicApiResources.contacts,
|
|
new URLSearchParams({
|
|
limit: "25",
|
|
cursor,
|
|
sort: "updatedAt.asc",
|
|
fields: "displayName,email",
|
|
q: "Meyer",
|
|
updatedSince: "2026-07-01T00:00:00.000Z",
|
|
}),
|
|
);
|
|
|
|
expect(result).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
limit: 25,
|
|
cursor: { fieldValue: "2026-07-03T00:00:00.000Z", id: "row_1" },
|
|
sort: { field: "updatedAt", direction: "asc" },
|
|
fields: ["displayName", "email"],
|
|
filters: { q: "Meyer" },
|
|
},
|
|
});
|
|
});
|
|
|
|
test("rejects unsupported sparse fields and embeds", () => {
|
|
expect(parsePublicApiListQuery(publicApiResources.contacts, new URLSearchParams({ fields: "passwordHash" }))).toMatchObject({
|
|
ok: false,
|
|
code: "invalid_fields",
|
|
});
|
|
expect(parsePublicApiListQuery(publicApiResources.contacts, new URLSearchParams({ embed: "secrets" }))).toMatchObject({
|
|
ok: false,
|
|
code: "invalid_embed",
|
|
});
|
|
});
|
|
|
|
test("rejects cursor payloads with non-date sort values", () => {
|
|
const cursor = serializePublicApiCursor({ fieldValue: "not-a-date", id: "row_1" });
|
|
|
|
expect(parsePublicApiListQuery(publicApiResources.contacts, new URLSearchParams({ cursor }))).toMatchObject({
|
|
ok: false,
|
|
code: "invalid_cursor",
|
|
});
|
|
});
|
|
|
|
test("keeps id and requested embeds in sparse fieldsets", () => {
|
|
expect(
|
|
applySparseFields(
|
|
{ id: "deal_1", name: "Deal", email: "a@example.test", pipeline: { id: "pipeline_1" } },
|
|
["email"],
|
|
publicApiResources.deals.defaultFields,
|
|
["pipeline"],
|
|
),
|
|
).toEqual({
|
|
email: "a@example.test",
|
|
id: "deal_1",
|
|
pipeline: { id: "pipeline_1" },
|
|
});
|
|
});
|
|
});
|