Path: blob/master/src/packages/backend/conat/test/sync/headers.test.ts
1451 views
/*1Test using user-defined headers with kv and stream.23DEVELOPMENT:45pnpm test ./headers.test.ts6*/78import { dstream, dkv } from "@cocalc/backend/conat/sync";9import { once } from "@cocalc/util/async-utils";10import { before, after, wait } from "@cocalc/backend/conat/test/setup";1112beforeAll(before);1314describe("test headers with a dstream", () => {15let s;16const name = `${Math.random()}`;17it("creates a dstream and writes a value without a header", async () => {18s = await dstream({ name });19expect(s.headers(s.length - 1)).toBe(undefined);20s.publish("x");21await once(s, "change");22const h = s.headers(s.length - 1);23for (const k in h ?? {}) {24if (!k.startsWith("CN-")) {25throw Error("system headers must start with CN-");26}27}28});2930it("writes a value with a header", async () => {31s.publish("y", { headers: { my: "header" } });32// header isn't visible until ack'd by server33// NOTE: not optimal but this is what is implemented and documented!34expect(s.headers(s.length - 1)).toEqual(undefined);35await wait({ until: () => s.headers(s.length - 1) != null });36expect(s.headers(s.length - 1)).toEqual(37expect.objectContaining({ my: "header" }),38);39});4041it("header still there", async () => {42await s.close();43s = await dstream({ name });44expect(s.headers(s.length - 1)).toEqual(45expect.objectContaining({ my: "header" }),46);47});4849it("clean up", async () => {50await s.delete({ all: true });51});52});5354describe("test headers with a dkv", () => {55let s;56const name = `${Math.random()}`;57it("creates a dkv and writes a value without a header", async () => {58s = await dkv({ name });59s.set("x", 10);60await once(s, "change");61const h = s.headers("x");62for (const k in h ?? {}) {63if (!k.startsWith("CN-")) {64throw Error("system headers must start with CN-");65}66}67});6869it("writes a value with a header - defined even before saving", async () => {70s.set("y", 20, { headers: { my: "header" } });71expect(s.headers("y")).toEqual(expect.objectContaining({ my: "header" }));72});7374it("clean up", async () => {75await s.clear();76});77});7879afterAll(after);808182