@jam-overture/loom 0.1.0 → 0.2.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.
Files changed (290) hide show
  1. package/README.md +3 -1
  2. package/dist/application/application.d.ts +109 -0
  3. package/dist/application/application.js +97 -0
  4. package/dist/application/memory.d.ts +20 -0
  5. package/dist/application/memory.js +79 -0
  6. package/dist/application/store.d.ts +101 -0
  7. package/dist/application/store.js +25 -0
  8. package/dist/audience/audience.d.ts +36 -0
  9. package/dist/audience/audience.js +41 -0
  10. package/dist/cli/templates.js +19 -0
  11. package/dist/data/plan.js +0 -0
  12. package/dist/data/resolution.d.ts +23 -1
  13. package/dist/data/resolve.d.ts +30 -3
  14. package/dist/data/resolve.js +11 -3
  15. package/dist/ids.d.ts +10 -0
  16. package/dist/ids.js +11 -1
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.js +1 -0
  19. package/dist/patch.d.ts +54 -0
  20. package/dist/patch.js +44 -0
  21. package/dist/project/closing.d.ts +51 -0
  22. package/dist/project/closing.js +53 -0
  23. package/dist/project/memory.d.ts +20 -0
  24. package/dist/project/memory.js +97 -0
  25. package/dist/project/project.d.ts +90 -0
  26. package/dist/project/project.js +91 -0
  27. package/dist/project/store.d.ts +135 -0
  28. package/dist/project/store.js +41 -0
  29. package/dist/registered.d.ts +64 -0
  30. package/dist/registered.js +118 -0
  31. package/dist/render/behaviour-adjust.d.ts +11 -19
  32. package/dist/render/behaviour-adjust.js +75 -21
  33. package/dist/render/behaviour-present.d.ts +8 -0
  34. package/dist/render/behaviour-present.js +57 -6
  35. package/dist/render/behaviour.d.ts +98 -17
  36. package/dist/render/behaviour.js +91 -26
  37. package/dist/render/control-name.d.ts +68 -0
  38. package/dist/render/control-name.js +37 -0
  39. package/dist/render/diagnostics.d.ts +72 -0
  40. package/dist/render/diagnostics.js +7 -0
  41. package/dist/render/index.d.ts +2 -0
  42. package/dist/render/index.js +2 -0
  43. package/dist/render/reads.d.ts +11 -0
  44. package/dist/render/reads.js +12 -1
  45. package/dist/render/render.js +102 -7
  46. package/dist/render/slots.d.ts +52 -0
  47. package/dist/render/slots.js +24 -0
  48. package/dist/render/theme.d.ts +54 -1
  49. package/dist/render/theme.js +23 -1
  50. package/dist/render/unshown.d.ts +152 -0
  51. package/dist/render/unshown.js +80 -0
  52. package/dist/runtime/analysis.d.ts +94 -9
  53. package/dist/runtime/analysis.js +54 -9
  54. package/dist/runtime/assessment.d.ts +36 -2
  55. package/dist/runtime/assessment.js +27 -2
  56. package/dist/runtime/checks.d.ts +148 -0
  57. package/dist/runtime/checks.js +112 -0
  58. package/dist/runtime/disposition.d.ts +96 -0
  59. package/dist/runtime/disposition.js +46 -0
  60. package/dist/runtime/gate.js +22 -0
  61. package/dist/runtime/index.d.ts +3 -0
  62. package/dist/runtime/index.js +3 -0
  63. package/dist/runtime/irreversibility.d.ts +64 -0
  64. package/dist/runtime/irreversibility.js +45 -0
  65. package/dist/runtime/pipeline.d.ts +43 -0
  66. package/dist/runtime/pipeline.js +10 -2
  67. package/dist/runtime/policy-change.d.ts +93 -0
  68. package/dist/runtime/policy-change.js +299 -0
  69. package/dist/runtime/policy-log.d.ts +406 -0
  70. package/dist/runtime/policy-log.js +285 -0
  71. package/dist/runtime/reversibility.d.ts +11 -9
  72. package/dist/runtime/stakes.d.ts +129 -1
  73. package/dist/runtime/stakes.js +240 -26
  74. package/dist/runtime/vocabulary.d.ts +164 -7
  75. package/dist/runtime/vocabulary.js +181 -8
  76. package/dist/sdk/audit.d.ts +174 -3
  77. package/dist/sdk/audit.js +176 -17
  78. package/dist/sdk/catalogue.d.ts +9 -1
  79. package/dist/sdk/conformance.d.ts +37 -3
  80. package/dist/sdk/conformance.js +25 -4
  81. package/dist/sdk/definition.d.ts +65 -0
  82. package/dist/sdk/definition.js +22 -0
  83. package/dist/sdk/registry.d.ts +29 -2
  84. package/dist/sdk/registry.js +63 -1
  85. package/dist/signals/action-change.d.ts +507 -0
  86. package/dist/signals/action-change.js +265 -0
  87. package/dist/signals/action.d.ts +331 -0
  88. package/dist/signals/action.js +182 -0
  89. package/dist/signals/applied-window.contract.d.ts +23 -0
  90. package/dist/signals/applied-window.contract.js +160 -0
  91. package/dist/signals/arriving.d.ts +210 -0
  92. package/dist/signals/arriving.js +133 -0
  93. package/dist/signals/audiences.d.ts +528 -0
  94. package/dist/signals/audiences.js +255 -0
  95. package/dist/signals/broadcast.d.ts +8 -2
  96. package/dist/signals/broadcast.js +58 -8
  97. package/dist/signals/change.d.ts +378 -0
  98. package/dist/signals/change.js +327 -0
  99. package/dist/signals/collect.d.ts +41 -0
  100. package/dist/signals/collect.js +88 -7
  101. package/dist/signals/copy-change.d.ts +553 -0
  102. package/dist/signals/copy-change.js +377 -0
  103. package/dist/signals/copy.d.ts +337 -0
  104. package/dist/signals/copy.js +211 -0
  105. package/dist/signals/counters.d.ts +46 -0
  106. package/dist/signals/counters.js +102 -0
  107. package/dist/signals/deliver.d.ts +23 -4
  108. package/dist/signals/deliver.js +34 -6
  109. package/dist/signals/deployment-change.d.ts +371 -0
  110. package/dist/signals/deployment-change.js +268 -0
  111. package/dist/signals/deployment.d.ts +265 -0
  112. package/dist/signals/deployment.js +152 -0
  113. package/dist/signals/door.d.ts +92 -0
  114. package/dist/signals/door.js +53 -0
  115. package/dist/signals/fold.d.ts +10 -0
  116. package/dist/signals/fold.js +5 -1
  117. package/dist/signals/form-action.d.ts +302 -0
  118. package/dist/signals/form-action.js +169 -0
  119. package/dist/signals/form-copy.d.ts +523 -0
  120. package/dist/signals/form-copy.js +346 -0
  121. package/dist/signals/forms.d.ts +247 -0
  122. package/dist/signals/forms.js +147 -0
  123. package/dist/signals/funnel-change.d.ts +548 -0
  124. package/dist/signals/funnel-change.js +361 -0
  125. package/dist/signals/funnel.d.ts +532 -0
  126. package/dist/signals/funnel.js +216 -0
  127. package/dist/signals/health.d.ts +497 -0
  128. package/dist/signals/health.js +361 -0
  129. package/dist/signals/index.d.ts +41 -0
  130. package/dist/signals/index.js +41 -0
  131. package/dist/signals/ingest.d.ts +56 -1
  132. package/dist/signals/ingest.js +91 -1
  133. package/dist/signals/journal.d.ts +11 -0
  134. package/dist/signals/kinds.d.ts +8 -6
  135. package/dist/signals/kinds.js +14 -6
  136. package/dist/signals/library-action.d.ts +430 -0
  137. package/dist/signals/library-action.js +336 -0
  138. package/dist/signals/library-change.d.ts +477 -0
  139. package/dist/signals/library-change.js +335 -0
  140. package/dist/signals/library-copy-change.d.ts +498 -0
  141. package/dist/signals/library-copy-change.js +476 -0
  142. package/dist/signals/library-pace.d.ts +319 -0
  143. package/dist/signals/library-pace.js +305 -0
  144. package/dist/signals/library-reach-change.d.ts +636 -0
  145. package/dist/signals/library-reach-change.js +534 -0
  146. package/dist/signals/library-reach.d.ts +377 -0
  147. package/dist/signals/library-reach.js +227 -0
  148. package/dist/signals/library-word-reach-change.d.ts +567 -0
  149. package/dist/signals/library-word-reach-change.js +568 -0
  150. package/dist/signals/library.d.ts +306 -0
  151. package/dist/signals/library.js +178 -0
  152. package/dist/signals/memory.d.ts +9 -0
  153. package/dist/signals/memory.js +105 -14
  154. package/dist/signals/migrate.d.ts +17 -2
  155. package/dist/signals/migrate.js +122 -5
  156. package/dist/signals/notice.d.ts +334 -0
  157. package/dist/signals/notice.js +228 -0
  158. package/dist/signals/pace-change.d.ts +501 -0
  159. package/dist/signals/pace-change.js +310 -0
  160. package/dist/signals/pace.d.ts +296 -0
  161. package/dist/signals/pace.js +291 -0
  162. package/dist/signals/page-views.d.ts +365 -0
  163. package/dist/signals/page-views.js +218 -0
  164. package/dist/signals/parts.d.ts +344 -0
  165. package/dist/signals/parts.js +273 -0
  166. package/dist/signals/postgres.d.ts +16 -2
  167. package/dist/signals/postgres.js +390 -87
  168. package/dist/signals/progress.d.ts +212 -0
  169. package/dist/signals/progress.js +150 -0
  170. package/dist/signals/ratios.d.ts +108 -0
  171. package/dist/signals/ratios.js +40 -0
  172. package/dist/signals/reach.d.ts +309 -0
  173. package/dist/signals/reach.js +93 -0
  174. package/dist/signals/readable.d.ts +34 -0
  175. package/dist/signals/readable.js +34 -0
  176. package/dist/signals/readership.d.ts +381 -0
  177. package/dist/signals/readership.js +308 -0
  178. package/dist/signals/region-pages.d.ts +173 -0
  179. package/dist/signals/region-pages.js +62 -0
  180. package/dist/signals/region.d.ts +287 -0
  181. package/dist/signals/region.js +198 -0
  182. package/dist/signals/retention.d.ts +141 -0
  183. package/dist/signals/retention.js +58 -0
  184. package/dist/signals/rollup.d.ts +67 -3
  185. package/dist/signals/rollup.js +0 -0
  186. package/dist/signals/schema.d.ts +432 -1
  187. package/dist/signals/schema.js +157 -3
  188. package/dist/signals/signal.d.ts +237 -11
  189. package/dist/signals/signal.js +70 -10
  190. package/dist/signals/silence-census.d.ts +205 -0
  191. package/dist/signals/silence-census.js +87 -0
  192. package/dist/signals/silences.d.ts +848 -0
  193. package/dist/signals/silences.js +753 -0
  194. package/dist/signals/tally.d.ts +140 -11
  195. package/dist/signals/window-figures.d.ts +148 -0
  196. package/dist/signals/window-figures.js +42 -0
  197. package/dist/signals/window.d.ts +866 -0
  198. package/dist/signals/window.js +693 -0
  199. package/dist/signals/words.d.ts +24 -0
  200. package/dist/signals/words.js +24 -0
  201. package/dist/store/index.d.ts +19 -0
  202. package/dist/store/index.js +19 -0
  203. package/dist/store/memory.d.ts +2 -1
  204. package/dist/store/memory.js +48 -12
  205. package/dist/store/migrate.d.ts +80 -0
  206. package/dist/store/migrate.js +259 -7
  207. package/dist/store/postgres-applications.d.ts +4 -0
  208. package/dist/store/postgres-applications.js +197 -0
  209. package/dist/store/postgres-holds.d.ts +10 -1
  210. package/dist/store/postgres-holds.js +116 -101
  211. package/dist/store/postgres-policy-log.d.ts +14 -0
  212. package/dist/store/postgres-policy-log.js +240 -0
  213. package/dist/store/postgres-projects.d.ts +4 -0
  214. package/dist/store/postgres-projects.js +217 -0
  215. package/dist/store/postgres.d.ts +16 -1
  216. package/dist/store/postgres.js +59 -9
  217. package/dist/store/replay.d.ts +102 -1
  218. package/dist/store/replay.js +190 -19
  219. package/dist/store/reseed.d.ts +238 -0
  220. package/dist/store/reseed.js +308 -0
  221. package/dist/store/schema.d.ts +450 -2
  222. package/dist/store/schema.js +141 -5
  223. package/dist/store/store.d.ts +29 -6
  224. package/dist/submit/resolve.js +2 -1
  225. package/dist/telemetry/calibration.d.ts +25 -0
  226. package/dist/telemetry/calibration.js +27 -1
  227. package/dist/telemetry/event.d.ts +391 -33
  228. package/dist/telemetry/event.js +84 -20
  229. package/dist/telemetry/index.d.ts +1 -0
  230. package/dist/telemetry/index.js +1 -0
  231. package/dist/telemetry/journal.d.ts +72 -3
  232. package/dist/telemetry/journal.js +9 -0
  233. package/dist/telemetry/lookup.d.ts +27 -0
  234. package/dist/telemetry/lookup.js +18 -0
  235. package/dist/telemetry/memory.js +23 -0
  236. package/dist/telemetry/migrate.js +22 -1
  237. package/dist/telemetry/postgres.d.ts +10 -1
  238. package/dist/telemetry/postgres.js +172 -90
  239. package/dist/telemetry/remeasure.d.ts +78 -0
  240. package/dist/telemetry/remeasure.js +83 -0
  241. package/dist/telemetry/retention.d.ts +2 -2
  242. package/dist/telemetry/schema.d.ts +17 -0
  243. package/dist/telemetry/schema.js +27 -1
  244. package/dist/telemetry/sink.d.ts +2 -2
  245. package/dist/testing/application-contract.d.ts +14 -0
  246. package/dist/testing/application-contract.js +270 -0
  247. package/dist/testing/contracts.d.ts +12 -7
  248. package/dist/testing/contracts.js +12 -7
  249. package/dist/testing/definitions.d.ts +78 -0
  250. package/dist/testing/definitions.js +134 -9
  251. package/dist/testing/doubles.d.ts +103 -0
  252. package/dist/testing/doubles.js +143 -0
  253. package/dist/testing/index.d.ts +2 -2
  254. package/dist/testing/index.js +2 -2
  255. package/dist/testing/journal-contract.js +140 -2
  256. package/dist/testing/model-contract.d.ts +110 -0
  257. package/dist/testing/model-contract.js +150 -0
  258. package/dist/testing/policy-log-contract.d.ts +26 -0
  259. package/dist/testing/policy-log-contract.js +344 -0
  260. package/dist/testing/project-contract.d.ts +14 -0
  261. package/dist/testing/project-contract.js +266 -0
  262. package/dist/testing/reader-signal-contract.d.ts +27 -2
  263. package/dist/testing/reader-signal-contract.js +475 -5
  264. package/dist/testing/store-contract.d.ts +7 -1
  265. package/dist/testing/store-contract.js +84 -3
  266. package/dist/theme/apply.d.ts +20 -1
  267. package/dist/theme/apply.js +5 -3
  268. package/dist/theme/derive.d.ts +22 -0
  269. package/dist/theme/derive.js +54 -1
  270. package/dist/theme/library.js +1 -1
  271. package/dist/theme/measure.d.ts +34 -0
  272. package/dist/theme/measure.js +30 -1
  273. package/dist/theme/palettes.js +7 -7
  274. package/dist/theme/ramp.d.ts +64 -0
  275. package/dist/theme/ramp.js +114 -0
  276. package/dist/theme/registry.d.ts +9 -0
  277. package/dist/theme/registry.js +11 -1
  278. package/dist/theme/separation.d.ts +97 -3
  279. package/dist/theme/separation.js +75 -0
  280. package/dist/tree/compare.d.ts +9 -0
  281. package/dist/tree/compare.js +18 -0
  282. package/dist/write/computed.d.ts +60 -0
  283. package/dist/write/computed.js +61 -0
  284. package/dist/write/held.d.ts +59 -0
  285. package/dist/write/index.d.ts +5 -2
  286. package/dist/write/index.js +5 -2
  287. package/dist/write/reseed.d.ts +151 -0
  288. package/dist/write/reseed.js +184 -0
  289. package/dist/write/revert.js +2 -16
  290. package/package.json +98 -19
