@@ -19,6 +19,28 @@ function goTemplatePlugin() {
1919 } satisfies PluginOption ;
2020}
2121
22+ /**
23+ * Available mock fixtures mapped by their localID.
24+ */
25+ const MOCK_FIXTURES = [
26+ 'discovery.kubernetes.heavy' ,
27+ 'discovery.kubernetes.light' ,
28+ ] ;
29+
30+ /**
31+ * Helper to load a fixture by component ID.
32+ */
33+ function loadFixture ( componentId : string ) : string | null {
34+ try {
35+ return readFileSync (
36+ join ( __dirname , `src/test/generated_fixtures/${ componentId } .json` ) ,
37+ 'utf-8'
38+ ) ;
39+ } catch {
40+ return null ;
41+ }
42+ }
43+
2244/**
2345 * Plugin to mock the Alloy API during development.
2446 * Serves fixture data from src/test/fixtures/ for testing without a real Alloy instance.
@@ -38,42 +60,38 @@ function mockApiPlugin(): PluginOption {
3860 // Mock: GET /api/v0/web/components (list all components)
3961 if ( req . url === '/api/v0/web/components' ) {
4062 res . setHeader ( 'Content-Type' , 'application/json' ) ;
41- try {
42- const fixture = readFileSync (
43- join ( __dirname , 'src/test/fixtures/large_disc_output.json' ) ,
44- 'utf-8'
45- ) ;
46- const data = JSON . parse ( fixture ) ;
47- // Return as a list with one component
48- res . end ( JSON . stringify ( [ {
49- moduleID : data . moduleID || '' ,
50- localID : data . localID ,
51- name : data . name ,
52- label : data . label ,
53- health : data . health ,
54- referencedBy : data . referencedBy || [ ] ,
55- referencesTo : data . referencesTo || [ ] ,
56- dataFlowEdgesTo : data . dataFlowEdgesTo || [ ] ,
57- liveDebuggingEnabled : false ,
58- } ] ) ) ;
59- } catch {
60- res . end ( JSON . stringify ( [ ] ) ) ;
63+ const components = [ ] ;
64+ for ( const fixtureId of MOCK_FIXTURES ) {
65+ const fixture = loadFixture ( fixtureId ) ;
66+ if ( fixture ) {
67+ const data = JSON . parse ( fixture ) ;
68+ components . push ( {
69+ moduleID : data . moduleID || '' ,
70+ localID : data . localID ,
71+ name : data . name ,
72+ label : data . label ,
73+ health : data . health ,
74+ referencedBy : data . referencedBy || [ ] ,
75+ referencesTo : data . referencesTo || [ ] ,
76+ dataFlowEdgesTo : data . dataFlowEdgesTo || [ ] ,
77+ liveDebuggingEnabled : false ,
78+ } ) ;
79+ }
6180 }
81+ res . end ( JSON . stringify ( components ) ) ;
6282 return ;
6383 }
6484
6585 // Mock: GET /api/v0/web/components/:id (component detail)
6686 if ( req . url . startsWith ( '/api/v0/web/components/' ) ) {
87+ const componentId = req . url . replace ( '/api/v0/web/components/' , '' ) ;
6788 res . setHeader ( 'Content-Type' , 'application/json' ) ;
68- try {
69- const fixture = readFileSync (
70- join ( __dirname , 'src/test/fixtures/large_disc_output.json' ) ,
71- 'utf-8'
72- ) ;
89+ const fixture = loadFixture ( componentId ) ;
90+ if ( fixture ) {
7391 res . end ( fixture ) ;
74- } catch ( err ) {
92+ } else {
7593 res . statusCode = 404 ;
76- res . end ( JSON . stringify ( { error : ' Fixture not found' } ) ) ;
94+ res . end ( JSON . stringify ( { error : ` Fixture not found: ${ componentId } ` } ) ) ;
7795 }
7896 return ;
7997 }
0 commit comments