Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/backend/conat/test/sync/headers.test.ts
1451 views
1
/*
2
Test using user-defined headers with kv and stream.
3
4
DEVELOPMENT:
5
6
pnpm test ./headers.test.ts
7
*/
8
9
import { dstream, dkv } from "@cocalc/backend/conat/sync";
10
import { once } from "@cocalc/util/async-utils";
11
import { before, after, wait } from "@cocalc/backend/conat/test/setup";
12
13
beforeAll(before);
14
15
describe("test headers with a dstream", () => {
16
let s;
17
const name = `${Math.random()}`;
18
it("creates a dstream and writes a value without a header", async () => {
19
s = await dstream({ name });
20
expect(s.headers(s.length - 1)).toBe(undefined);
21
s.publish("x");
22
await once(s, "change");
23
const h = s.headers(s.length - 1);
24
for (const k in h ?? {}) {
25
if (!k.startsWith("CN-")) {
26
throw Error("system headers must start with CN-");
27
}
28
}
29
});
30
31
it("writes a value with a header", async () => {
32
s.publish("y", { headers: { my: "header" } });
33
// header isn't visible until ack'd by server
34
// NOTE: not optimal but this is what is implemented and documented!
35
expect(s.headers(s.length - 1)).toEqual(undefined);
36
await wait({ until: () => s.headers(s.length - 1) != null });
37
expect(s.headers(s.length - 1)).toEqual(
38
expect.objectContaining({ my: "header" }),
39
);
40
});
41
42
it("header still there", async () => {
43
await s.close();
44
s = await dstream({ name });
45
expect(s.headers(s.length - 1)).toEqual(
46
expect.objectContaining({ my: "header" }),
47
);
48
});
49
50
it("clean up", async () => {
51
await s.delete({ all: true });
52
});
53
});
54
55
describe("test headers with a dkv", () => {
56
let s;
57
const name = `${Math.random()}`;
58
it("creates a dkv and writes a value without a header", async () => {
59
s = await dkv({ name });
60
s.set("x", 10);
61
await once(s, "change");
62
const h = s.headers("x");
63
for (const k in h ?? {}) {
64
if (!k.startsWith("CN-")) {
65
throw Error("system headers must start with CN-");
66
}
67
}
68
});
69
70
it("writes a value with a header - defined even before saving", async () => {
71
s.set("y", 20, { headers: { my: "header" } });
72
expect(s.headers("y")).toEqual(expect.objectContaining({ my: "header" }));
73
});
74
75
it("clean up", async () => {
76
await s.clear();
77
});
78
});
79
80
afterAll(after);
81
82