@@ -1,9 +1,11 @@
1
1
  import { describe, expect, it } from "vitest";
2
+ import { DEFAULT_AUDIENCE_ID, audienceIdSchema, } from "../audience/audience.js";
2
3
  import { nodeIdSchema, treeIdSchema } from "../ids.js";
3
4
  import { primitiveTypeSchema } from "../primitive-type.js";
5
+ import { UNKNOWN_REGION, } from "../signals/region.js";
4
6
  /**
5
- * Two suites, run against every implementation of the buffer and of the
6
- * counters.
7
+ * Three suites, run against every implementation of the buffer, the counters and
8
+ * the regions.
7
9
  *
8
10
  * The same argument as `store-contract.ts` and `journal-contract.ts`: an
9
11
  * interface is only tested once a second implementation exists, and a
@@ -17,6 +19,17 @@ export const nodeId = (name) => nodeIdSchema.parse(`n_${name}`);
17
19
  export const primitiveType = (name) => primitiveTypeSchema.parse(name);
18
20
  /** A key of the right shape, spelled so a failing assertion says which view it was. */
19
21
  export const viewKey = (nth) => String(nth).padStart(32, "0");
22
+ export const region = (code) => code;
23
+ /**
24
+ * Two audiences to tell apart, and neither of them the default.
25
+ *
26
+ * Named rather than reusing `aud_everyone` for one side, because a test whose
27
+ * two audiences are *everyone* and *somebody* passes on a store that quietly
28
+ * files everything under the default — which is exactly the failure these are
29
+ * here to catch (0284).
30
+ */
31
+ export const SKIERS = audienceIdSchema.parse("aud_skiers");
32
+ export const RIDERS = audienceIdSchema.parse("aud_riders");
20
33
  export const batchOf = (signals, overrides = {}) => ({
21
34
  treeId: TREE,
22
35
  revision: 1,
@@ -59,6 +72,20 @@ export const disclosedIn = (name, open, ...regions) => ({
59
72
  at: 1_200,
60
73
  within: regions.map((region) => ({ nodeId: nodeId(region), type: primitiveType("loom.section") })),
61
74
  });
75
+ export const completed = (name) => ({
76
+ kind: "completed",
77
+ nodeId: nodeId(name),
78
+ type: primitiveType("loom.form"),
79
+ at: 1_300,
80
+ });
81
+ /** A form let go, and the bands it was the end of — nearest first, like a press. */
82
+ export const completedIn = (name, ...regions) => ({
83
+ kind: "completed",
84
+ nodeId: nodeId(name),
85
+ type: primitiveType("loom.form"),
86
+ at: 1_300,
87
+ within: regions.map((region) => ({ nodeId: nodeId(region), type: primitiveType("loom.section") })),
88
+ });
62
89
  export const dwelled = (name, ms) => ({
63
90
  kind: "dwelled",
64
91
  nodeId: nodeId(name),
@@ -77,6 +104,7 @@ const tally = (overrides = {}) => ({
77
104
  activations: 0,
78
105
  opens: 0,
79
106
  closes: 0,
107
+ completions: 0,
80
108
  ...overrides,
81
109
  });
82
110
  const PAIR = {
@@ -131,6 +159,50 @@ export const describeReaderSignalJournalContract = (name, make) => {
131
159
  viewKey(1),
132
160
  ]);
133
161
  });
162
+ /**
163
+ * The marker that says a page view began with this delivery is read at the
164
+ * door and goes no further. Both implementations drop it, and they have to
165
+ * agree: a buffer that kept it in memory and lost it in Postgres is a
166
+ * rollup whose answer depends on where it ran.
167
+ */
168
+ it("does not buffer the opening marker", async () => {
169
+ const journal = await make();
170
+ await journal.receive([batchOf([viewed("a")], { view: viewKey(3), first: true })]);
171
+ const [stored] = unwrap(await journal.read()).batches;
172
+ expect(stored?.first).toBeUndefined();
173
+ expect(stored?.view).toBe(viewKey(3));
174
+ });
175
+ /**
176
+ * The dimension this record is about, at the point it enters the system
177
+ * (0284). A buffer that dropped it would make every counter below it one
178
+ * population rather than two, and nothing afterwards could tell.
179
+ */
180
+ it("files a batch under the audience it names", async () => {
181
+ const journal = await make();
182
+ await journal.receive([batchOf([viewed("a")], { audience: SKIERS })]);
183
+ expect(unwrap(await journal.read()).batches[0]?.audienceId).toBe(SKIERS);
184
+ });
185
+ /**
186
+ * Resolved rather than left absent, so a fold reads one spelling of *this
187
+ * batch named no audience*. A sender serving one form of every page has
188
+ * nothing to say, and what it was doing all along has a name now.
189
+ */
190
+ it("files a batch that named no audience under everyone", async () => {
191
+ const journal = await make();
192
+ await journal.receive([batchOf([viewed("a")])]);
193
+ expect(unwrap(await journal.read()).batches[0]?.audienceId).toBe(DEFAULT_AUDIENCE_ID);
194
+ });
195
+ it("keeps two audiences' batches apart in one delivery", async () => {
196
+ const journal = await make();
197
+ await journal.receive([
198
+ batchOf([viewed("a")], { audience: SKIERS }),
199
+ batchOf([viewed("b")], { audience: RIDERS }),
200
+ ]);
201
+ expect(unwrap(await journal.read()).batches.map((batch) => batch.audienceId)).toEqual([
202
+ SKIERS,
203
+ RIDERS,
204
+ ]);
205
+ });
134
206
  it("keeps trees apart", async () => {
135
207
  const journal = await make();
136
208
  await journal.receive([batchOf([viewed("a")]), batchOf([viewed("b")], { treeId: OTHER_TREE })]);
@@ -241,6 +313,64 @@ export const describeReaderTallyStoreContract = (name, make) => {
241
313
  expect(unwrap(await store.tallies())).toEqual([]);
242
314
  expect(unwrap(await store.funnels())).toEqual([]);
243
315
  });
316
+ /**
317
+ * ## The audience (0284)
318
+ *
319
+ * The dimension is in the key, and these are the three things that makes
320
+ * true: two audiences never fold into one row, a reading may name one, and
321
+ * a reading that names none gets both and can tell them apart.
322
+ *
323
+ * The failure this guards is the one described of applications, and of
324
+ * audiences before them — *a number that looks right forever* (0273, 0263).
325
+ * Two variants summed into one tally is not recoverable from the rows, so a
326
+ * store that keyed without the audience would pass every other test in this
327
+ * file.
328
+ */
329
+ describe("the audience", () => {
330
+ it("keeps two audiences' tallies of the same node apart", async () => {
331
+ const store = await make();
332
+ await store.apply({ tallies: [tally({ views: 2 })], funnels: [] }, AT, SKIERS);
333
+ await store.apply({ tallies: [tally({ views: 5 })], funnels: [] }, AT, RIDERS);
334
+ const rows = unwrap(await store.tallies());
335
+ expect(rows).toHaveLength(2);
336
+ expect(Object.fromEntries(rows.map((row) => [row.audienceId, row.views]))).toEqual({ [SKIERS]: 2, [RIDERS]: 5 });
337
+ });
338
+ it("adds a second window of the same audience to the first", async () => {
339
+ const store = await make();
340
+ await store.apply({ tallies: [tally({ views: 2 })], funnels: [] }, AT, SKIERS);
341
+ await store.apply({ tallies: [tally({ views: 3 })], funnels: [] }, LATER, SKIERS);
342
+ const rows = unwrap(await store.tallies());
343
+ expect(rows).toHaveLength(1);
344
+ expect(rows[0]).toMatchObject({ audienceId: SKIERS, views: 5, updatedAt: LATER });
345
+ });
346
+ it("reads one audience without the other", async () => {
347
+ const store = await make();
348
+ await store.apply({ tallies: [tally({ views: 2 })], funnels: [] }, AT, SKIERS);
349
+ await store.apply({ tallies: [tally({ views: 5 })], funnels: [] }, AT, RIDERS);
350
+ const rows = unwrap(await store.tallies({ audience: RIDERS }));
351
+ expect(rows).toHaveLength(1);
352
+ expect(rows[0]).toMatchObject({ audienceId: RIDERS, views: 5 });
353
+ });
354
+ it("reads an audience nothing was ever filed under as nothing", async () => {
355
+ const store = await make();
356
+ await store.apply({ tallies: [tally()], funnels: [] }, AT, SKIERS);
357
+ expect(unwrap(await store.tallies({ audience: RIDERS }))).toEqual([]);
358
+ });
359
+ it("files a window that named no audience under everyone", async () => {
360
+ const store = await make();
361
+ await store.apply({ tallies: [tally()], funnels: [] }, AT);
362
+ expect(unwrap(await store.tallies())[0]?.audienceId).toBe(DEFAULT_AUDIENCE_ID);
363
+ });
364
+ it("keeps two audiences' funnel answers apart", async () => {
365
+ const store = await make();
366
+ const answer = { treeId: TREE, revision: 1, pair: PAIR, reached: 10 };
367
+ await store.apply({ tallies: [], funnels: [{ ...answer, converted: 1 }] }, AT, SKIERS);
368
+ await store.apply({ tallies: [], funnels: [{ ...answer, converted: 4 }] }, AT, RIDERS);
369
+ const rows = unwrap(await store.funnels());
370
+ expect(rows).toHaveLength(2);
371
+ expect(Object.fromEntries(rows.map((row) => [row.audienceId, row.converted]))).toEqual({ [SKIERS]: 1, [RIDERS]: 4 });
372
+ });
373
+ });
244
374
  it("accepts a rollup with nothing in it", async () => {
245
375
  const store = await make();
246
376
  expect(unwrap(await store.apply({ tallies: [], funnels: [] }, AT))).toBeUndefined();
@@ -253,10 +383,10 @@ export const describeReaderTallyStoreContract = (name, make) => {
253
383
  */
254
384
  it("adds a second window to the first rather than replacing it", async () => {
255
385
  const store = await make();
256
- await store.apply({ tallies: [tally({ dwellMs: 500, activations: 1 })], funnels: [] }, AT);
257
- await store.apply({ tallies: [tally({ dwellMs: 250, activations: 2 })], funnels: [] }, LATER);
386
+ await store.apply({ tallies: [tally({ dwellMs: 500, activations: 1, completions: 1 })], funnels: [] }, AT);
387
+ await store.apply({ tallies: [tally({ dwellMs: 250, activations: 2, completions: 2 })], funnels: [] }, LATER);
258
388
  const [row] = unwrap(await store.tallies());
259
- expect(row).toMatchObject({ dwellMs: 750, activations: 3, views: 2, updatedAt: LATER });
389
+ expect(row).toMatchObject({ dwellMs: 750, activations: 3, completions: 3, views: 2, updatedAt: LATER });
260
390
  });
261
391
  /**
262
392
  * The counter regions are reported by, held to the same rule as the rest.
@@ -272,6 +402,39 @@ export const describeReaderTallyStoreContract = (name, make) => {
272
402
  const [row] = unwrap(await store.tallies());
273
403
  expect(row).toMatchObject({ engaged: 5, updatedAt: LATER });
274
404
  });
405
+ /**
406
+ * The same seam the page views were bitten by, held on the counter it would
407
+ * matter most on: `ON CONFLICT … DO UPDATE` refuses to affect one row twice
408
+ * in one command, so a rollup handed a list with a node in it twice must be
409
+ * folded before the statement rather than refused by the driver. Nothing in
410
+ * the repository can produce that list — `rollUp` keys its output by map —
411
+ * and a caller composing counters of its own can.
412
+ */
413
+ it("adds up two tallies of the same node in one call", async () => {
414
+ const store = await make();
415
+ await store.apply({
416
+ tallies: [
417
+ tally({ dwellMs: 500, views: 1, reached: 1, activations: 1 }),
418
+ tally({ dwellMs: 250, views: 2, reached: 2, activations: 3 }),
419
+ ],
420
+ funnels: [],
421
+ }, AT);
422
+ const rows = unwrap(await store.tallies());
423
+ expect(rows).toHaveLength(1);
424
+ expect(rows[0]).toMatchObject({ dwellMs: 750, views: 3, reached: 3, activations: 4 });
425
+ });
426
+ /** The later row wins the one column that is not a number, in both stores. */
427
+ it("takes the last type given for a node named twice in one call", async () => {
428
+ const store = await make();
429
+ await store.apply({
430
+ tallies: [
431
+ tally({ type: primitiveType("loom.section") }),
432
+ tally({ type: primitiveType("loom.hero") }),
433
+ ],
434
+ funnels: [],
435
+ }, AT);
436
+ expect(unwrap(await store.tallies())[0]).toMatchObject({ type: "loom.hero" });
437
+ });
275
438
  it("keeps revisions of the same node apart", async () => {
276
439
  const store = await make();
277
440
  await store.apply({ tallies: [tally({ revision: 1, dwellMs: 500 }), tally({ revision: 2, dwellMs: 30 })], funnels: [] }, AT);
@@ -301,6 +464,199 @@ export const describeReaderTallyStoreContract = (name, make) => {
301
464
  await store.apply({ tallies: [], funnels: [{ ...answer, reached: 10, converted: 1 }] }, LATER);
302
465
  expect(unwrap(await store.funnels())[0]).toMatchObject({ reached: 50, converted: 10 });
303
466
  });
467
+ /** The funnels' half of the same fold, keyed by both ends of the pair. */
468
+ it("adds up two answers about the same pair in one call", async () => {
469
+ const store = await make();
470
+ await store.apply({
471
+ tallies: [],
472
+ funnels: [
473
+ { treeId: TREE, revision: 1, pair: PAIR, reached: 40, converted: 9 },
474
+ { treeId: TREE, revision: 1, pair: PAIR, reached: 10, converted: 1 },
475
+ ],
476
+ }, AT);
477
+ const rows = unwrap(await store.funnels());
478
+ expect(rows).toHaveLength(1);
479
+ expect(rows[0]).toMatchObject({ reached: 50, converted: 10 });
480
+ });
481
+ describe("page views", () => {
482
+ const opening = (over = {}) => ({
483
+ treeId: TREE,
484
+ revision: 1,
485
+ views: 1,
486
+ ...over,
487
+ });
488
+ it("reads nothing before anybody has arrived", async () => {
489
+ const store = await make();
490
+ expect(unwrap(await store.pageViews())).toEqual([]);
491
+ });
492
+ /**
493
+ * The door's half of the dimension, on the counter it matters most on
494
+ * (0284): page views are the denominator every rate on a screen is taken
495
+ * of, so two audiences summed here would make both variants' rates wrong
496
+ * in the same invisible direction.
497
+ */
498
+ it("keeps two audiences' openings apart", async () => {
499
+ const store = await make();
500
+ await store.opened([opening({ views: 3 })], AT, SKIERS);
501
+ await store.opened([opening({ views: 7 })], AT, RIDERS);
502
+ const rows = unwrap(await store.pageViews());
503
+ expect(rows).toHaveLength(2);
504
+ expect(Object.fromEntries(rows.map((row) => [row.audienceId, row.opened]))).toEqual({ [SKIERS]: 3, [RIDERS]: 7 });
505
+ });
506
+ /**
507
+ * The row written from two sides, in one audience. A fold's appearances
508
+ * and a door's openings have to meet in the same row or the subtraction
509
+ * that measures the straddle (0147) is taken across two populations.
510
+ */
511
+ it("meets a fold of the same audience in one row", async () => {
512
+ const store = await make();
513
+ await store.opened([opening({ views: 4 })], AT, SKIERS);
514
+ await store.apply({ tallies: [], funnels: [], appearances: [opening({ views: 5 })] }, LATER, SKIERS);
515
+ const rows = unwrap(await store.pageViews());
516
+ expect(rows).toHaveLength(1);
517
+ expect(rows[0]).toMatchObject({ audienceId: SKIERS, opened: 4, appearances: 5 });
518
+ });
519
+ it("keeps a fold of one audience out of another's row", async () => {
520
+ const store = await make();
521
+ await store.opened([opening({ views: 4 })], AT, SKIERS);
522
+ await store.apply({ tallies: [], funnels: [], appearances: [opening({ views: 5 })] }, LATER, RIDERS);
523
+ const rows = unwrap(await store.pageViews());
524
+ expect(rows).toHaveLength(2);
525
+ expect(Object.fromEntries(rows.map((row) => [row.audienceId, [row.opened, row.appearances]]))).toEqual({ [SKIERS]: [4, 0], [RIDERS]: [0, 5] });
526
+ });
527
+ it("accepts a delivery that opened nothing", async () => {
528
+ const store = await make();
529
+ expect(unwrap(await store.opened([], AT))).toBeUndefined();
530
+ expect(unwrap(await store.pageViews())).toEqual([]);
531
+ });
532
+ it("reads back the page views a door counted", async () => {
533
+ const store = await make();
534
+ await store.opened([opening({ views: 3 })], AT);
535
+ expect(unwrap(await store.pageViews())[0]).toMatchObject({
536
+ treeId: TREE,
537
+ revision: 1,
538
+ opened: 3,
539
+ appearances: 0,
540
+ updatedAt: AT,
541
+ });
542
+ });
543
+ /**
544
+ * The property the exactness rests on. Every call is one delivery's worth
545
+ * of arrivals, so a store that replaced would report the last reader
546
+ * rather than the readership — and unlike a distinct count there is no
547
+ * window here for the error to be bounded by.
548
+ */
549
+ it("adds a second delivery's arrivals to the first", async () => {
550
+ const store = await make();
551
+ await store.opened([opening({ views: 2 })], AT);
552
+ await store.opened([opening({ views: 5 })], LATER);
553
+ expect(unwrap(await store.pageViews())[0]).toMatchObject({ opened: 7, updatedAt: LATER });
554
+ });
555
+ /**
556
+ * The two numbers are counted from opposite ends and must not land in the
557
+ * same column: their difference is the over-count in every distinct view
558
+ * count stored against the revision (0147), and a store that added them
559
+ * together would report nought drift for ever.
560
+ */
561
+ it("keeps a rollup's appearances beside the openings rather than in them", async () => {
562
+ const store = await make();
563
+ await store.opened([opening({ views: 10 })], AT);
564
+ await store.apply({ tallies: [], funnels: [], appearances: [opening({ views: 11 })] }, LATER);
565
+ expect(unwrap(await store.pageViews())[0]).toMatchObject({
566
+ opened: 10,
567
+ appearances: 11,
568
+ updatedAt: LATER,
569
+ });
570
+ });
571
+ it("adds a second window's appearances to the first", async () => {
572
+ const store = await make();
573
+ await store.apply({ tallies: [], funnels: [], appearances: [opening({ views: 4 })] }, AT);
574
+ await store.apply({ tallies: [], funnels: [], appearances: [opening({ views: 6 })] }, LATER);
575
+ expect(unwrap(await store.pageViews())[0]).toMatchObject({ opened: 0, appearances: 10 });
576
+ });
577
+ /**
578
+ * A rollup applied with no appearances is a caller that has none to
579
+ * report — a backfill, a fixture — and it must not write a row saying
580
+ * nobody arrived, which a reading would show as counters that are exact.
581
+ */
582
+ it("writes no page-view row for a rollup that reported no window", async () => {
583
+ const store = await make();
584
+ await store.apply({ tallies: [tally()], funnels: [] }, AT);
585
+ expect(unwrap(await store.pageViews())).toEqual([]);
586
+ });
587
+ it("keeps revisions apart, which is what before and after a change means here", async () => {
588
+ const store = await make();
589
+ await store.opened([opening({ views: 3 }), opening({ revision: 2, views: 8 })], AT);
590
+ expect(unwrap(await store.pageViews({ revision: 2 })).map((row) => row.opened)).toEqual([8]);
591
+ });
592
+ it("keeps trees apart", async () => {
593
+ const store = await make();
594
+ await store.opened([opening(), opening({ treeId: OTHER_TREE, views: 4 })], AT);
595
+ expect(unwrap(await store.pageViews({ treeId: OTHER_TREE })).map((row) => row.opened)).toEqual([4]);
596
+ });
597
+ /**
598
+ * The fold's own timestamp, which is the one field of this row a door
599
+ * write may not touch (0274).
600
+ *
601
+ * `updatedAt` cannot say when a revision was last folded, because the
602
+ * door writes it on every delivery — so a deployment whose rollup has
603
+ * stopped keeps a fresh timestamp on a row whose appearances never move
604
+ * again. These four facts are what make the fold's age readable instead.
605
+ */
606
+ describe("when the fold last touched the row", () => {
607
+ it("attests no fold on a row a door write created", async () => {
608
+ const store = await make();
609
+ await store.opened([opening({ views: 3 })], AT);
610
+ expect(unwrap(await store.pageViews())[0]?.foldedAt).toBeUndefined();
611
+ });
612
+ it("stamps the instant a window was folded", async () => {
613
+ const store = await make();
614
+ await store.apply({ tallies: [], funnels: [], appearances: [opening()] }, AT);
615
+ expect(unwrap(await store.pageViews())[0]?.foldedAt).toBe(AT);
616
+ });
617
+ /**
618
+ * The property the whole fault rests on: arrivals land on the request
619
+ * path, so if they moved this column a busy page would report a fresh
620
+ * fold for ever and a stalled collection would be undiagnosable.
621
+ */
622
+ it("leaves it where the fold left it when an arrival lands afterwards", async () => {
623
+ const store = await make();
624
+ await store.apply({ tallies: [], funnels: [], appearances: [opening()] }, AT);
625
+ await store.opened([opening({ views: 5 })], LATER);
626
+ expect(unwrap(await store.pageViews())[0]).toMatchObject({
627
+ opened: 5,
628
+ updatedAt: LATER,
629
+ foldedAt: AT,
630
+ });
631
+ });
632
+ it("moves it to the later of two folds", async () => {
633
+ const store = await make();
634
+ await store.apply({ tallies: [], funnels: [], appearances: [opening()] }, AT);
635
+ await store.apply({ tallies: [], funnels: [], appearances: [opening()] }, LATER);
636
+ expect(unwrap(await store.pageViews())[0]?.foldedAt).toBe(LATER);
637
+ });
638
+ /**
639
+ * A caller applying counters it composed itself reports no window
640
+ * (`tally.ts`), so it has folded nothing and may not say it has — a
641
+ * backfill that stamped a fold would make a stalled deployment look
642
+ * freshly collected.
643
+ */
644
+ it("attests no fold for a rollup that reported no window", async () => {
645
+ const store = await make();
646
+ await store.opened([opening({ views: 3 })], AT);
647
+ await store.apply({ tallies: [tally()], funnels: [] }, LATER);
648
+ expect(unwrap(await store.pageViews())[0]?.foldedAt).toBeUndefined();
649
+ });
650
+ });
651
+ /** Two arrivals in one call are one row and one addition, not a row each. */
652
+ it("adds up two openings of the same revision in one call", async () => {
653
+ const store = await make();
654
+ await store.opened([opening({ views: 2 }), opening({ views: 3 })], AT);
655
+ const rows = unwrap(await store.pageViews());
656
+ expect(rows).toHaveLength(1);
657
+ expect(rows[0]).toMatchObject({ opened: 5 });
658
+ });
659
+ });
304
660
  it("keeps two funnels that share a starting node apart", async () => {
305
661
  const store = await make();
306
662
  const other = { from: PAIR.from, to: { nodeId: nodeId("contact"), kind: "activated" } };
@@ -315,4 +671,118 @@ export const describeReaderTallyStoreContract = (name, make) => {
315
671
  });
316
672
  });
317
673
  };
674
+ /**
675
+ * Where readers were, against every implementation.
676
+ *
677
+ * Shorter than the other two suites because the table is narrower, and the
678
+ * properties it does assert are the ones a wrong implementation would be
679
+ * plausible without: that a second delivery adds to the first, that two
680
+ * countries are two rows, and that a revision is part of the key.
681
+ */
682
+ export const describeReaderRegionStoreContract = (name, make) => {
683
+ describe(`${name} — ReaderRegionStore contract`, () => {
684
+ const count = (over = {}) => ({
685
+ treeId: TREE,
686
+ revision: 1,
687
+ region: region("GB"),
688
+ views: 1,
689
+ ...over,
690
+ });
691
+ it("reads back a count it was given", async () => {
692
+ const store = await make();
693
+ await store.count([count({ views: 4 })], AT);
694
+ expect(unwrap(await store.regions())[0]).toMatchObject({
695
+ region: "GB",
696
+ views: 4,
697
+ revision: 1,
698
+ updatedAt: AT,
699
+ });
700
+ });
701
+ it("reads nothing from a store nothing has been counted into", async () => {
702
+ const store = await make();
703
+ expect(unwrap(await store.regions())).toEqual([]);
704
+ });
705
+ /** The regions' half of the dimension — see the tally contract's note (0284). */
706
+ it("keeps two audiences' buckets apart", async () => {
707
+ const store = await make();
708
+ await store.count([count({ views: 3 })], AT, SKIERS);
709
+ await store.count([count({ views: 8 })], AT, RIDERS);
710
+ const rows = unwrap(await store.regions());
711
+ expect(rows).toHaveLength(2);
712
+ expect(Object.fromEntries(rows.map((row) => [row.audienceId, row.views]))).toEqual({
713
+ [SKIERS]: 3,
714
+ [RIDERS]: 8,
715
+ });
716
+ });
717
+ it("reads one audience's buckets without the other's", async () => {
718
+ const store = await make();
719
+ await store.count([count({ views: 3 })], AT, SKIERS);
720
+ await store.count([count({ views: 8 })], AT, RIDERS);
721
+ const rows = unwrap(await store.regions({ audience: SKIERS }));
722
+ expect(rows).toHaveLength(1);
723
+ expect(rows[0]).toMatchObject({ audienceId: SKIERS, views: 3 });
724
+ });
725
+ it("accepts a delivery that counted nothing", async () => {
726
+ const store = await make();
727
+ expect(unwrap(await store.count([], AT))).toBeUndefined();
728
+ });
729
+ /**
730
+ * The property the floor depends on. Every call carries one delivery's
731
+ * arrivals, so a store that replaced would hold a bucket at one view for
732
+ * ever and withhold a country a thousand readers came from.
733
+ */
734
+ it("adds a second delivery to the first rather than replacing it", async () => {
735
+ const store = await make();
736
+ await store.count([count({ views: 3 })], AT);
737
+ await store.count([count({ views: 2 })], LATER);
738
+ const rows = unwrap(await store.regions());
739
+ expect(rows).toHaveLength(1);
740
+ expect(rows[0]).toMatchObject({ views: 5, updatedAt: LATER });
741
+ });
742
+ /**
743
+ * The sharpest of the four, because of where it runs. A delivery whose
744
+ * batches place two readers in one country reaches this with the bucket
745
+ * named twice — and a refused write at the door is a bucket that silently
746
+ * did not move, reported in the delivery's outcome and nowhere else.
747
+ */
748
+ it("adds up two counts of the same bucket in one call", async () => {
749
+ const store = await make();
750
+ await store.count([count({ views: 3 }), count({ views: 2 })], AT);
751
+ const rows = unwrap(await store.regions());
752
+ expect(rows).toHaveLength(1);
753
+ expect(rows[0]).toMatchObject({ region: "GB", views: 5 });
754
+ });
755
+ it("keeps two regions apart", async () => {
756
+ const store = await make();
757
+ await store.count([count({ views: 3 }), count({ region: region("FR"), views: 1 })], AT);
758
+ expect(unwrap(await store.regions())
759
+ .map((row) => `${row.region}:${row.views}`)
760
+ .sort()).toEqual(["FR:1", "GB:3"]);
761
+ });
762
+ it("keeps the readers of one revision apart from the next", async () => {
763
+ const store = await make();
764
+ await store.count([count({ views: 3 }), count({ revision: 2, views: 1 })], AT);
765
+ expect(unwrap(await store.regions()).map((row) => row.revision).sort()).toEqual([1, 2]);
766
+ });
767
+ it("keeps a bucket for the readers it could not place", async () => {
768
+ const store = await make();
769
+ await store.count([count({ region: UNKNOWN_REGION, views: 2 })], AT);
770
+ expect(unwrap(await store.regions())[0]).toMatchObject({ region: "unknown", views: 2 });
771
+ });
772
+ it("reads one tree without the other", async () => {
773
+ const store = await make();
774
+ await store.count([count(), count({ treeId: OTHER_TREE, views: 7 })], AT);
775
+ const rows = unwrap(await store.regions({ treeId: OTHER_TREE }));
776
+ expect(rows).toHaveLength(1);
777
+ expect(rows[0]?.views).toBe(7);
778
+ });
779
+ it("reads one revision without the other", async () => {
780
+ const store = await make();
781
+ await store.count([count(), count({ revision: 9, views: 7 })], AT);
782
+ const rows = unwrap(await store.regions({ revision: 9 }));
783
+ expect(rows).toHaveLength(1);
784
+ expect(rows[0]?.views).toBe(7);
785
+ });
786
+ });
787
+ };
318
788
  //# sourceMappingURL=reader-signal-contract.js.map
@@ -14,7 +14,13 @@ import { type LoomTree } from "../tree/tree.js";
14
14
  * implementation — how it reports a connection failure, what it exposes beyond
15
15
  * the interface — stays in that implementation's own test file.
16
16
  */
17
- export declare const appendOf: (delta: TreeDelta, proposal?: string) => AppendRequest;
17
+ export declare const appendOf: (delta: TreeDelta, proposal?: string,
18
+ /**
19
+ * When the runtime applied it. Defaulted, because almost every case here is
20
+ * about what a store does with a delta rather than when; named by the cases
21
+ * that are, which cannot be written at all if every append shares one stamp.
22
+ */
23
+ appliedAt?: string) => AppendRequest;
18
24
  export declare const removalOf: (tree: LoomTree, nodeId: string, baseRevision?: number) => TreeDelta;
19
25
  export declare const treeNamed: (namespace: string) => LoomTree;
20
26
  /**