Files
suaveplan/packages/foundation/data/pwa/src/schemas/schemas.test.ts
2026-05-27 23:47:53 +01:00

94 lines
2.7 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import { SYNC_QUEUE_SCHEMA_VERSION } from "../constants/index";
import {
JsonValueSchema,
SyncQueueEntrySchema,
WebAppManifestSchema,
} from "./schemas";
describe("SyncQueueEntrySchema", () => {
const valid = {
id: "sq_1",
url: "/api/v1/orders",
method: "POST",
createdAt: 1_700_000_000_000,
retryCount: 0,
maxRetries: 3,
schemaVersion: SYNC_QUEUE_SCHEMA_VERSION,
};
it("accepts a fully-populated entry", { timeout: 5000 }, () => {
const parsed = SyncQueueEntrySchema.parse({
...valid,
headers: { "x-trace": "abc" },
body: '{"a":1}',
metadata: { tenant: "acme", attempts: 2, flags: [true, "x"] },
});
expect(parsed.id).toBe("sq_1");
expect(parsed.metadata?.tenant).toBe("acme");
});
it("rejects entries with the wrong schemaVersion", {
timeout: 5000,
}, () => {
expect(() =>
SyncQueueEntrySchema.parse({ ...valid, schemaVersion: 1 })
).toThrow();
});
it("rejects negative retryCount", { timeout: 5000 }, () => {
expect(() =>
SyncQueueEntrySchema.parse({ ...valid, retryCount: -1 })
).toThrow();
});
it("rejects metadata containing non-JSON values", { timeout: 5000 }, () => {
expect(() =>
SyncQueueEntrySchema.parse({
...valid,
metadata: { fn: () => 1 },
})
).toThrow();
});
});
describe("JsonValueSchema", () => {
it("accepts nested JSON values", { timeout: 5000 }, () => {
expect(() =>
JsonValueSchema.parse({
a: [1, "x", true, null, { b: [false, "y"] }],
})
).not.toThrow();
});
it("rejects functions", { timeout: 5000 }, () => {
expect(() => JsonValueSchema.parse(() => 1)).toThrow();
});
});
describe("WebAppManifestSchema", () => {
it("accepts a minimal valid manifest", { timeout: 5000 }, () => {
const parsed = WebAppManifestSchema.parse({
name: "Suaveplan",
icons: [{ src: "/icon.png", sizes: "512x512" }],
});
expect(parsed.name).toBe("Suaveplan");
});
it("rejects manifests without icons", { timeout: 5000 }, () => {
expect(() =>
WebAppManifestSchema.parse({ name: "X", icons: [] })
).toThrow();
});
it("rejects manifests with invalid display", { timeout: 5000 }, () => {
expect(() =>
WebAppManifestSchema.parse({
name: "X",
icons: [{ src: "/i.png", sizes: "1x1" }],
display: "bogus",
})
).toThrow();
});
});