const { test } = require("node:test"); const assert = require("node:assert"); const { getSiteItems, createSiteItem, updateSiteItem, toHashPayload } = require("../src/lib/pleasanterClient"); test("toHashPayload nests flat Class*/Date*/Description* keys under their Hash, leaves others as-is", () => { const payload = toHashPayload({ ClassA: "key1", Date001: "2026-08-24T00:00:00.000Z", Description001: "enc" }); assert.deepStrictEqual(payload, { ClassHash: { ClassA: "key1" }, DateHash: { Date001: "2026-08-24T00:00:00.000Z" }, DescriptionHash: { Description001: "enc" }, }); }); test("getSiteItems pages through results using top-level Offset and flattens Hash keys", async () => { let call = 0; const fetchImpl = async (url, opts) => { const body = JSON.parse(opts.body); call++; if (body.Offset === 0) { return { ok: true, json: async () => ({ StatusCode: 200, Response: { Data: [{ ResultId: 1, ClassHash: { ClassA: "a" } }], TotalCount: 2 } }) }; } return { ok: true, json: async () => ({ StatusCode: 200, Response: { Data: [{ ResultId: 2, ClassHash: { ClassA: "b" } }], TotalCount: 2 } }) }; }; const items = await getSiteItems({ baseUrl: "https://example.test/", apiKey: "k", siteId: 1, fetchImpl }); assert.strictEqual(call, 2); assert.deepStrictEqual(items.map((i) => i.ClassA), ["a", "b"]); }); test("createSiteItem sends fields nested under Hash keys and returns the new item Id", async () => { let capturedBody; const fetchImpl = async (url, opts) => { capturedBody = JSON.parse(opts.body); return { ok: true, json: async () => ({ Id: 42, StatusCode: 200 }) }; }; const id = await createSiteItem({ baseUrl: "https://example.test/", apiKey: "k", siteId: 1, fields: { ClassA: "key1" }, fetchImpl }); assert.strictEqual(id, 42); assert.deepStrictEqual(capturedBody.ClassHash, { ClassA: "key1" }); }); test("updateSiteItem posts Hash-nested fields to /api/items/{itemId}/update without siteId in the path", async () => { let capturedUrl, capturedBody; const fetchImpl = async (url, opts) => { capturedUrl = url; capturedBody = JSON.parse(opts.body); return { ok: true, json: async () => ({ Id: 7, StatusCode: 200 }) }; }; await updateSiteItem({ baseUrl: "https://example.test/", apiKey: "k", itemId: 7, fields: { Date001: "2026-08-24T00:00:00.000Z" }, fetchImpl }); assert.strictEqual(capturedUrl, "https://example.test/api/items/7/update"); assert.deepStrictEqual(capturedBody.DateHash, { Date001: "2026-08-24T00:00:00.000Z" }); }); test("throws when StatusCode is not 200", async () => { const fetchImpl = async () => ({ ok: true, json: async () => ({ StatusCode: 400, Message: "bad" }) }); await assert.rejects(() => getSiteItems({ baseUrl: "https://example.test/", apiKey: "k", siteId: 1, fetchImpl })); });