-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathslack-context.test.ts
More file actions
78 lines (67 loc) · 2.21 KB
/
slack-context.test.ts
File metadata and controls
78 lines (67 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { describe, expect, test } from "bun:test";
import { type SlackContext, slackContextStore } from "../slack-context.ts";
const SAMPLE: SlackContext = {
slackChannelId: "C123",
slackThreadTs: "1700000000.000100",
slackMessageTs: "1700000000.000200",
};
describe("slackContextStore", () => {
test("getStore() is undefined outside a run()", () => {
expect(slackContextStore.getStore()).toBeUndefined();
});
test("synchronous read inside run() sees the context", () => {
const seen = slackContextStore.run(SAMPLE, () => slackContextStore.getStore());
expect(seen).toEqual(SAMPLE);
});
test("context propagates across a plain await boundary", async () => {
const seen = await slackContextStore.run(SAMPLE, async () => {
await Promise.resolve();
return slackContextStore.getStore();
});
expect(seen).toEqual(SAMPLE);
});
test("context propagates across a setImmediate hop", async () => {
const seen = await slackContextStore.run(SAMPLE, async () => {
await new Promise<void>((resolve) => setImmediate(resolve));
return slackContextStore.getStore();
});
expect(seen).toEqual(SAMPLE);
});
test("context propagates through an async generator for-await loop", async () => {
async function* producer(): AsyncGenerator<number> {
for (let i = 0; i < 3; i++) {
await Promise.resolve();
yield i;
}
}
const observations: (SlackContext | undefined)[] = [];
await slackContextStore.run(SAMPLE, async () => {
for await (const _ of producer()) {
observations.push(slackContextStore.getStore());
}
});
expect(observations.length).toBe(3);
for (const seen of observations) {
expect(seen).toEqual(SAMPLE);
}
});
test("concurrent run() calls keep contexts isolated", async () => {
const other: SlackContext = {
slackChannelId: "C999",
slackThreadTs: "2700000000.000100",
slackMessageTs: "2700000000.000200",
};
const [a, b] = await Promise.all([
slackContextStore.run(SAMPLE, async () => {
await Promise.resolve();
return slackContextStore.getStore();
}),
slackContextStore.run(other, async () => {
await Promise.resolve();
return slackContextStore.getStore();
}),
]);
expect(a).toEqual(SAMPLE);
expect(b).toEqual(other);
});
});