@niwa-labs/requests 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -2
- package/package.json +41 -4
- package/src/access-ask.ts +97 -0
- package/src/actions.ts +216 -0
- package/src/cockpit/actions.ts +68 -0
- package/src/cockpit/answer-form.tsx +103 -0
- package/src/cockpit/answers.ts +71 -0
- package/src/cockpit/history.tsx +149 -0
- package/src/cockpit/index.tsx +28 -0
- package/src/cockpit/page.tsx +115 -0
- package/src/entity.ts +58 -0
- package/src/index.ts +619 -0
- package/src/question.ts +50 -0
- package/src/records-entry.ts +47 -0
- package/src/records.ts +111 -0
- package/src/resolver.ts +74 -0
- package/src/state.ts +398 -0
- package/test/ask.test.ts +265 -0
- package/test/cockpit-actions.test.ts +36 -0
- package/test/cockpit-answers.test.ts +77 -0
- package/test/deadline.test.ts +40 -0
- package/test/ending.test.ts +223 -0
- package/test/entity.test.ts +18 -0
- package/test/ledger.test.ts +64 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// What the ledger reads from the journal: which request a text names, and what a fold of the facts makes of it.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { recordId, seq, streamId } from "@niwa-labs/core";
|
|
6
|
+
import type { JournalReader, StoredRecord } from "@niwa-labs/core";
|
|
7
|
+
import { owner } from "@niwa-labs/core";
|
|
8
|
+
|
|
9
|
+
import { REQUEST_TYPES, RequestCancelledRecord, RequestCreated, RequestId } from "../src/records.ts";
|
|
10
|
+
import { RequestLedger } from "../src/state.ts";
|
|
11
|
+
|
|
12
|
+
/** A journal that holds the records it is given: all the ledger asks of it is `since`. */
|
|
13
|
+
function journalOf(rows: ReadonlyArray<{ type: string; data: unknown; actor?: StoredRecord["actor"] }>): JournalReader {
|
|
14
|
+
const stored = rows.map(
|
|
15
|
+
(row, index): StoredRecord => ({
|
|
16
|
+
id: recordId(`r${String(index + 1)}`),
|
|
17
|
+
seq: seq(index + 1),
|
|
18
|
+
stream: streamId("test"),
|
|
19
|
+
type: row.type as StoredRecord["type"],
|
|
20
|
+
schema: 1,
|
|
21
|
+
data: row.data,
|
|
22
|
+
actor: row.actor ?? { kind: "host" },
|
|
23
|
+
cause: null,
|
|
24
|
+
root: recordId(`r${String(index + 1)}`),
|
|
25
|
+
at: 0 as StoredRecord["at"],
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
return {
|
|
29
|
+
since: (from, types) => stored.filter((one) => one.seq > from && (types === undefined || types.includes(one.type))),
|
|
30
|
+
} as JournalReader;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const created = (id: string) => ({
|
|
34
|
+
type: RequestCreated.name,
|
|
35
|
+
data: { id, to: owner, question: "q.q", input: { kind: "text", title: "Which one?" } },
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("the full id of an ended request names it, though a longer open id starts with the same text", () => {
|
|
39
|
+
const journal = journalOf([created("req-123456"), { type: RequestCancelledRecord.name, data: { id: "req-123456" } }, created("req-1234567")]);
|
|
40
|
+
const ledger = new RequestLedger(REQUEST_TYPES.map((type) => type.name), () => journal);
|
|
41
|
+
expect(ledger.named({ id: "req-123456" })).toBe(RequestId.parse("req-123456"));
|
|
42
|
+
// A short form still prefers what is open.
|
|
43
|
+
expect(ledger.named({ id: "req-12345" })).toBe(RequestId.parse("req-1234567"));
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("the full id of a request the caller may not see is not turned into another request", () => {
|
|
47
|
+
const journal = journalOf([created("req-123456"), created("req-1234567")]);
|
|
48
|
+
const ledger = new RequestLedger(REQUEST_TYPES.map((type) => type.name), () => journal);
|
|
49
|
+
// Only the longer one is the caller's: the exact id still names the first, which it may not act on.
|
|
50
|
+
expect(ledger.named({ id: "req-123456" }, (request) => request.id === "req-1234567")).toBe(RequestId.parse("req-123456"));
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
test("rebuilding a request ignores an unissued end and keeps the provider's end", () => {
|
|
55
|
+
const birth = created("req-123456");
|
|
56
|
+
const end = { type: RequestCancelledRecord.name, data: { id: birth.data.id } };
|
|
57
|
+
const writer: StoredRecord["actor"] = { kind: "client", id: "reader" as never };
|
|
58
|
+
const journal = journalOf([birth, { ...end, actor: writer }]);
|
|
59
|
+
const ledger = new RequestLedger(REQUEST_TYPES.map((type) => type.name), () => journal);
|
|
60
|
+
expect(ledger.requests().get(RequestId.parse(birth.data.id))?.status).toBe("open");
|
|
61
|
+
const restored = journalOf([birth, { ...end, actor: writer }, { ...end, actor: { ...writer, issuedBy: "requests" } }]);
|
|
62
|
+
const finalized = new RequestLedger(REQUEST_TYPES.map((type) => type.name), () => restored);
|
|
63
|
+
expect(finalized.requests().get(RequestId.parse(birth.data.id))?.status).toBe("cancelled");
|
|
64
|
+
});
|