@bitcobblers/wod-wiki-core 0.52.2198 → 0.53.2204

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.
@@ -0,0 +1,569 @@
1
+ // src/models/blockContentId.ts
2
+ function blockContentId(content) {
3
+ const normalized = content.trim();
4
+ let hash = 2166136261;
5
+ for (let i = 0; i < normalized.length; i++) {
6
+ hash ^= normalized.charCodeAt(i);
7
+ hash = Math.imul(hash, 16777619);
8
+ }
9
+ return `bc-${(hash >>> 0).toString(16).padStart(8, "0")}`;
10
+ }
11
+
12
+ // src/sections/sectionParser.ts
13
+ var KNOWN_SECTION_WIDGETS = ["playground-run-tip", "attention", "code-example", "syntax-group"];
14
+ var KNOWN_QUERY_WIDGET_TYPES = {
15
+ table: true,
16
+ list: true,
17
+ value: true,
18
+ timeseries: true,
19
+ bar: true,
20
+ donut: true,
21
+ toplist: true,
22
+ "stacked-bar": true,
23
+ "goal-rings": true,
24
+ "zone-distribution": true
25
+ };
26
+ function matchDialectFence(trimmedLine) {
27
+ const match = trimmedLine.match(/^`{3,}(time|log)(?::([\w-]+))?\s*$/i);
28
+ if (!match) return null;
29
+ return { dialect: match[1].toLowerCase() === "time" ? "time" : "log", ...match[2] ? { sport: match[2] } : {} };
30
+ }
31
+ function frontmatterSubtype(properties) {
32
+ const type = properties.type?.toLowerCase();
33
+ if (type === "youtube" || type === "strava" || type === "amazon" || type === "file" || type === "effort" || type === "equipment") return type;
34
+ const url = properties.url || properties.link || "";
35
+ const host = url.replace(/^[a-z][a-z0-9+.-]*:\/\//i, "").split(/[/?#:]/, 1)[0].toLowerCase();
36
+ if (host === "youtu.be" || host === "youtube.com" || host.endsWith(".youtube.com")) return "youtube";
37
+ if (host === "strava.com" || host.endsWith(".strava.com")) return "strava";
38
+ if (host === "amzn.to" || host === "amazon.com" || host.endsWith(".amazon.com")) return "amazon";
39
+ if (["baseAttributes", "metrics.met", "discipline", "effortType", "targetRPE", "equipment", "aliases", "derivation"].some((key) => key in properties)) return "effort";
40
+ if (["slug", "label", "registrySource", "ownerNoteId", "sourcePath"].some((key) => key in properties)) return "equipment";
41
+ return "default";
42
+ }
43
+ function markdownSubtype(text) {
44
+ if (/^#{1,6}\s/.test(text)) return "heading";
45
+ if (/^[-*+]\s|^\d+\.\s/.test(text)) return "list";
46
+ if (/^>\s?/.test(text)) return "blockquote";
47
+ if (text.startsWith("|") && /^\|[\s:]*-+/m.test(text)) return "table";
48
+ return "paragraph";
49
+ }
50
+ function parseDocumentSections(content, options = {}) {
51
+ if (!content) return [];
52
+ const lines = content.split(/\r\n|\n|\r/);
53
+ const offsets = [];
54
+ const separators = [];
55
+ let offset = 0;
56
+ for (const text of lines) {
57
+ offsets.push(offset);
58
+ offset += text.length;
59
+ const separator = content.startsWith("\r\n", offset) ? "\r\n" : /[\r\n]/.test(content[offset] ?? "") ? content[offset] : "";
60
+ separators.push(separator);
61
+ offset += separator.length;
62
+ }
63
+ const sections = [];
64
+ const createdAt = Date.now();
65
+ let firstText = true;
66
+ let line = 0;
67
+ function make(type, start, end) {
68
+ const from = offsets[start];
69
+ const to = offsets[end] + lines[end].length;
70
+ return {
71
+ id: crypto.randomUUID(),
72
+ type,
73
+ from,
74
+ to,
75
+ rawContent: content.slice(from, to),
76
+ displayContent: content.slice(from, to),
77
+ startLine: start,
78
+ endLine: end,
79
+ lineCount: end - start + 1,
80
+ version: 1,
81
+ createdAt,
82
+ separatorAfter: separators[end]
83
+ };
84
+ }
85
+ while (line < lines.length) {
86
+ const text = lines[line].trim();
87
+ const fence = text.match(/^(`{3,}|~{3,})(.*)$/);
88
+ if (fence) {
89
+ const marker = fence[1];
90
+ let close = line + 1;
91
+ const closing = new RegExp(`^${marker[0]}{${marker.length},}\\s*$`);
92
+ while (close < lines.length && !closing.test(lines[close].trim())) close++;
93
+ const closed = close < lines.length;
94
+ const end2 = closed ? close : lines.length - 1;
95
+ const tag = fence[2].trim();
96
+ const dialect = marker[0] === "`" ? matchDialectFence(`\`\`\`${tag}`) : null;
97
+ const widget = marker[0] === "`" ? tag.match(/^widget:([\w-]+)$/) : null;
98
+ const names = options.registeredWidgets ?? KNOWN_SECTION_WIDGETS;
99
+ const knownWidget = widget && ("has" in names ? names.has(widget[1]) : names.includes(widget[1]));
100
+ const widgetChartMatch = marker[0] === "`" && !knownWidget ? tag.match(/^widget:([a-zA-Z-]+)(?:-(?:full|\d+))?(?:\s+.*)?$/i) : null;
101
+ const isWidgetChart = Boolean(widgetChartMatch && KNOWN_QUERY_WIDGET_TYPES[widgetChartMatch[1].toLowerCase()]);
102
+ const query = marker[0] === "`" ? tag.match(/^query(?:[:\s](.*))?$/i) ?? (isWidgetChart ? tag.match(/^widget:?(.*)$/i) : null) : null;
103
+ const type = closed && dialect ? dialect.dialect : closed && knownWidget ? "widget" : closed && query ? "query" : "code";
104
+ const section2 = make(type, line, end2);
105
+ section2.contentFrom = offsets[line] + lines[line].length + separators[line].length;
106
+ section2.contentTo = closed ? offsets[close] : section2.to;
107
+ section2.displayContent = content.slice(section2.contentFrom, section2.contentTo);
108
+ if (dialect && closed) {
109
+ section2.sport = dialect.sport;
110
+ section2.contentId = blockContentId(section2.displayContent);
111
+ section2.scriptBlock = {
112
+ id: section2.id,
113
+ contentId: section2.contentId,
114
+ dialect: dialect.dialect,
115
+ sport: dialect.sport,
116
+ startLine: line,
117
+ endLine: end2,
118
+ content: section2.displayContent,
119
+ state: "idle",
120
+ version: 1,
121
+ createdAt
122
+ };
123
+ }
124
+ if (type === "widget" && widget) section2.widgetName = widget[1];
125
+ if (type === "query" && query) section2.querySuffix = (query[1] ?? "").trim();
126
+ sections.push(section2);
127
+ line = end2 + 1;
128
+ continue;
129
+ }
130
+ if (text === "---" && lines[line + 1]?.trim()) {
131
+ let close = line + 1;
132
+ while (close < lines.length && lines[close].trim() !== "---" && !/^(`{3,}|~{3,})/.test(lines[close].trim())) close++;
133
+ const inner = lines.slice(line + 1, close);
134
+ if (lines[close]?.trim() === "---" && inner.some((value) => /^[A-Za-z_][\w.-]*\s*:/.test(value.trim()))) {
135
+ const section2 = make("frontmatter", line, close);
136
+ section2.contentFrom = offsets[line + 1];
137
+ section2.contentTo = offsets[close];
138
+ section2.displayContent = content.slice(section2.contentFrom, section2.contentTo);
139
+ section2.properties = {};
140
+ for (const value of inner) {
141
+ const pair = value.match(/^([^:]+):\s*(.*)$/);
142
+ if (pair) section2.properties[pair[1].trim()] = pair[2].trim();
143
+ }
144
+ section2.frontmatterType = frontmatterSubtype(section2.properties);
145
+ sections.push(section2);
146
+ line = close + 1;
147
+ continue;
148
+ }
149
+ }
150
+ const embed = text.match(/^(!)?\[([^\]]*)\]\((https?:\/\/[^)\s]+|\/[^)\s]+)\)$/);
151
+ if (embed) {
152
+ const section2 = make("embed", line, line);
153
+ const isImage = !!embed[1];
154
+ section2.embed = { type: isImage ? "image" : /(?:youtube\.com|youtu\.be)/i.test(embed[3]) ? "youtube" : "link", label: embed[2], url: embed[3], isImage };
155
+ sections.push(section2);
156
+ line++;
157
+ continue;
158
+ }
159
+ let end = line;
160
+ if (text) {
161
+ while (end + 1 < lines.length && lines[end + 1].trim() && !/^(?:`{3,}|~{3,}|---\s*$|!?\[[^\]]*\]\()/.test(lines[end + 1].trim())) end++;
162
+ }
163
+ const section = make(text && firstText ? "title" : "markdown", line, end);
164
+ section.subtype = markdownSubtype(section.displayContent.trim());
165
+ if (text) firstText = false;
166
+ sections.push(section);
167
+ line = end + 1;
168
+ }
169
+ return sections;
170
+ }
171
+ function actionableSectionKind(section) {
172
+ if (section.type === "time" || section.type === "log") return "wod";
173
+ if (section.type === "query" || section.type === "widget" || section.type === "embed") return section.type;
174
+ return null;
175
+ }
176
+ function reconcileNoteBlocks(rawContent, options) {
177
+ if (!Number.isSafeInteger(options.sourceRevision) || options.sourceRevision < 1) throw new Error("Invalid source revision");
178
+ const result = /* @__PURE__ */ Object.create(null);
179
+ const lineage = Object.values(options.blocks ?? {}).filter((block) => block && typeof block === "object");
180
+ const createId = options.createId ?? (() => crypto.randomUUID());
181
+ for (const section of parseDocumentSections(rawContent, options)) {
182
+ const kind = actionableSectionKind(section);
183
+ if (!kind) continue;
184
+ const matches = lineage.filter((block) => block.from === section.from && block.to === section.to && block.kind === kind);
185
+ const prior = matches.length === 1 ? matches[0] : void 0;
186
+ const id = prior?.id ?? createId();
187
+ if (!id || Object.hasOwn(result, id)) throw new Error("Duplicate block occurrence ID");
188
+ result[id] = {
189
+ id,
190
+ kind,
191
+ from: section.from,
192
+ to: section.to,
193
+ sourceRevision: options.sourceRevision,
194
+ version: prior?.version ?? 1,
195
+ ...kind === "wod" && (section.type === "time" || section.type === "log") ? { dialect: section.type, ...section.sport ? { sport: section.sport } : {} } : {}
196
+ };
197
+ }
198
+ return result;
199
+ }
200
+
201
+ // src/testing.ts
202
+ var TEST_EPOCH = 17040672e5;
203
+ var EFFORT_VOCABULARY = [];
204
+ var EQUIPMENT_VOCABULARY = [];
205
+ function resetTestingVocabulary() {
206
+ EFFORT_VOCABULARY.length = 0;
207
+ EQUIPMENT_VOCABULARY.length = 0;
208
+ }
209
+ function slugifyText(text) {
210
+ return text.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
211
+ }
212
+ function yamlScalar(value) {
213
+ return /^[A-Za-z0-9][^:#]*$/.test(value) ? value : JSON.stringify(value);
214
+ }
215
+ function yamlMapEntries(record) {
216
+ return Object.entries(record).map(
217
+ ([key, value]) => ` ${key}: ${typeof value === "string" ? yamlScalar(value) : typeof value === "number" || typeof value === "boolean" ? String(value) : JSON.stringify(value)}`
218
+ );
219
+ }
220
+ function yamlListLines(key, items) {
221
+ return items.length > 0 ? [`${key}:`, ...items.map((item) => ` - ${yamlScalar(item)}`)] : [`${key}: []`];
222
+ }
223
+ function mentionPhraseRegex(entry) {
224
+ const alternatives = [...new Set(entry.phrases.map((phrase) => phrase.trim()).filter(Boolean))].map((phrase) => phrase.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|");
225
+ return new RegExp(`(?<![\\w-])(?:${alternatives})(?![\\w-])`, "i");
226
+ }
227
+ function scanMentions(content, vocabulary) {
228
+ const slugs = [];
229
+ for (const entry of vocabulary) {
230
+ if (slugs.includes(entry.slug)) continue;
231
+ if (mentionPhraseRegex(entry).test(content)) slugs.push(entry.slug);
232
+ }
233
+ return slugs;
234
+ }
235
+ function slugToMentionEntry(slug, label, aliases) {
236
+ return { slug, phrases: [slug, label, ...aliases] };
237
+ }
238
+ function buildTestNote(input) {
239
+ const noteId = input.id ?? `note-${input.title ? slugifyText(input.title) : blockContentId(input.rawContent)}`;
240
+ const createdAt = input.createdAt ?? TEST_EPOCH;
241
+ const sourceRevision = input.sourceRevision ?? 1;
242
+ const rawContent = input.rawContent;
243
+ const title = input.title ?? "Untitled";
244
+ let ordinal = 0;
245
+ const blocks = reconcileNoteBlocks(rawContent, {
246
+ sourceRevision,
247
+ blocks: input.priorBlocks,
248
+ createId: () => {
249
+ let candidate = `blk-${ordinal++}`;
250
+ while (input.priorBlocks && Object.hasOwn(input.priorBlocks, candidate)) {
251
+ candidate = `blk-${ordinal++}`;
252
+ }
253
+ return candidate;
254
+ }
255
+ });
256
+ const effectiveFeedOrigin = input.feedOrigin ?? (input.feedSourceId ? "feed" : void 0);
257
+ const effectiveSeedOrigin = input.seedOrigin ?? (effectiveFeedOrigin === "feed" ? "seed" : void 0);
258
+ const sections = parseDocumentSections(rawContent);
259
+ const revisions = [];
260
+ const blockIndex = [];
261
+ const blockEfforts = [];
262
+ const blockEquipment = [];
263
+ const isStatic = effectiveSeedOrigin === "seed";
264
+ for (const [position, block] of Object.values(blocks).entries()) {
265
+ const slice = rawContent.slice(block.from, block.to);
266
+ if (input.priorBlocks?.[block.id]?.version !== block.version) {
267
+ revisions.push({
268
+ noteId,
269
+ blockId: block.id,
270
+ version: block.version,
271
+ kind: block.kind,
272
+ rawContent: slice,
273
+ savedAt: createdAt,
274
+ ...block.dialect ? { dialect: block.dialect } : {},
275
+ ...block.sport ? { sport: block.sport } : {}
276
+ });
277
+ }
278
+ const section = sections.find((s) => s.from === block.from && s.to === block.to);
279
+ const contentId = block.kind === "wod" ? section?.scriptBlock?.contentId ?? blockContentId(slice) : void 0;
280
+ blockIndex.push({
281
+ id: `${noteId}:${block.id}`,
282
+ noteId,
283
+ blockId: block.id,
284
+ blockVersion: block.version,
285
+ sourceRevision,
286
+ from: block.from,
287
+ to: block.to,
288
+ position,
289
+ dataType: block.kind,
290
+ ...contentId ? { blockContentId: contentId } : {},
291
+ rawContent: slice,
292
+ noteTitle: title,
293
+ createdAt,
294
+ isStatic,
295
+ ...input.sourceId ? { sourceId: input.sourceId } : {},
296
+ ...input.sourcePath ? { sourcePath: input.sourcePath } : {},
297
+ ...input.notebookId ? { notebookId: input.notebookId } : {}
298
+ });
299
+ if (block.kind === "wod") {
300
+ const body = section?.scriptBlock?.content ?? slice;
301
+ for (const slug of scanMentions(body, EFFORT_VOCABULARY)) {
302
+ blockEfforts.push({
303
+ id: `${noteId}:${block.id}:${slug}`,
304
+ noteId,
305
+ blockId: block.id,
306
+ ...contentId ? { blockContentId: contentId } : {},
307
+ effortSlug: slug,
308
+ isStatic,
309
+ ...input.sourcePath ? { sourcePath: input.sourcePath } : {},
310
+ createdAt
311
+ });
312
+ }
313
+ for (const slug of scanMentions(body, EQUIPMENT_VOCABULARY)) {
314
+ blockEquipment.push({
315
+ id: `${noteId}:${block.id}:${slug}`,
316
+ noteId,
317
+ blockId: block.id,
318
+ ...contentId ? { blockContentId: contentId } : {},
319
+ equipmentSlug: slug,
320
+ isStatic,
321
+ ...input.sourcePath ? { sourcePath: input.sourcePath } : {},
322
+ createdAt
323
+ });
324
+ }
325
+ }
326
+ }
327
+ const tags = [];
328
+ const noteTags = [];
329
+ for (const label of input.tags ?? []) {
330
+ const tag = { id: `tag:${label}`, label, createdAt };
331
+ if (!tags.some((t) => t.id === tag.id)) tags.push(tag);
332
+ noteTags.push({ id: `${noteId}:${tag.id}`, noteId, tagId: tag.id });
333
+ }
334
+ const notebookIds = input.notebookIds ?? (input.notebookId ? [input.notebookId] : void 0);
335
+ const primaryNotebookId = input.notebookId ?? notebookIds?.[0];
336
+ const notebookLinks = (notebookIds ?? []).map((nbId) => ({
337
+ id: `${nbId}:note:${noteId}`,
338
+ notebookId: nbId,
339
+ targetKind: "note",
340
+ targetId: noteId,
341
+ noteId,
342
+ createdAt
343
+ }));
344
+ const note = {
345
+ id: noteId,
346
+ title,
347
+ rawContent,
348
+ sourceRevision,
349
+ blocks,
350
+ ...input.date !== void 0 ? { date: input.date } : {},
351
+ createdAt,
352
+ ...input.type ? { type: input.type } : {},
353
+ ...input.sourceId ? { sourceId: input.sourceId } : {},
354
+ ...primaryNotebookId ? { notebookId: primaryNotebookId } : {},
355
+ ...notebookIds ? { notebookIds } : {},
356
+ ...input.provenance ? { provenance: input.provenance } : {},
357
+ ...input.tags?.length ? { tags: input.tags } : {},
358
+ ...effectiveSeedOrigin ? { seedOrigin: effectiveSeedOrigin } : {},
359
+ ...effectiveFeedOrigin ? { feedOrigin: effectiveFeedOrigin } : {},
360
+ ...input.feedSourceId ? { feedSourceId: input.feedSourceId } : {},
361
+ ...input.sourcePath ? { sourcePath: input.sourcePath } : {}
362
+ };
363
+ return { note, revisions, blockIndex, blockEfforts, blockEquipment, tags, noteTags, notebookLinks };
364
+ }
365
+ function buildPropertyNote(input) {
366
+ const slug = input.slug.trim().toLowerCase();
367
+ if (!slug) throw new Error("Property note requires a non-empty canonical slug.");
368
+ const kind = input.kind;
369
+ const aliases = input.aliases ?? [];
370
+ const entityId = input.id ?? `${kind}-${slug}`;
371
+ const lines = ["---"];
372
+ lines.push(`id: ${yamlScalar(entityId)}`);
373
+ if (kind === "equipment") lines.push("type: equipment");
374
+ if (kind === "tag-type") lines.push("type: tag-type");
375
+ lines.push(`slug: ${yamlScalar(slug)}`);
376
+ lines.push(`label: ${yamlScalar(input.label)}`);
377
+ lines.push(...yamlListLines("aliases", aliases));
378
+ if (input.metrics && Object.keys(input.metrics).length > 0) {
379
+ lines.push("metrics:", ...yamlMapEntries(input.metrics));
380
+ }
381
+ if (input.summaries && Object.keys(input.summaries).length > 0) {
382
+ lines.push("summaries:", ...yamlMapEntries(input.summaries));
383
+ }
384
+ const baseAttributes = {
385
+ ...kind === "effort" ? { met: 5 } : {},
386
+ ...input.baseAttributes
387
+ };
388
+ if (Object.keys(baseAttributes).length > 0) {
389
+ lines.push("baseAttributes:", ...yamlMapEntries(baseAttributes));
390
+ }
391
+ lines.push("---");
392
+ if (input.body?.trim()) {
393
+ lines.push("", input.body.trim(), "");
394
+ }
395
+ const rawContent = lines.join("\n");
396
+ const createdAt = TEST_EPOCH;
397
+ const seedOrigin = input.seedOrigin ?? "user";
398
+ const notebookId = input.notebookId !== void 0 ? input.notebookId : input.asOverride ? "notebook/custom" : void 0;
399
+ const wantsOverride = input.asOverride === true || notebookId !== void 0 && notebookId !== "notebook/defaults";
400
+ const nbSlug = notebookId !== void 0 && notebookId.startsWith("notebook/") ? notebookId.slice("notebook/".length) : notebookId;
401
+ const ownerNoteId = `override/${nbSlug}/${kind}/${slug}`;
402
+ const note = {
403
+ id: input.id ?? (wantsOverride && notebookId ? ownerNoteId : `property/${kind}/${slug}`),
404
+ title: input.label,
405
+ rawContent,
406
+ sourceRevision: 1,
407
+ blocks: {},
408
+ createdAt,
409
+ seedOrigin,
410
+ ...input.feedSourceId ? { feedSourceId: input.feedSourceId, feedOrigin: "feed" } : {}
411
+ };
412
+ if (kind === "effort") {
413
+ EFFORT_VOCABULARY.push(slugToMentionEntry(slug, input.label, aliases));
414
+ } else if (kind === "equipment") {
415
+ EQUIPMENT_VOCABULARY.push(slugToMentionEntry(slug, input.label, aliases));
416
+ }
417
+ let override;
418
+ if (wantsOverride && notebookId !== void 0) {
419
+ const payload = kind === "effort" ? { id: entityId, slug, label: input.label, aliases, baseAttributes, registrySource: "user", ownerNoteId, sourceId: notebookId, updatedAt: new Date(createdAt).toISOString() } : kind === "equipment" ? { id: entityId, slug, label: input.label, aliases, registrySource: "user", ownerNoteId, sourceId: notebookId, updatedAt: new Date(createdAt).toISOString() } : kind === "tag" ? { id: entityId, label: slug, type: "tag", contributions: [], createdAt } : { id: entityId, name: slug, label: input.label, createdAt };
420
+ override = {
421
+ id: `${notebookId}:${kind}:${slug}`,
422
+ notebookId,
423
+ kind,
424
+ slug,
425
+ label: input.label,
426
+ ownerNoteId,
427
+ rawContent,
428
+ payload,
429
+ seedOrigin,
430
+ ...input.feedSourceId ? { feedSourceId: input.feedSourceId } : {},
431
+ updatedAt: createdAt
432
+ };
433
+ }
434
+ return { note, override };
435
+ }
436
+ function buildTestSession(input) {
437
+ return buildTestSessionAt(input, 1);
438
+ }
439
+ function buildTestSessionAt(input, sequence) {
440
+ const noteId = input.noteId;
441
+ const blockId = input.blockId ?? "blk-0";
442
+ const blockVersion = input.blockVersion ?? 1;
443
+ const startedAt = input.startedAt ?? TEST_EPOCH;
444
+ const durationMs = input.durationMs ?? 36e5;
445
+ const endTime = input.completedAt ?? startedAt + durationMs;
446
+ const session = {
447
+ id: input.id ?? `session-${noteId}:${sequence}`,
448
+ noteId,
449
+ blockId,
450
+ blockVersion,
451
+ origin: input.origin ?? "journal",
452
+ status: "completed",
453
+ startTime: startedAt,
454
+ endTime,
455
+ duration: endTime - startedAt,
456
+ completed: true,
457
+ createdAt: startedAt,
458
+ ...input.isPublic !== void 0 ? { public: input.isPublic } : {},
459
+ ...input.notebookIds ? { notebookIds: input.notebookIds } : {}
460
+ };
461
+ const events = (input.events ?? []).map((event, index) => {
462
+ const record = {
463
+ id: event.id ?? `${session.id}:${index}`,
464
+ resultId: session.id,
465
+ noteId,
466
+ timestamp: startedAt,
467
+ grain: "event",
468
+ outputType: "event",
469
+ metrics: [],
470
+ ...event
471
+ };
472
+ if (record.blockId === void 0) record.blockId = blockId;
473
+ if (record.blockVersion === void 0) record.blockVersion = blockVersion;
474
+ return record;
475
+ });
476
+ const notebookLinks = (input.notebookIds ?? []).map((nbId) => ({
477
+ id: `${nbId}:session:${session.id}`,
478
+ notebookId: nbId,
479
+ targetKind: "session",
480
+ targetId: session.id,
481
+ noteId,
482
+ createdAt: startedAt
483
+ }));
484
+ return { session, events, notebookLinks };
485
+ }
486
+ function buildDomainWorld(input = {}) {
487
+ const builtProperties = (input.propertyNotes ?? []).map(buildPropertyNote);
488
+ const builtNotes = (input.notes ?? []).map(buildTestNote);
489
+ const world = {
490
+ notes: [],
491
+ revisions: [],
492
+ blockIndex: [],
493
+ blockEfforts: [],
494
+ blockEquipment: [],
495
+ tags: [],
496
+ noteTags: [],
497
+ notebooks: [],
498
+ notebookLinks: [],
499
+ propertyOverrides: [],
500
+ pages: input.pages ?? [],
501
+ pageNotes: input.pageNotes ?? [],
502
+ sessions: [],
503
+ events: []
504
+ };
505
+ const firstWodBlockByNote = /* @__PURE__ */ new Map();
506
+ for (const built of builtNotes) {
507
+ world.notes.push(built.note);
508
+ world.revisions.push(...built.revisions);
509
+ world.blockIndex.push(...built.blockIndex);
510
+ world.blockEfforts.push(...built.blockEfforts);
511
+ world.blockEquipment.push(...built.blockEquipment);
512
+ world.tags.push(...built.tags);
513
+ world.noteTags.push(...built.noteTags);
514
+ world.notebookLinks.push(...built.notebookLinks);
515
+ const firstWod = built.blockIndex.find((row) => row.dataType === "wod");
516
+ if (firstWod) {
517
+ firstWodBlockByNote.set(built.note.id, {
518
+ blockId: firstWod.blockId,
519
+ blockVersion: firstWod.blockVersion,
520
+ ...firstWod.blockContentId ? { blockContentId: firstWod.blockContentId } : {}
521
+ });
522
+ }
523
+ }
524
+ for (const { note, override } of builtProperties) {
525
+ world.notes.push(note);
526
+ if (override) {
527
+ world.propertyOverrides.push(override);
528
+ if (!world.notebooks.some((nb) => nb.id === override.notebookId)) world.notebooks.push(notebookLandingNote(override.notebookId));
529
+ }
530
+ }
531
+ let sessionSequence = 0;
532
+ for (const sessionInput of input.sessions ?? []) {
533
+ sessionSequence++;
534
+ const pin = firstWodBlockByNote.get(sessionInput.noteId);
535
+ const built = buildTestSessionAt(
536
+ {
537
+ ...sessionInput,
538
+ ...sessionInput.blockId === void 0 && pin ? { blockId: pin.blockId, blockVersion: pin.blockVersion } : {}
539
+ },
540
+ sessionSequence
541
+ );
542
+ const blockContentId2 = built.session.blockContentId ?? pin?.blockContentId;
543
+ const session = { ...built.session, ...blockContentId2 ? { blockContentId: blockContentId2 } : {} };
544
+ world.sessions.push(session);
545
+ world.events.push(...built.events);
546
+ world.notebookLinks.push(...built.notebookLinks);
547
+ }
548
+ const referencedNotebooks = /* @__PURE__ */ new Set();
549
+ for (const link of world.notebookLinks) referencedNotebooks.add(link.notebookId);
550
+ for (const nbId of referencedNotebooks) {
551
+ if (!world.notebooks.some((nb) => nb.id === nbId)) world.notebooks.push(notebookLandingNote(nbId));
552
+ }
553
+ return world;
554
+ }
555
+ function notebookLandingNote(notebookId) {
556
+ const slug = notebookId.startsWith("notebook/") ? notebookId.slice("notebook/".length) : notebookId;
557
+ return {
558
+ id: notebookId,
559
+ title: slug.charAt(0).toUpperCase() + slug.slice(1),
560
+ rawContent: "",
561
+ sourceRevision: 1,
562
+ blocks: {},
563
+ createdAt: TEST_EPOCH
564
+ };
565
+ }
566
+
567
+ export { TEST_EPOCH, buildDomainWorld, buildPropertyNote, buildTestNote, buildTestSession, resetTestingVocabulary };
568
+ //# sourceMappingURL=testing.mjs.map
569
+ //# sourceMappingURL=testing.mjs.map