Skip to content

Commit e22354f

Browse files
committed
Add another component to the tests which doesn't need a download button
1 parent e69f191 commit e22354f

4 files changed

Lines changed: 77 additions & 42 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ data-alloy/
1818
/packaging/windows/LICENSE
1919
/packaging/windows/agent-windows-amd64.exe
2020
internal/web/ui/dist
21-
internal/web/ui/src/test/fixtures/large_disc_output.json
21+
internal/web/ui/src/test/generated_fixtures/
2222

2323
.DS_Store
2424
buildx-v*

internal/web/ui/src/test/fixtures/generateLargeDiscOutput.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ function generateTarget(index: number) {
6565
}
6666

6767
/**
68-
* Generates a large discovery.kubernetes output with the specified number of targets.
69-
* Default count is chosen to create a large dataset that exceeds typical render limits.
68+
* Generates a discovery.kubernetes output with the specified number of targets and label.
7069
*/
71-
export function generateLargeDiscOutput(targetCount = 20000) {
70+
export function generateDiscOutput(label: string, targetCount: number) {
7271
const targets = [];
7372
for (let i = 0; i < targetCount; i++) {
7473
targets.push(generateTarget(i));
@@ -77,12 +76,12 @@ export function generateLargeDiscOutput(targetCount = 20000) {
7776
return {
7877
name: 'discovery.kubernetes',
7978
type: 'block',
80-
localID: 'discovery.kubernetes.default_kubernetes',
79+
localID: `discovery.kubernetes.${label}`,
8180
moduleID: '',
82-
label: 'default_kubernetes',
81+
label: label,
8382
referencesTo: [],
84-
referencedBy: ['discovery.relabel.default_kubernetes'],
85-
dataFlowEdgesTo: ['discovery.relabel.default_kubernetes'],
83+
referencedBy: [`discovery.relabel.${label}`],
84+
dataFlowEdgesTo: [`discovery.relabel.${label}`],
8685
health: {
8786
state: 'healthy',
8887
message: 'started component',
@@ -109,5 +108,14 @@ export function generateLargeDiscOutput(targetCount = 20000) {
109108
};
110109
}
111110

112-
// Pre-generated fixture for tests - uses 5000 targets to ensure it's large enough
113-
export const largeDiscOutput = generateLargeDiscOutput(50000);
111+
// Heavy fixture - 50000 targets, requires download button
112+
export const heavyDiscOutput = generateDiscOutput('heavy', 50000);
113+
114+
// Light fixture - 3 targets, renders inline without download button
115+
export const lightDiscOutput = generateDiscOutput('light', 3);
116+
117+
// Backwards compatibility alias
118+
export const largeDiscOutput = heavyDiscOutput;
119+
export function generateLargeDiscOutput(targetCount = 20000) {
120+
return generateDiscOutput('heavy', targetCount);
121+
}
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
import { writeFileSync } from 'fs';
1+
import { mkdirSync, writeFileSync } from 'fs';
22
import { dirname, join } from 'path';
33
import { fileURLToPath } from 'url';
44

5-
import { largeDiscOutput } from './generateLargeDiscOutput';
5+
import { heavyDiscOutput, lightDiscOutput } from './generateLargeDiscOutput';
66

77
const __dirname = dirname(fileURLToPath(import.meta.url));
8-
const jsonPath = join(__dirname, 'large_disc_output.json');
8+
const generatedDir = join(__dirname, '..', 'generated_fixtures');
99

10-
writeFileSync(jsonPath, JSON.stringify(largeDiscOutput, null, 2));
10+
// Ensure the generated_fixtures directory exists
11+
mkdirSync(generatedDir, { recursive: true });
1112

12-
console.log(`Generated fixture at ${jsonPath}`);
13+
// Write heavy fixture (large - requires download button)
14+
const heavyPath = join(generatedDir, 'discovery.kubernetes.heavy.json');
15+
writeFileSync(heavyPath, JSON.stringify(heavyDiscOutput, null, 2));
16+
console.log(`Generated heavy fixture at ${heavyPath}`);
17+
18+
// Write light fixture (small - renders inline)
19+
const lightPath = join(generatedDir, 'discovery.kubernetes.light.json');
20+
writeFileSync(lightPath, JSON.stringify(lightDiscOutput, null, 2));
21+
console.log(`Generated light fixture at ${lightPath}`);

internal/web/ui/vite.config.ts

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)