Initial Commit

This commit is contained in:
2026-05-27 23:47:53 +01:00
parent fbc70f96e6
commit 9b28db724f
18103 changed files with 1600554 additions and 0 deletions

View File

@@ -0,0 +1,143 @@
/**
* Boundary schema tests — confirm every schema rejects malformed
* input and accepts the canonical shape.
*/
import { describe, expect, it } from "@suaveplan/testing/runner";
import {
HttpsTransportOptionsSchema,
RecvResponseSchema,
SendBodySchema,
SendResponseSchema,
} from "../src/index.js";
import { makeEnvelope } from "./_helpers.js";
describe("HttpsTransportOptionsSchema", () => {
it("accepts a minimal valid option bag", { timeout: 5000 }, () => {
const r = HttpsTransportOptionsSchema.safeParse({
relayUrl: "https://r.test",
ourPeerId: "peer-a",
});
expect(r.success).toBe(true);
});
it("rejects relative URL", { timeout: 5000 }, () => {
const r = HttpsTransportOptionsSchema.safeParse({
relayUrl: "/relative",
ourPeerId: "peer-a",
});
expect(r.success).toBe(false);
});
it("rejects empty ourPeerId", { timeout: 5000 }, () => {
const r = HttpsTransportOptionsSchema.safeParse({
relayUrl: "https://r.test",
ourPeerId: "",
});
expect(r.success).toBe(false);
});
it("rejects backoffBaseMs > backoffMaxMs", { timeout: 5000 }, () => {
const r = HttpsTransportOptionsSchema.safeParse({
relayUrl: "https://r.test",
ourPeerId: "peer-a",
backoffBaseMs: 1000,
backoffMaxMs: 100,
});
expect(r.success).toBe(false);
});
it("rejects non-positive longPollTimeoutMs", { timeout: 5000 }, () => {
const r = HttpsTransportOptionsSchema.safeParse({
relayUrl: "https://r.test",
ourPeerId: "peer-a",
longPollTimeoutMs: 0,
});
expect(r.success).toBe(false);
});
it("accepts only one backoff bound", { timeout: 5000 }, () => {
const a = HttpsTransportOptionsSchema.safeParse({
relayUrl: "https://r.test",
ourPeerId: "peer-a",
backoffBaseMs: 100,
});
expect(a.success).toBe(true);
const b = HttpsTransportOptionsSchema.safeParse({
relayUrl: "https://r.test",
ourPeerId: "peer-a",
backoffMaxMs: 100,
});
expect(b.success).toBe(true);
});
});
describe("SendBodySchema", () => {
it("accepts a canonical body", { timeout: 5000 }, () => {
const r = SendBodySchema.safeParse({
to: "peer-b",
envelope: makeEnvelope(),
});
expect(r.success).toBe(true);
});
it("rejects non-string envelope id", { timeout: 5000 }, () => {
const r = SendBodySchema.safeParse({
to: "peer-b",
envelope: { ...makeEnvelope(), id: 42 },
});
expect(r.success).toBe(false);
});
it("accepts broadcast targetId", { timeout: 5000 }, () => {
const r = SendBodySchema.safeParse({
to: "peer-b",
envelope: { ...makeEnvelope(), targetId: "*" },
});
expect(r.success).toBe(true);
});
});
describe("SendResponseSchema", () => {
it("accepts { ok: true }", { timeout: 5000 }, () => {
expect(SendResponseSchema.safeParse({ ok: true }).success).toBe(true);
});
it("accepts { ok: false, code: 'POLICY' }", { timeout: 5000 }, () => {
expect(
SendResponseSchema.safeParse({ ok: false, code: "POLICY" }).success
).toBe(true);
});
it("rejects missing ok", { timeout: 5000 }, () => {
expect(SendResponseSchema.safeParse({ code: "X" }).success).toBe(false);
});
});
describe("RecvResponseSchema", () => {
it("accepts { envelopes: [...], nextCursor: 'x' }", {
timeout: 5000,
}, () => {
expect(
RecvResponseSchema.safeParse({
envelopes: [makeEnvelope()],
nextCursor: "x",
}).success
).toBe(true);
});
it("rejects missing nextCursor", { timeout: 5000 }, () => {
expect(RecvResponseSchema.safeParse({ envelopes: [] }).success).toBe(
false
);
});
it("rejects an envelope missing required fields", { timeout: 5000 }, () => {
expect(
RecvResponseSchema.safeParse({
envelopes: [{ id: "x" }],
nextCursor: "n",
}).success
).toBe(false);
});
});