@principal-ai/principal-view-core 0.27.2 → 0.28.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/dist/events/EventsCanvasValidator.d.ts +13 -0
- package/dist/events/EventsCanvasValidator.d.ts.map +1 -1
- package/dist/events/EventsCanvasValidator.js +70 -0
- package/dist/events/EventsCanvasValidator.js.map +1 -1
- package/dist/events/NamespacePathIndex.d.ts +61 -0
- package/dist/events/NamespacePathIndex.d.ts.map +1 -0
- package/dist/events/NamespacePathIndex.js +74 -0
- package/dist/events/NamespacePathIndex.js.map +1 -0
- package/dist/events/OtelEventPathsValidator.d.ts +75 -0
- package/dist/events/OtelEventPathsValidator.d.ts.map +1 -0
- package/dist/events/OtelEventPathsValidator.js +126 -0
- package/dist/events/OtelEventPathsValidator.js.map +1 -0
- package/dist/events/index.d.ts +4 -0
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +5 -1
- package/dist/events/index.js.map +1 -1
- package/dist/events/path-helpers.d.ts +33 -0
- package/dist/events/path-helpers.d.ts.map +1 -0
- package/dist/events/path-helpers.js +59 -0
- package/dist/events/path-helpers.js.map +1 -0
- package/package.json +1 -1
- package/src/events/EventsCanvasValidator.test.ts +190 -0
- package/src/events/EventsCanvasValidator.ts +86 -0
- package/src/events/NamespacePathIndex.test.ts +97 -0
- package/src/events/NamespacePathIndex.ts +100 -0
- package/src/events/OtelEventPathsValidator.test.ts +243 -0
- package/src/events/OtelEventPathsValidator.ts +191 -0
- package/src/events/index.ts +13 -0
- package/src/events/path-helpers.ts +53 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Path helpers shared across events-canvas validators.
|
|
4
|
+
*
|
|
5
|
+
* Paths declared in canvases (namespace `paths`, `otel.files`, etc.) are
|
|
6
|
+
* always repo-relative. These helpers give a consistent notion of
|
|
7
|
+
* canonicalization and prefix-based containment so that separate validators
|
|
8
|
+
* don't drift.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.pathsOverlap = exports.pathCovers = exports.isPathDescendant = exports.normalizePath = void 0;
|
|
12
|
+
/**
|
|
13
|
+
* Canonicalize a declared path for comparison.
|
|
14
|
+
* Strips leading `./`, trailing slashes, and collapses duplicate slashes.
|
|
15
|
+
*/
|
|
16
|
+
function normalizePath(p) {
|
|
17
|
+
return p
|
|
18
|
+
.replace(/^\.\//, '')
|
|
19
|
+
.replace(/\/+$/, '')
|
|
20
|
+
.replace(/\/+/g, '/');
|
|
21
|
+
}
|
|
22
|
+
exports.normalizePath = normalizePath;
|
|
23
|
+
/**
|
|
24
|
+
* True when `child` is strictly nested under `parent` (not equal).
|
|
25
|
+
* Both inputs should already be normalized.
|
|
26
|
+
*/
|
|
27
|
+
function isPathDescendant(child, parent) {
|
|
28
|
+
return child.startsWith(parent + '/');
|
|
29
|
+
}
|
|
30
|
+
exports.isPathDescendant = isPathDescendant;
|
|
31
|
+
/**
|
|
32
|
+
* True when `filepath` is covered by `declaredPath` — either equal to it
|
|
33
|
+
* (file match) or nested under it (folder match). Inputs are normalized
|
|
34
|
+
* internally so callers can pass raw canvas values.
|
|
35
|
+
*/
|
|
36
|
+
function pathCovers(declaredPath, filepath) {
|
|
37
|
+
const d = normalizePath(declaredPath);
|
|
38
|
+
const f = normalizePath(filepath);
|
|
39
|
+
return d === f || isPathDescendant(f, d);
|
|
40
|
+
}
|
|
41
|
+
exports.pathCovers = pathCovers;
|
|
42
|
+
/**
|
|
43
|
+
* Classify the relationship between two declared paths.
|
|
44
|
+
* - 'none': disjoint — no overlap
|
|
45
|
+
* - 'partition': one is strictly a parent of the other — valid nested-namespace case
|
|
46
|
+
* (longest-prefix wins at runtime, parent/child partition the tree)
|
|
47
|
+
* - 'conflict': identical, or overlap that isn't a clean parent-child partition
|
|
48
|
+
*/
|
|
49
|
+
function pathsOverlap(a, b) {
|
|
50
|
+
const na = normalizePath(a);
|
|
51
|
+
const nb = normalizePath(b);
|
|
52
|
+
if (na === nb)
|
|
53
|
+
return 'conflict';
|
|
54
|
+
if (isPathDescendant(na, nb) || isPathDescendant(nb, na))
|
|
55
|
+
return 'partition';
|
|
56
|
+
return 'none';
|
|
57
|
+
}
|
|
58
|
+
exports.pathsOverlap = pathsOverlap;
|
|
59
|
+
//# sourceMappingURL=path-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-helpers.js","sourceRoot":"","sources":["../../src/events/path-helpers.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAEH;;;GAGG;AACH,SAAgB,aAAa,CAAC,CAAS;IACrC,OAAO,CAAC;SACL,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;SACpB,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;SACnB,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC1B,CAAC;AALD,sCAKC;AAED;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,KAAa,EAAE,MAAc;IAC5D,OAAO,KAAK,CAAC,UAAU,CAAC,MAAM,GAAG,GAAG,CAAC,CAAC;AACxC,CAAC;AAFD,4CAEC;AAED;;;;GAIG;AACH,SAAgB,UAAU,CAAC,YAAoB,EAAE,QAAgB;IAC/D,MAAM,CAAC,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC;IACtC,MAAM,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;IAClC,OAAO,CAAC,KAAK,CAAC,IAAI,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAC3C,CAAC;AAJD,gCAIC;AAED;;;;;;GAMG;AACH,SAAgB,YAAY,CAAC,CAAS,EAAE,CAAS;IAC/C,MAAM,EAAE,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;IAC5B,MAAM,EAAE,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;IAC5B,IAAI,EAAE,KAAK,EAAE;QAAE,OAAO,UAAU,CAAC;IACjC,IAAI,gBAAgB,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,gBAAgB,CAAC,EAAE,EAAE,EAAE,CAAC;QAAE,OAAO,WAAW,CAAC;IAC7E,OAAO,MAAM,CAAC;AAChB,CAAC;AAND,oCAMC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { describe, expect, test, beforeAll, afterAll } from 'bun:test';
|
|
2
|
+
import { mkdtempSync, mkdirSync, rmSync } from 'fs';
|
|
3
|
+
import { tmpdir } from 'os';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
import { EventsCanvasValidator } from './EventsCanvasValidator';
|
|
6
|
+
import type { ExtendedCanvas } from '../types/canvas';
|
|
7
|
+
|
|
8
|
+
let testRoot: string;
|
|
9
|
+
|
|
10
|
+
beforeAll(() => {
|
|
11
|
+
testRoot = mkdtempSync(join(tmpdir(), 'events-canvas-validator-'));
|
|
12
|
+
mkdirSync(join(testRoot, 'src/events'), { recursive: true });
|
|
13
|
+
mkdirSync(join(testRoot, 'src/workflow/scenarios'), { recursive: true });
|
|
14
|
+
mkdirSync(join(testRoot, 'src/trace'), { recursive: true });
|
|
15
|
+
mkdirSync(join(testRoot, 'src/generated/events'), { recursive: true });
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
afterAll(() => {
|
|
19
|
+
rmSync(testRoot, { recursive: true, force: true });
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
function namespaceNode(
|
|
23
|
+
id: string,
|
|
24
|
+
name: string,
|
|
25
|
+
opts: { paths?: string[]; eventNames?: string[] } = {},
|
|
26
|
+
): any {
|
|
27
|
+
const eventNames = opts.eventNames ?? [`${name}.happened`];
|
|
28
|
+
return {
|
|
29
|
+
id,
|
|
30
|
+
type: 'event-namespace',
|
|
31
|
+
x: 0,
|
|
32
|
+
y: 0,
|
|
33
|
+
width: 100,
|
|
34
|
+
height: 100,
|
|
35
|
+
namespace: {
|
|
36
|
+
name,
|
|
37
|
+
description: `${name} description`,
|
|
38
|
+
paths: opts.paths,
|
|
39
|
+
events: eventNames.map((n) => ({
|
|
40
|
+
name: n,
|
|
41
|
+
severity: 'INFO',
|
|
42
|
+
description: `desc for ${n}`,
|
|
43
|
+
})),
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function canvas(nodes: any[]): ExtendedCanvas {
|
|
49
|
+
return { nodes, edges: [] } as unknown as ExtendedCanvas;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function runValidator(nodes: any[]) {
|
|
53
|
+
const validator = new EventsCanvasValidator();
|
|
54
|
+
return validator.validate({
|
|
55
|
+
eventsCanvas: canvas(nodes),
|
|
56
|
+
eventsCanvasPath: 'test.events.canvas',
|
|
57
|
+
basePath: testRoot,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function rules(result: { violations: Array<{ ruleId: string }> }, ruleId: string) {
|
|
62
|
+
return result.violations.filter((v) => v.ruleId === ruleId);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
describe('EventsCanvasValidator — paths field', () => {
|
|
66
|
+
test('namespace without paths: no path-related violations (opt-in, backward compatible)', async () => {
|
|
67
|
+
const result = await runValidator([namespaceNode('events', 'events')]);
|
|
68
|
+
|
|
69
|
+
expect(rules(result, 'events-namespace-multiple-paths')).toHaveLength(0);
|
|
70
|
+
expect(rules(result, 'events-namespace-paths-missing')).toHaveLength(0);
|
|
71
|
+
expect(rules(result, 'events-namespace-paths-overlap')).toHaveLength(0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('namespace with one existing path: no path-related violations', async () => {
|
|
75
|
+
const result = await runValidator([
|
|
76
|
+
namespaceNode('events', 'events', { paths: ['src/events'] }),
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
expect(rules(result, 'events-namespace-multiple-paths')).toHaveLength(0);
|
|
80
|
+
expect(rules(result, 'events-namespace-paths-missing')).toHaveLength(0);
|
|
81
|
+
expect(rules(result, 'events-namespace-paths-overlap')).toHaveLength(0);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('namespace with missing path: warns events-namespace-paths-missing', async () => {
|
|
85
|
+
const result = await runValidator([
|
|
86
|
+
namespaceNode('events', 'events', { paths: ['src/does-not-exist'] }),
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
const missing = rules(result, 'events-namespace-paths-missing');
|
|
90
|
+
expect(missing).toHaveLength(1);
|
|
91
|
+
expect(missing[0].severity).toBe('warn');
|
|
92
|
+
expect(missing[0].message).toContain('src/does-not-exist');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('namespace with multiple paths: warns events-namespace-multiple-paths', async () => {
|
|
96
|
+
const result = await runValidator([
|
|
97
|
+
namespaceNode('events', 'events', {
|
|
98
|
+
paths: ['src/events', 'src/generated/events'],
|
|
99
|
+
}),
|
|
100
|
+
]);
|
|
101
|
+
|
|
102
|
+
const multi = rules(result, 'events-namespace-multiple-paths');
|
|
103
|
+
expect(multi).toHaveLength(1);
|
|
104
|
+
expect(multi[0].severity).toBe('warn');
|
|
105
|
+
expect(multi[0].message).toContain('2 paths');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('two namespaces declaring identical paths: errors events-namespace-paths-overlap', async () => {
|
|
109
|
+
const result = await runValidator([
|
|
110
|
+
namespaceNode('a', 'alpha', {
|
|
111
|
+
paths: ['src/events'],
|
|
112
|
+
eventNames: ['alpha.happened'],
|
|
113
|
+
}),
|
|
114
|
+
namespaceNode('b', 'beta', {
|
|
115
|
+
paths: ['src/events'],
|
|
116
|
+
eventNames: ['beta.happened'],
|
|
117
|
+
}),
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
const overlap = rules(result, 'events-namespace-paths-overlap');
|
|
121
|
+
expect(overlap).toHaveLength(1);
|
|
122
|
+
expect(overlap[0].severity).toBe('error');
|
|
123
|
+
expect(result.valid).toBe(false);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('parent-child partition (workflow / workflow.scenarios): no overlap error', async () => {
|
|
127
|
+
const result = await runValidator([
|
|
128
|
+
namespaceNode('workflow', 'workflow', {
|
|
129
|
+
paths: ['src/workflow'],
|
|
130
|
+
eventNames: ['workflow.happened'],
|
|
131
|
+
}),
|
|
132
|
+
namespaceNode('workflow.scenarios', 'workflow.scenarios', {
|
|
133
|
+
paths: ['src/workflow/scenarios'],
|
|
134
|
+
eventNames: ['workflow.scenarios.matched'],
|
|
135
|
+
}),
|
|
136
|
+
]);
|
|
137
|
+
|
|
138
|
+
expect(rules(result, 'events-namespace-paths-overlap')).toHaveLength(0);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test('disjoint sibling paths: no overlap error', async () => {
|
|
142
|
+
const result = await runValidator([
|
|
143
|
+
namespaceNode('events', 'events', {
|
|
144
|
+
paths: ['src/events'],
|
|
145
|
+
eventNames: ['events.happened'],
|
|
146
|
+
}),
|
|
147
|
+
namespaceNode('trace', 'trace', {
|
|
148
|
+
paths: ['src/trace'],
|
|
149
|
+
eventNames: ['trace.happened'],
|
|
150
|
+
}),
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
expect(rules(result, 'events-namespace-paths-overlap')).toHaveLength(0);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test('path normalization: trailing slashes and "./" prefix compared equivalently', async () => {
|
|
157
|
+
const result = await runValidator([
|
|
158
|
+
namespaceNode('a', 'alpha', {
|
|
159
|
+
paths: ['src/events/'],
|
|
160
|
+
eventNames: ['alpha.happened'],
|
|
161
|
+
}),
|
|
162
|
+
namespaceNode('b', 'beta', {
|
|
163
|
+
paths: ['./src/events'],
|
|
164
|
+
eventNames: ['beta.happened'],
|
|
165
|
+
}),
|
|
166
|
+
]);
|
|
167
|
+
|
|
168
|
+
expect(rules(result, 'events-namespace-paths-overlap')).toHaveLength(1);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test('similar-prefix paths that are not parent/child: no overlap error', async () => {
|
|
172
|
+
// src/foo and src/foobar share a prefix but neither is a descendant of the other.
|
|
173
|
+
// These should not trigger the overlap rule.
|
|
174
|
+
mkdirSync(join(testRoot, 'src/foo'), { recursive: true });
|
|
175
|
+
mkdirSync(join(testRoot, 'src/foobar'), { recursive: true });
|
|
176
|
+
|
|
177
|
+
const result = await runValidator([
|
|
178
|
+
namespaceNode('a', 'foo', {
|
|
179
|
+
paths: ['src/foo'],
|
|
180
|
+
eventNames: ['foo.happened'],
|
|
181
|
+
}),
|
|
182
|
+
namespaceNode('b', 'foobar', {
|
|
183
|
+
paths: ['src/foobar'],
|
|
184
|
+
eventNames: ['foobar.happened'],
|
|
185
|
+
}),
|
|
186
|
+
]);
|
|
187
|
+
|
|
188
|
+
expect(rules(result, 'events-namespace-paths-overlap')).toHaveLength(0);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import type { ExtendedCanvas, ExtendedCanvasNode } from '../types/canvas';
|
|
9
|
+
import { existsSync } from 'fs';
|
|
10
|
+
import { resolve } from 'path';
|
|
11
|
+
import { pathsOverlap } from './path-helpers';
|
|
9
12
|
|
|
10
13
|
/**
|
|
11
14
|
* Event namespace node structure
|
|
@@ -16,6 +19,19 @@ export interface EventNamespaceNode {
|
|
|
16
19
|
namespace: {
|
|
17
20
|
name: string;
|
|
18
21
|
description: string;
|
|
22
|
+
/**
|
|
23
|
+
* Optional source paths that define this component's code location.
|
|
24
|
+
* Each entry may be a folder (covers all descendants) or a specific file.
|
|
25
|
+
* When present, events in this namespace may only be emitted from files
|
|
26
|
+
* covered by one of these paths. Namespaces without `paths` remain
|
|
27
|
+
* unenforced — enforcement is opt-in per namespace.
|
|
28
|
+
*
|
|
29
|
+
* Multiple entries are valid for cases like generated code, platform-split
|
|
30
|
+
* implementations, or migration periods. The validator warns when
|
|
31
|
+
* `paths.length > 1` to prompt authors to confirm the multi-location
|
|
32
|
+
* is intentional.
|
|
33
|
+
*/
|
|
34
|
+
paths?: string[];
|
|
19
35
|
events: Array<{
|
|
20
36
|
name: string;
|
|
21
37
|
severity?: 'INFO' | 'WARN' | 'ERROR';
|
|
@@ -328,6 +344,76 @@ export class EventsCanvasValidator {
|
|
|
328
344
|
}
|
|
329
345
|
}
|
|
330
346
|
|
|
347
|
+
// Validate namespace paths (optional field — enforcement is opt-in per namespace).
|
|
348
|
+
// Collects declared paths across namespaces to check for cross-namespace overlap.
|
|
349
|
+
const declaredPaths: Array<{ namespace: string; nodeId: string; path: string }> = [];
|
|
350
|
+
for (const node of eventsCanvas.nodes || []) {
|
|
351
|
+
if (!this.isEventNamespaceNode(node)) continue;
|
|
352
|
+
const namespaceNode = node as EventNamespaceNode;
|
|
353
|
+
const paths = namespaceNode.namespace.paths;
|
|
354
|
+
if (!paths || paths.length === 0) continue;
|
|
355
|
+
|
|
356
|
+
// Warn when a namespace declares more than one path — prompt author to
|
|
357
|
+
// confirm the multi-location is intentional (generated code, platform
|
|
358
|
+
// splits, migration) rather than an accidental sprawl.
|
|
359
|
+
if (paths.length > 1) {
|
|
360
|
+
violations.push({
|
|
361
|
+
ruleId: 'events-namespace-multiple-paths',
|
|
362
|
+
severity: 'warn',
|
|
363
|
+
file: eventsCanvasPath || '.principal-views/cli.events.canvas',
|
|
364
|
+
path: `nodes[id="${namespaceNode.id}"].namespace.paths`,
|
|
365
|
+
message: `Namespace "${namespaceNode.namespace.name}" declares ${paths.length} paths`,
|
|
366
|
+
impact: 'Multiple paths can indicate legitimate cases (generated code, platform splits, migration) but often signal that the namespace should be split',
|
|
367
|
+
suggestion: 'Confirm the multi-location is intentional. Otherwise consider splitting the namespace or grouping the code under a single parent folder.',
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
for (const p of paths) {
|
|
372
|
+
declaredPaths.push({
|
|
373
|
+
namespace: namespaceNode.namespace.name,
|
|
374
|
+
nodeId: namespaceNode.id,
|
|
375
|
+
path: p,
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
// Warn when a declared path does not exist relative to the repo root.
|
|
379
|
+
const resolved = resolve(basePath, p);
|
|
380
|
+
if (!existsSync(resolved)) {
|
|
381
|
+
violations.push({
|
|
382
|
+
ruleId: 'events-namespace-paths-missing',
|
|
383
|
+
severity: 'warn',
|
|
384
|
+
file: eventsCanvasPath || '.principal-views/cli.events.canvas',
|
|
385
|
+
path: `nodes[id="${namespaceNode.id}"].namespace.paths`,
|
|
386
|
+
message: `Path "${p}" declared by namespace "${namespaceNode.namespace.name}" does not exist`,
|
|
387
|
+
impact: 'Events emitted under this namespace cannot be validated against a real code location',
|
|
388
|
+
suggestion: 'Verify the path exists relative to the repository root, or remove it from the namespace declaration.',
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Cross-namespace overlap check. Parent-child nesting is a valid partition
|
|
395
|
+
// (longest-prefix wins at runtime); any other overlap makes namespace
|
|
396
|
+
// ownership of a file ambiguous.
|
|
397
|
+
for (let i = 0; i < declaredPaths.length; i++) {
|
|
398
|
+
for (let j = i + 1; j < declaredPaths.length; j++) {
|
|
399
|
+
const a = declaredPaths[i];
|
|
400
|
+
const b = declaredPaths[j];
|
|
401
|
+
if (a.namespace === b.namespace) continue;
|
|
402
|
+
|
|
403
|
+
const relationship = pathsOverlap(a.path, b.path);
|
|
404
|
+
if (relationship === 'conflict') {
|
|
405
|
+
violations.push({
|
|
406
|
+
ruleId: 'events-namespace-paths-overlap',
|
|
407
|
+
severity: 'error',
|
|
408
|
+
file: eventsCanvasPath || '.principal-views/cli.events.canvas',
|
|
409
|
+
message: `Paths overlap between namespaces "${a.namespace}" ("${a.path}") and "${b.namespace}" ("${b.path}")`,
|
|
410
|
+
impact: 'A file covered by two namespaces makes namespace ownership ambiguous',
|
|
411
|
+
suggestion: 'Separate the paths so they are disjoint, or restructure as parent/child namespaces (e.g., "workflow" covering "src/workflow" and "workflow.scenarios" covering "src/workflow/scenarios").',
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
331
417
|
metrics.totalNamespaces = extractedNamespaces.size;
|
|
332
418
|
|
|
333
419
|
const errors = violations.filter(v => v.severity === 'error');
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { NamespacePathIndex } from './NamespacePathIndex';
|
|
3
|
+
|
|
4
|
+
function makeIndex(entries: Array<{ scope: string; namespace: string; paths: string[] }>) {
|
|
5
|
+
const idx = new NamespacePathIndex();
|
|
6
|
+
idx.addAll(entries);
|
|
7
|
+
return idx;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe('NamespacePathIndex.resolve', () => {
|
|
11
|
+
test('empty index resolves to null', () => {
|
|
12
|
+
const idx = new NamespacePathIndex();
|
|
13
|
+
expect(idx.resolve('scope', 'src/foo.ts')).toBeNull();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('matches file nested under a declared folder path', () => {
|
|
17
|
+
const idx = makeIndex([{ scope: 's', namespace: 'events', paths: ['src/events'] }]);
|
|
18
|
+
const match = idx.resolve('s', 'src/events/foo.ts');
|
|
19
|
+
expect(match?.entry.namespace).toBe('events');
|
|
20
|
+
expect(match?.matchedPath).toBe('src/events');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('returns null when no path covers the file', () => {
|
|
24
|
+
const idx = makeIndex([{ scope: 's', namespace: 'events', paths: ['src/events'] }]);
|
|
25
|
+
expect(idx.resolve('s', 'src/other/foo.ts')).toBeNull();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('scope isolation: match in scope A is not returned when querying scope B', () => {
|
|
29
|
+
const idx = makeIndex([{ scope: 'A', namespace: 'events', paths: ['src/events'] }]);
|
|
30
|
+
expect(idx.resolve('B', 'src/events/foo.ts')).toBeNull();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('longest-prefix wins: nested child namespace beats parent', () => {
|
|
34
|
+
const idx = makeIndex([
|
|
35
|
+
{ scope: 's', namespace: 'workflow', paths: ['src/workflow'] },
|
|
36
|
+
{ scope: 's', namespace: 'workflow.scenarios', paths: ['src/workflow/scenarios'] },
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
// Parent-only subtree → parent wins
|
|
40
|
+
expect(idx.resolve('s', 'src/workflow/orchestrator.ts')?.entry.namespace).toBe('workflow');
|
|
41
|
+
// Child subtree → child wins (longer prefix)
|
|
42
|
+
expect(idx.resolve('s', 'src/workflow/scenarios/matcher.ts')?.entry.namespace).toBe(
|
|
43
|
+
'workflow.scenarios',
|
|
44
|
+
);
|
|
45
|
+
// Deeper under child → still child (inherited)
|
|
46
|
+
expect(idx.resolve('s', 'src/workflow/scenarios/helpers/fmt.ts')?.entry.namespace).toBe(
|
|
47
|
+
'workflow.scenarios',
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('exact-file path matches when declared as a file', () => {
|
|
52
|
+
const idx = makeIndex([{ scope: 's', namespace: 'retry', paths: ['src/utils/retry.ts'] }]);
|
|
53
|
+
expect(idx.resolve('s', 'src/utils/retry.ts')?.entry.namespace).toBe('retry');
|
|
54
|
+
// Not a folder, so unrelated files don't match
|
|
55
|
+
expect(idx.resolve('s', 'src/utils/other.ts')).toBeNull();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('normalization: trailing slash and ./ prefix are equivalent', () => {
|
|
59
|
+
const idx = makeIndex([{ scope: 's', namespace: 'events', paths: ['src/events/'] }]);
|
|
60
|
+
expect(idx.resolve('s', './src/events/foo.ts')?.entry.namespace).toBe('events');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('similar-prefix paths are not mistaken for nesting', () => {
|
|
64
|
+
const idx = makeIndex([
|
|
65
|
+
{ scope: 's', namespace: 'foo', paths: ['src/foo'] },
|
|
66
|
+
{ scope: 's', namespace: 'foobar', paths: ['src/foobar'] },
|
|
67
|
+
]);
|
|
68
|
+
expect(idx.resolve('s', 'src/foobar/x.ts')?.entry.namespace).toBe('foobar');
|
|
69
|
+
expect(idx.resolve('s', 'src/foo/x.ts')?.entry.namespace).toBe('foo');
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('NamespacePathIndex.getEntry', () => {
|
|
74
|
+
test('returns null when (scope, namespace) not registered', () => {
|
|
75
|
+
const idx = makeIndex([{ scope: 's', namespace: 'events', paths: ['src/events'] }]);
|
|
76
|
+
expect(idx.getEntry('s', 'workflow')).toBeNull();
|
|
77
|
+
expect(idx.getEntry('other', 'events')).toBeNull();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('returns the entry when present', () => {
|
|
81
|
+
const idx = makeIndex([{ scope: 's', namespace: 'events', paths: ['src/events'] }]);
|
|
82
|
+
expect(idx.getEntry('s', 'events')?.paths).toEqual(['src/events']);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('NamespacePathIndex.getScopeEntries / getScopes', () => {
|
|
87
|
+
test('returns only entries for the requested scope', () => {
|
|
88
|
+
const idx = makeIndex([
|
|
89
|
+
{ scope: 'A', namespace: 'a1', paths: ['src/a1'] },
|
|
90
|
+
{ scope: 'A', namespace: 'a2', paths: ['src/a2'] },
|
|
91
|
+
{ scope: 'B', namespace: 'b1', paths: ['src/b1'] },
|
|
92
|
+
]);
|
|
93
|
+
expect(idx.getScopeEntries('A')).toHaveLength(2);
|
|
94
|
+
expect(idx.getScopeEntries('B')).toHaveLength(1);
|
|
95
|
+
expect(idx.getScopes().sort()).toEqual(['A', 'B']);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Namespace Path Index
|
|
3
|
+
*
|
|
4
|
+
* Maps (scope, filepath) to the event-namespace that owns the file, using the
|
|
5
|
+
* `paths` field declared on `event-namespace` nodes.
|
|
6
|
+
*
|
|
7
|
+
* Resolution uses the longest-prefix partitioning rule: when multiple
|
|
8
|
+
* namespaces within a scope have paths that cover the same file, the one
|
|
9
|
+
* whose matched path is the most specific (longest after normalization) wins.
|
|
10
|
+
* This naturally supports nested namespaces such as `workflow` and
|
|
11
|
+
* `workflow.scenarios`, where the child claims its subtree without stealing
|
|
12
|
+
* the parent's.
|
|
13
|
+
*
|
|
14
|
+
* Pure/browser-safe — no filesystem access, just string comparison.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { normalizePath, pathCovers } from './path-helpers';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* One declared namespace/paths tuple, tagged with the scope it belongs to.
|
|
21
|
+
* Multiple entries may share the same scope (different namespaces in the
|
|
22
|
+
* same events canvas) or the same namespace across scopes (no cross-scope
|
|
23
|
+
* conflict — resolution is scoped).
|
|
24
|
+
*/
|
|
25
|
+
export interface NamespacePathEntry {
|
|
26
|
+
scope: string;
|
|
27
|
+
namespace: string;
|
|
28
|
+
paths: string[];
|
|
29
|
+
/** Optional — useful for surfacing where a match came from in diagnostics. */
|
|
30
|
+
sourceCanvasPath?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Result of resolving a file against the index.
|
|
35
|
+
*/
|
|
36
|
+
export interface NamespacePathMatch {
|
|
37
|
+
/** The winning entry (by longest-prefix). */
|
|
38
|
+
entry: NamespacePathEntry;
|
|
39
|
+
/** Which of the entry's paths covered the file — the one that won. */
|
|
40
|
+
matchedPath: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class NamespacePathIndex {
|
|
44
|
+
private entries: NamespacePathEntry[] = [];
|
|
45
|
+
|
|
46
|
+
/** Register a single namespace/paths entry. */
|
|
47
|
+
add(entry: NamespacePathEntry): void {
|
|
48
|
+
this.entries.push(entry);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Register many entries at once. */
|
|
52
|
+
addAll(entries: readonly NamespacePathEntry[]): void {
|
|
53
|
+
for (const e of entries) this.add(e);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Resolve a filepath to its owning namespace within a scope.
|
|
58
|
+
* Returns null if no entry in that scope has a path covering the file.
|
|
59
|
+
*/
|
|
60
|
+
resolve(scope: string, filepath: string): NamespacePathMatch | null {
|
|
61
|
+
const normalizedFile = normalizePath(filepath);
|
|
62
|
+
let best: { entry: NamespacePathEntry; matchedPath: string; length: number } | null = null;
|
|
63
|
+
|
|
64
|
+
for (const entry of this.entries) {
|
|
65
|
+
if (entry.scope !== scope) continue;
|
|
66
|
+
for (const declared of entry.paths) {
|
|
67
|
+
if (!pathCovers(declared, normalizedFile)) continue;
|
|
68
|
+
const normalizedDeclared = normalizePath(declared);
|
|
69
|
+
const length = normalizedDeclared.length;
|
|
70
|
+
if (best === null || length > best.length) {
|
|
71
|
+
best = { entry, matchedPath: declared, length };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return best ? { entry: best.entry, matchedPath: best.matchedPath } : null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** All entries registered under the given scope (for diagnostics). */
|
|
80
|
+
getScopeEntries(scope: string): readonly NamespacePathEntry[] {
|
|
81
|
+
return this.entries.filter((e) => e.scope === scope);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Look up the entry for a specific (scope, namespace) pair. Returns null
|
|
86
|
+
* when the namespace isn't registered — either because its events-canvas
|
|
87
|
+
* node omits `paths` (opt-out of enforcement) or because no such namespace
|
|
88
|
+
* is declared at all.
|
|
89
|
+
*/
|
|
90
|
+
getEntry(scope: string, namespace: string): NamespacePathEntry | null {
|
|
91
|
+
return this.entries.find(
|
|
92
|
+
(e) => e.scope === scope && e.namespace === namespace,
|
|
93
|
+
) ?? null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** All scopes present in the index. */
|
|
97
|
+
getScopes(): readonly string[] {
|
|
98
|
+
return Array.from(new Set(this.entries.map((e) => e.scope)));
|
|
99
|
+
}
|
|
100
|
+
}
|