@wirecat/cli-core 0.699.0 → 0.701.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/CHANGELOG.md +57 -0
- package/dist/cli/documents/words.d.ts +1 -1
- package/dist/cli/service-api.js +19 -6
- package/dist/crm/manifest/manifest.d.ts +35 -2
- package/dist/crm/manifest/manifest.js +26 -3
- package/dist/crm/manifest/map.d.ts +4 -1
- package/dist/crm/manifest/map.js +39 -1
- package/dist/crm/manifest/paths.js +7 -4
- package/dist/crm/manifest/source.js +70 -8
- package/dist/crm/testing/manifest-fake.js +96 -41
- package/dist/documents/html-markdown.d.ts +11 -0
- package/dist/documents/html-markdown.js +81 -0
- package/dist/documents/index.d.ts +1 -0
- package/dist/documents/index.js +1 -0
- package/dist/store/sqlite/vectors.js +11 -4
- package/dist/tracker/manifest/manifest.d.ts +2 -0
- package/dist/tracker/manifest/manifest.js +1 -1
- package/dist/tracker/testing/index.d.ts +1 -0
- package/dist/tracker/testing/index.js +1 -0
- package/dist/tracker/testing/manifest-fake.d.ts +51 -0
- package/dist/tracker/testing/manifest-fake.js +450 -0
- package/package.json +8 -3
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import { graphqlFake, sampleOf, } from "@wirecat/cli-base/codegen";
|
|
2
|
+
import { serviceApiCall } from "../../cli/service-api-call.js";
|
|
3
|
+
import { manifestBaseUrl } from "../../cli/service-api-manifest.js";
|
|
4
|
+
import { crmValues } from "../../crm/index.js";
|
|
5
|
+
import { at, evaluate, MISSING, rulePaths } from "../../crm/manifest/map.js";
|
|
6
|
+
import { resolve, step } from "../../crm/manifest/paths.js";
|
|
7
|
+
import { FAKE_MANIFEST_TOKEN } from "../../crm/testing/index.js";
|
|
8
|
+
import { STATE_TYPES } from "../../domain/tracker.js";
|
|
9
|
+
import { parseTrackerManifest, rowSelection, selectionOf, } from "../manifest/manifest.js";
|
|
10
|
+
import { TRACKER_FIELDS } from "../manifest/map.js";
|
|
11
|
+
import { trackerSourceFromManifest } from "../manifest/source.js";
|
|
12
|
+
import { trackerSourceContract } from "./source-contract.js";
|
|
13
|
+
/** Three rows in pages of two: every kind pages at least once, whatever `first` the manifest asks for. */
|
|
14
|
+
const ROWS = 3;
|
|
15
|
+
const PAGE = 2;
|
|
16
|
+
const SEEDED = Date.parse("2026-11-01T10:00:00Z");
|
|
17
|
+
/** Linear's argument; a call to an operation that takes it and leaves it unset sees no archived row. */
|
|
18
|
+
const ARCHIVED = "includeArchived";
|
|
19
|
+
const CURSOR = /^fake-page-(\d+)$/;
|
|
20
|
+
/** The kind an id field points at: an issue's assignee is a user, its labels are labels. */
|
|
21
|
+
const TARGETS = {
|
|
22
|
+
team: "team",
|
|
23
|
+
teams: "team",
|
|
24
|
+
state: "state",
|
|
25
|
+
label: "label",
|
|
26
|
+
labels: "label",
|
|
27
|
+
project: "project",
|
|
28
|
+
lead: "user",
|
|
29
|
+
assignee: "user",
|
|
30
|
+
creator: "user",
|
|
31
|
+
author: "user",
|
|
32
|
+
issue: "issue",
|
|
33
|
+
parent: "issue",
|
|
34
|
+
};
|
|
35
|
+
const SINCE = {
|
|
36
|
+
iso: (value) => Date.parse(value),
|
|
37
|
+
ms: (value) => (/^\d+$/.test(value) ? Number(value) : Number.NaN),
|
|
38
|
+
seconds: (value) => (/^\d+$/.test(value) ? Number(value) * 1000 : Number.NaN),
|
|
39
|
+
};
|
|
40
|
+
const idOf = (kind, n) => `${kind}-${n + 1}`;
|
|
41
|
+
const merge = (a, b) => Object.fromEntries([...new Set([...Object.keys(a), ...Object.keys(b)])].map((key) => {
|
|
42
|
+
const [x, y] = [a[key], b[key]];
|
|
43
|
+
if (x === undefined || x === true)
|
|
44
|
+
return [key, y ?? x ?? true];
|
|
45
|
+
return [key, y === undefined || y === true ? x : merge(x, y)];
|
|
46
|
+
}));
|
|
47
|
+
const project = (value, selection) => {
|
|
48
|
+
if (selection === true || value === null || value === undefined)
|
|
49
|
+
return value ?? null;
|
|
50
|
+
if (Array.isArray(value))
|
|
51
|
+
return value.map((one) => project(one, selection));
|
|
52
|
+
return Object.fromEntries(Object.entries(selection).map(([field, inner]) => [field, project(value[field], inner)]));
|
|
53
|
+
};
|
|
54
|
+
const fixedOf = (value, trail = []) => crmValues.isRaw(value)
|
|
55
|
+
? Object.entries(value).flatMap(([key, inner]) => fixedOf(inner, [...trail, key]))
|
|
56
|
+
: /^\{\w+\}$/.test(String(value))
|
|
57
|
+
? []
|
|
58
|
+
: [[trail.join("."), value]];
|
|
59
|
+
const placeOf = (value, placeholder, trail = []) => {
|
|
60
|
+
if (value === placeholder)
|
|
61
|
+
return trail.join(".");
|
|
62
|
+
if (!crmValues.isRaw(value))
|
|
63
|
+
return undefined;
|
|
64
|
+
for (const [key, inner] of Object.entries(value)) {
|
|
65
|
+
const found = placeOf(inner, placeholder, [...trail, key]);
|
|
66
|
+
if (found !== undefined)
|
|
67
|
+
return found;
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
};
|
|
71
|
+
const put = (row, path, value) => {
|
|
72
|
+
const keys = path.split(".");
|
|
73
|
+
const last = keys.pop();
|
|
74
|
+
let here = row;
|
|
75
|
+
for (const key of keys) {
|
|
76
|
+
if (!crmValues.isRaw(here[key]))
|
|
77
|
+
here[key] = {};
|
|
78
|
+
here = here[key];
|
|
79
|
+
}
|
|
80
|
+
here[last] = value;
|
|
81
|
+
};
|
|
82
|
+
/** The only filter the fake applies: a since on the change time. Any other would pass rows the tracker drops. */
|
|
83
|
+
const FILTER = "filter";
|
|
84
|
+
/**
|
|
85
|
+
* A GraphQL tracker in memory, built from a source manifest and the generated API it names: cli-base's strict
|
|
86
|
+
* `graphqlFake` with three rows of each kind behind it. Each row holds the values the map reads where it reads them,
|
|
87
|
+
* links to rows of the kinds it points at, and is answered within each call's selection, Relay-paged two a page. A
|
|
88
|
+
* change read keeps the rows changed at or after its since, oldest first. A cursor the fake never gave, an unreadable
|
|
89
|
+
* since and a call no route of the manifest makes are refused and recorded.
|
|
90
|
+
*/
|
|
91
|
+
export const trackerManifestFake = (input, options) => {
|
|
92
|
+
const { api, definitions } = options;
|
|
93
|
+
const manifest = parseTrackerManifest(input, api);
|
|
94
|
+
const operations = new Map(api.operations.map((one) => [one.id, one]));
|
|
95
|
+
const own = [];
|
|
96
|
+
const operationOf = (call) => {
|
|
97
|
+
const found = operations.get(call.operation);
|
|
98
|
+
if (found?.binding.kind !== "graphql")
|
|
99
|
+
throw new Error(`${call.operation}: the tracker fake answers GraphQL operations only`);
|
|
100
|
+
return found;
|
|
101
|
+
};
|
|
102
|
+
const itemsOf = (call) => call.items ?? operationOf(call).pagination?.items;
|
|
103
|
+
const responseOf = (call) => {
|
|
104
|
+
const schema = operationOf(call).response?.schema;
|
|
105
|
+
return schema === undefined ? { type: "unknown" } : { type: "ref", ref: schema };
|
|
106
|
+
};
|
|
107
|
+
const nodeAt = (node, path) => (path?.split(".") ?? []).reduce((inner, key) => {
|
|
108
|
+
const found = inner && step(inner, key, definitions);
|
|
109
|
+
const resolved = found ? resolve(found, definitions) : undefined;
|
|
110
|
+
return resolved?.type === "array" ? resolved.items : (found ?? undefined);
|
|
111
|
+
}, node);
|
|
112
|
+
const sampled = (node, selection) => {
|
|
113
|
+
if (selection === true)
|
|
114
|
+
return sampleOf(node, definitions);
|
|
115
|
+
const found = resolve(node, definitions);
|
|
116
|
+
if (found.type === "array")
|
|
117
|
+
return [sampled(found.items, selection)];
|
|
118
|
+
return Object.fromEntries(Object.entries(selection).map(([name, inner]) => [
|
|
119
|
+
name,
|
|
120
|
+
sampled(step(found, name, definitions) ?? { type: "unknown" }, inner),
|
|
121
|
+
]));
|
|
122
|
+
};
|
|
123
|
+
/** A value as the schema at its place takes it: a time as ISO text or epoch ms; `undefined` when it cannot. */
|
|
124
|
+
const typed = (node, value) => {
|
|
125
|
+
const found = node && resolve(node, definitions);
|
|
126
|
+
if (value === null)
|
|
127
|
+
return found === undefined || node?.nullable || found.nullable ? null : undefined;
|
|
128
|
+
// A text the schema keeps as an enum (Linear's project status type) is one of its values.
|
|
129
|
+
if (typeof value === "string" && found?.type === "string" && found.enum && !found.enum.includes(value))
|
|
130
|
+
return found.enum[0];
|
|
131
|
+
if (typeof value !== "object")
|
|
132
|
+
return value;
|
|
133
|
+
return found?.type === "integer" || found?.type === "number" ? value.ms : new Date(value.ms).toISOString();
|
|
134
|
+
};
|
|
135
|
+
const write = (row, item, path, value) => {
|
|
136
|
+
const written = typed(nodeAt(item, path), value);
|
|
137
|
+
if (written !== undefined)
|
|
138
|
+
put(row, path, written);
|
|
139
|
+
};
|
|
140
|
+
const pointed = (field) => {
|
|
141
|
+
const target = TARGETS[field];
|
|
142
|
+
return target && manifest.kinds.includes(target) ? idOf(target, 0) : `${field}-1`;
|
|
143
|
+
};
|
|
144
|
+
const sampleValue = (kind, field, n) => {
|
|
145
|
+
const type = TRACKER_FIELDS[kind][field];
|
|
146
|
+
if (field === "externalId")
|
|
147
|
+
return idOf(kind, n);
|
|
148
|
+
// A state of a type outside STATE_TYPES is dropped by the map, and its issues with it.
|
|
149
|
+
if (kind === "state" && field === "type")
|
|
150
|
+
return STATE_TYPES[n % STATE_TYPES.length];
|
|
151
|
+
switch (type) {
|
|
152
|
+
case "epoch":
|
|
153
|
+
return field === "createdAt" || field === "updatedAt" ? { ms: SEEDED + n * 60_000 } : null;
|
|
154
|
+
case "id":
|
|
155
|
+
return pointed(field);
|
|
156
|
+
case "number":
|
|
157
|
+
return n + 1;
|
|
158
|
+
case "bool":
|
|
159
|
+
return true;
|
|
160
|
+
case "day":
|
|
161
|
+
return `2026-11-0${n + 1}`;
|
|
162
|
+
default:
|
|
163
|
+
return `${field} ${n + 1}`;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
const timePath = (kind, field) => {
|
|
167
|
+
const rule = manifest.entities[kind]?.map[field];
|
|
168
|
+
return typeof rule === "string" ? rule : typeof rule === "object" && "path" in rule ? rule.path : undefined;
|
|
169
|
+
};
|
|
170
|
+
const items = new Map();
|
|
171
|
+
const rows = {};
|
|
172
|
+
for (const kind of manifest.kinds) {
|
|
173
|
+
const entity = manifest.entities[kind];
|
|
174
|
+
const calls = [entity.list, entity.changes, entity.ids].filter((one) => one !== undefined);
|
|
175
|
+
const selection = calls
|
|
176
|
+
.map((call) => rowSelection(selectionOf(call, api) ?? {}, itemsOf(call)) ?? {})
|
|
177
|
+
.reduce((all, one) => merge(all, one), {});
|
|
178
|
+
const item = nodeAt(responseOf(entity.list), itemsOf(entity.list));
|
|
179
|
+
items.set(kind, item);
|
|
180
|
+
if (entity.changes && timePath(kind, "updatedAt") === undefined)
|
|
181
|
+
throw new Error(`${kind}: map updatedAt to a path, so the fake can read a change time`);
|
|
182
|
+
rows[kind] = Array.from({ length: ROWS }, (_, n) => {
|
|
183
|
+
const made = item ? sampled(item, selection) : {};
|
|
184
|
+
const row = crmValues.isRaw(made) ? made : {};
|
|
185
|
+
for (const [field, rule] of Object.entries(entity.map)) {
|
|
186
|
+
if (typeof rule === "function" || (typeof rule === "object" && "value" in rule && !("each" in rule)))
|
|
187
|
+
continue;
|
|
188
|
+
const [first, inside] = rulePaths(rule);
|
|
189
|
+
if (!first)
|
|
190
|
+
continue;
|
|
191
|
+
if (inside?.inside !== undefined) {
|
|
192
|
+
const one = {};
|
|
193
|
+
put(one, inside.path, pointed(field));
|
|
194
|
+
put(row, first.path, [one]);
|
|
195
|
+
}
|
|
196
|
+
else if (typeof rule === "object" && "map" in rule)
|
|
197
|
+
put(row, first.path, Object.keys(rule.map)[0] ?? null);
|
|
198
|
+
else
|
|
199
|
+
write(row, item, first.path, sampleValue(kind, field, n));
|
|
200
|
+
}
|
|
201
|
+
return row;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
const filterProblem = (kind, call, place) => {
|
|
205
|
+
const filter = fixedOf(call.variables?.[FILTER] ?? {});
|
|
206
|
+
if (filter.length > 0)
|
|
207
|
+
return `${call.operation}: the fake cannot apply ${FILTER}.${filter[0]?.[0]}, so it would list rows the tracker drops`;
|
|
208
|
+
if (place === undefined)
|
|
209
|
+
return undefined;
|
|
210
|
+
const time = timePath(kind, "updatedAt");
|
|
211
|
+
const ends = [`${time}.gte`, `${time}.gt`];
|
|
212
|
+
if (!ends.some((end) => place === end || place.endsWith(`.${end}`)))
|
|
213
|
+
return `${call.operation}: {since} at ${place} is not ${time}.gte or ${time}.gt, the change time the map reads`;
|
|
214
|
+
return undefined;
|
|
215
|
+
};
|
|
216
|
+
const routes = manifest.kinds.flatMap((kind) => {
|
|
217
|
+
const entity = manifest.entities[kind];
|
|
218
|
+
const plain = (call) => {
|
|
219
|
+
const problem = call.variables && FILTER in call.variables
|
|
220
|
+
? `${call.operation}: a list or id read with a ${FILTER} leaves rows out, and sync would mark them deleted`
|
|
221
|
+
: undefined;
|
|
222
|
+
return { kind, call, ...(problem ? { problem } : {}) };
|
|
223
|
+
};
|
|
224
|
+
const changes = entity.changes;
|
|
225
|
+
const place = changes && placeOf(changes.variables, "{since}");
|
|
226
|
+
const problem = changes && filterProblem(kind, changes, place);
|
|
227
|
+
return [
|
|
228
|
+
plain(entity.list),
|
|
229
|
+
...(entity.ids ? [plain(entity.ids)] : []),
|
|
230
|
+
...(changes && place
|
|
231
|
+
? [
|
|
232
|
+
{
|
|
233
|
+
kind,
|
|
234
|
+
call: changes,
|
|
235
|
+
since: { place, format: changes.since, after: place.endsWith(".gt") },
|
|
236
|
+
...(problem ? { problem } : {}),
|
|
237
|
+
},
|
|
238
|
+
]
|
|
239
|
+
: []),
|
|
240
|
+
];
|
|
241
|
+
});
|
|
242
|
+
/** A page's size and place are the engine's to set, whatever the template says. */
|
|
243
|
+
const paging = (operation, key) => {
|
|
244
|
+
const pagination = operations.get(operation)?.pagination;
|
|
245
|
+
return pagination?.style === "cursor" && [pagination.cursor.name, pagination.limit?.name].includes(key);
|
|
246
|
+
};
|
|
247
|
+
const fixed = (route) => fixedOf(route.call.variables ?? {}).filter(([key]) => !paging(route.call.operation, key));
|
|
248
|
+
const matches = (route, call) => route.call.operation === call.operation &&
|
|
249
|
+
fixed(route).every(([key, value]) => JSON.stringify(at(call.variables, key)) === JSON.stringify(value)) &&
|
|
250
|
+
(!route.since || at(call.variables, route.since.place) !== undefined);
|
|
251
|
+
/** A change read wins over a list when its since was sent; else the route that fixes the most values. */
|
|
252
|
+
const routeOf = (call) => routes
|
|
253
|
+
.filter((route) => matches(route, call))
|
|
254
|
+
.sort((a, b) => Number(!!b.since) - Number(!!a.since) || fixed(b).length - fixed(a).length)[0];
|
|
255
|
+
const find = (kind, id) => {
|
|
256
|
+
const found = rows[kind]?.find((row) => externalIdOf(manifest, kind, row) === id);
|
|
257
|
+
if (!found)
|
|
258
|
+
throw new Error(`the fake holds no ${kind} ${id}`);
|
|
259
|
+
return found;
|
|
260
|
+
};
|
|
261
|
+
const timeOf = (kind, row, field) => {
|
|
262
|
+
const path = timePath(kind, field);
|
|
263
|
+
const value = path === undefined ? null : at(row, path);
|
|
264
|
+
return typeof value === "number" ? value : typeof value === "string" ? Date.parse(value) : null;
|
|
265
|
+
};
|
|
266
|
+
const stamp = (kind, row, field) => {
|
|
267
|
+
const path = timePath(kind, field);
|
|
268
|
+
if (path === undefined)
|
|
269
|
+
throw new Error(`${kind}: map ${field} to a path, so the fake can write it`);
|
|
270
|
+
write(row, items.get(kind), path, { ms: Date.now() });
|
|
271
|
+
};
|
|
272
|
+
const refuse = (message) => {
|
|
273
|
+
own.push(message);
|
|
274
|
+
return { status: 400, errors: [{ message, extensions: { code: "FAKE_REFUSED" } }] };
|
|
275
|
+
};
|
|
276
|
+
const answer = (call) => {
|
|
277
|
+
const route = routeOf(call);
|
|
278
|
+
if (!route)
|
|
279
|
+
return refuse(`${call.operation}: no call of the source manifest sends these variables`);
|
|
280
|
+
if (route.problem)
|
|
281
|
+
return refuse(route.problem);
|
|
282
|
+
const { kind } = route;
|
|
283
|
+
const operation = operationOf(route.call);
|
|
284
|
+
const hidesArchived = operation.binding.kind === "graphql" && ARCHIVED in operation.binding.variables;
|
|
285
|
+
let listed = (rows[kind] ?? []).filter((row) => !hidesArchived || call.variables[ARCHIVED] === true || !Number.isFinite(timeOf(kind, row, "archivedAt") ?? NaN));
|
|
286
|
+
if (route.since) {
|
|
287
|
+
const sent = String(at(call.variables, route.since.place));
|
|
288
|
+
const from = SINCE[route.since.format](sent);
|
|
289
|
+
if (!Number.isFinite(from))
|
|
290
|
+
return refuse(`${call.operation}: since ${sent} is unreadable`);
|
|
291
|
+
listed = listed
|
|
292
|
+
.filter((row) => {
|
|
293
|
+
const time = timeOf(kind, row, "updatedAt") ?? Number.NEGATIVE_INFINITY;
|
|
294
|
+
return route.since?.after ? time > from : time >= from;
|
|
295
|
+
})
|
|
296
|
+
.sort((a, b) => (timeOf(kind, a, "updatedAt") ?? 0) - (timeOf(kind, b, "updatedAt") ?? 0));
|
|
297
|
+
}
|
|
298
|
+
const selection = call.selection ?? {};
|
|
299
|
+
const where = itemsOf(route.call);
|
|
300
|
+
const inner = rowSelection(selection, where) ?? true;
|
|
301
|
+
const pagination = operation.pagination;
|
|
302
|
+
if (where === undefined)
|
|
303
|
+
return { data: listed.map((row) => project(row, inner)) };
|
|
304
|
+
const made = sampled(responseOf(route.call), selection);
|
|
305
|
+
const body = crmValues.isRaw(made) ? made : {};
|
|
306
|
+
if (pagination && pagination.style !== "cursor")
|
|
307
|
+
throw new Error(`${call.operation}: the tracker fake pages by a cursor only, not by ${pagination.style}`);
|
|
308
|
+
if (!pagination) {
|
|
309
|
+
put(body, where, listed.map((row) => project(row, inner)));
|
|
310
|
+
return { data: project(body, selection) };
|
|
311
|
+
}
|
|
312
|
+
const sent = call.variables[pagination.cursor.name];
|
|
313
|
+
const offset = sent === undefined || sent === null ? 0 : Number(CURSOR.exec(String(sent))?.[1] ?? Number.NaN);
|
|
314
|
+
if (!Number.isSafeInteger(offset))
|
|
315
|
+
return refuse(`${call.operation}: ${String(sent)} is not a cursor this fake gave`);
|
|
316
|
+
const limit = pagination.limit ? Number(call.variables[pagination.limit.name]) : Number.NaN;
|
|
317
|
+
const size = Number.isSafeInteger(limit) && limit > 0 ? Math.min(limit, PAGE) : PAGE;
|
|
318
|
+
const more = offset + size < listed.length;
|
|
319
|
+
put(body, where, listed.slice(offset, offset + size).map((row) => project(row, inner)));
|
|
320
|
+
put(body, pagination.next, more ? `fake-page-${offset + size}` : null);
|
|
321
|
+
if (pagination.hasMore)
|
|
322
|
+
put(body, pagination.hasMore, more);
|
|
323
|
+
return { data: project(body, selection) };
|
|
324
|
+
};
|
|
325
|
+
const scheme = Object.values(api.securitySchemes ?? {})[0];
|
|
326
|
+
const fake = graphqlFake({
|
|
327
|
+
operations: api.operations,
|
|
328
|
+
definitions,
|
|
329
|
+
url: options.baseUrl ?? manifestBaseUrl(api),
|
|
330
|
+
...(scheme
|
|
331
|
+
? {
|
|
332
|
+
auth: scheme.type === "apiKey" && scheme.in === "header" && scheme.name
|
|
333
|
+
? { header: scheme.name, value: FAKE_MANIFEST_TOKEN }
|
|
334
|
+
: { header: "Authorization", value: `Bearer ${FAKE_MANIFEST_TOKEN}` },
|
|
335
|
+
}
|
|
336
|
+
: {}),
|
|
337
|
+
answers: Object.fromEntries(routes.map((route) => [route.call.operation, answer])),
|
|
338
|
+
});
|
|
339
|
+
return {
|
|
340
|
+
// An answer of the fake's own that breaks the schema throws inside fetch, which the source reports as a network
|
|
341
|
+
// error; the refusal names it.
|
|
342
|
+
fetch: (input, init) => fake.fetch(input, init).catch((error) => {
|
|
343
|
+
own.push(error instanceof Error ? error.message : String(error));
|
|
344
|
+
throw error;
|
|
345
|
+
}),
|
|
346
|
+
calls: fake.calls,
|
|
347
|
+
get refusals() {
|
|
348
|
+
return [...fake.refusals, ...own];
|
|
349
|
+
},
|
|
350
|
+
rows,
|
|
351
|
+
touch: (kind, id) => stamp(kind, find(kind, id), "updatedAt"),
|
|
352
|
+
archive(kind, id) {
|
|
353
|
+
const row = find(kind, id);
|
|
354
|
+
stamp(kind, row, "archivedAt");
|
|
355
|
+
stamp(kind, row, "updatedAt");
|
|
356
|
+
},
|
|
357
|
+
remove(kind, id) {
|
|
358
|
+
const row = find(kind, id);
|
|
359
|
+
rows[kind]?.splice(rows[kind].indexOf(row), 1);
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
};
|
|
363
|
+
const externalIdOf = (manifest, kind, row) => {
|
|
364
|
+
const rule = manifest.entities[kind]?.map.externalId;
|
|
365
|
+
const found = rule === undefined ? MISSING : evaluate(rule, row);
|
|
366
|
+
return found === MISSING ? null : crmValues.id(found);
|
|
367
|
+
};
|
|
368
|
+
/**
|
|
369
|
+
* A `trackerSourceContract` fixture for a source built from a manifest, over `trackerManifestFake`: the source, the
|
|
370
|
+
* rows it holds, a dead cursor, the faults the manifest allows, and the fake itself, whose `refusals` a test asserts
|
|
371
|
+
* empty.
|
|
372
|
+
*/
|
|
373
|
+
export const trackerManifestFixture = (manifest, options) => {
|
|
374
|
+
const fake = trackerManifestFake(manifest, options);
|
|
375
|
+
const parsed = parseTrackerManifest(manifest, options.api);
|
|
376
|
+
const call = serviceApiCall({
|
|
377
|
+
manifest: options.api,
|
|
378
|
+
service: parsed.provider,
|
|
379
|
+
fetch: fake.fetch,
|
|
380
|
+
token: () => FAKE_MANIFEST_TOKEN,
|
|
381
|
+
...(options.baseUrl ? { baseUrl: options.baseUrl } : {}),
|
|
382
|
+
});
|
|
383
|
+
const sourceAt = (now = Date.now) => trackerSourceFromManifest(manifest, {
|
|
384
|
+
api: options.api,
|
|
385
|
+
call,
|
|
386
|
+
now,
|
|
387
|
+
...(options.hooks ? { hooks: options.hooks } : {}),
|
|
388
|
+
});
|
|
389
|
+
const held = Object.fromEntries(parsed.kinds.map((kind) => [kind, (fake.rows[kind] ?? []).map((row) => externalIdOf(parsed, kind, row))]));
|
|
390
|
+
// An issue first: the kind a tracker's changes matter most for. Its last row is the one archived or removed, which
|
|
391
|
+
// no other row points at.
|
|
392
|
+
const changed = ["issue", ...[...parsed.kinds].reverse()].find((kind) => parsed.kinds.includes(kind) && parsed.entities[kind]?.changes);
|
|
393
|
+
const archivable = changed && parsed.entities[changed]?.map.archivedAt !== undefined;
|
|
394
|
+
const first = (kind) => held[kind]?.[0];
|
|
395
|
+
const last = (kind) => held[kind]?.at(-1);
|
|
396
|
+
return {
|
|
397
|
+
source: sourceAt(),
|
|
398
|
+
fake,
|
|
399
|
+
held,
|
|
400
|
+
deadCursor: { kind: parsed.kinds[0], cursor: "not a time" },
|
|
401
|
+
faults: {
|
|
402
|
+
remove: () => {
|
|
403
|
+
const kind = changed ?? parsed.kinds.at(-1);
|
|
404
|
+
fake.remove(kind, last(kind));
|
|
405
|
+
return { kind, id: last(kind) };
|
|
406
|
+
},
|
|
407
|
+
...(changed
|
|
408
|
+
? {
|
|
409
|
+
skewed: (ms) => sourceAt(() => Date.now() + ms),
|
|
410
|
+
change: () => {
|
|
411
|
+
fake.touch(changed, first(changed));
|
|
412
|
+
return { kind: changed, id: first(changed) };
|
|
413
|
+
},
|
|
414
|
+
}
|
|
415
|
+
: {}),
|
|
416
|
+
...(changed && archivable
|
|
417
|
+
? {
|
|
418
|
+
archive: () => {
|
|
419
|
+
fake.archive(changed, last(changed));
|
|
420
|
+
return { kind: changed, id: last(changed) };
|
|
421
|
+
},
|
|
422
|
+
}
|
|
423
|
+
: {}),
|
|
424
|
+
},
|
|
425
|
+
};
|
|
426
|
+
};
|
|
427
|
+
/**
|
|
428
|
+
* `trackerSourceContract` over `trackerManifestFixture`, each case failing too when the fake refused a call: what a
|
|
429
|
+
* generated test runs, `for (const { name, run } of trackerManifestContract(SOURCE, { api, definitions })) it(name, run)`.
|
|
430
|
+
*/
|
|
431
|
+
export const trackerManifestContract = (manifest, options) => {
|
|
432
|
+
const fakes = [];
|
|
433
|
+
return trackerSourceContract(() => {
|
|
434
|
+
const fixture = trackerManifestFixture(manifest, options);
|
|
435
|
+
fakes.push(fixture.fake);
|
|
436
|
+
return fixture;
|
|
437
|
+
}).map(({ name, run }) => ({
|
|
438
|
+
name,
|
|
439
|
+
async run() {
|
|
440
|
+
fakes.length = 0;
|
|
441
|
+
const failed = await run().then(() => undefined, (error) => error);
|
|
442
|
+
// A refused call fails the source too; the refusal says why, its error only that the tracker said no.
|
|
443
|
+
const refused = fakes.flatMap((fake) => fake.refusals);
|
|
444
|
+
if (refused.length)
|
|
445
|
+
throw new Error(`the fake refused calls the source made:\n${refused.join("\n")}`, { cause: failed });
|
|
446
|
+
if (failed)
|
|
447
|
+
throw failed;
|
|
448
|
+
},
|
|
449
|
+
}));
|
|
450
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wirecat/cli-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.701.0",
|
|
4
4
|
"description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Viacheslav Ptsarev",
|
|
@@ -277,6 +277,9 @@
|
|
|
277
277
|
"disposable-email-domains-js": "1.26.0",
|
|
278
278
|
"echarts": "6.1.0",
|
|
279
279
|
"fflate": "0.8.3",
|
|
280
|
+
"hast-util-from-html": "2.1.0",
|
|
281
|
+
"hast-util-to-html": "9.0.6",
|
|
282
|
+
"hast-util-to-mdast": "10.1.3",
|
|
280
283
|
"iconv-lite": "0.7.3",
|
|
281
284
|
"image-size": "2.0.4",
|
|
282
285
|
"linkedom": "0.18.14",
|
|
@@ -285,6 +288,8 @@
|
|
|
285
288
|
"mammoth": "1.13.0",
|
|
286
289
|
"mdast-util-from-markdown": "2.1.0",
|
|
287
290
|
"mdast-util-gfm": "3.1.0",
|
|
291
|
+
"mdast-util-to-hast": "13.2.2",
|
|
292
|
+
"mdast-util-to-markdown": "2.2.0",
|
|
288
293
|
"micromark-extension-gfm": "3.0.0",
|
|
289
294
|
"mpg123-decoder": "1.0.3",
|
|
290
295
|
"ogg-opus-decoder": "1.7.5",
|
|
@@ -313,7 +318,7 @@
|
|
|
313
318
|
"@types/pngjs": "6.0.5",
|
|
314
319
|
"@types/ssh2": "1.15.5",
|
|
315
320
|
"@vitest/coverage-v8": "^5.0.5",
|
|
316
|
-
"@wirecat/cli-base": "0.
|
|
321
|
+
"@wirecat/cli-base": "0.118.0",
|
|
317
322
|
"@wirecat/cli-core-legacy-tags": "npm:@wirecat/cli-core@0.414.0",
|
|
318
323
|
"ajv": "8.17.1",
|
|
319
324
|
"commander": "15.0.0",
|
|
@@ -337,7 +342,7 @@
|
|
|
337
342
|
},
|
|
338
343
|
"peerDependencies": {
|
|
339
344
|
"@napi-rs/canvas": ">=0.1.69 <2",
|
|
340
|
-
"@wirecat/cli-base": ">=0.
|
|
345
|
+
"@wirecat/cli-base": ">=0.118.0 <1.0.0",
|
|
341
346
|
"commander": ">=15",
|
|
342
347
|
"mysql2": "^3.24.6",
|
|
343
348
|
"pg": "^8.23.1",
|