-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathmock-api.ts
More file actions
75 lines (68 loc) · 2.46 KB
/
mock-api.ts
File metadata and controls
75 lines (68 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { Page } from '@playwright/test';
import { mockOutlines } from './test-data/scene-outlines';
import { mockSceneContentResponse } from './test-data/scene-content';
import { createMockSceneActionsResponse } from './test-data/scene-actions';
/**
* Wraps Playwright's page.route() to mock OpenMAIC API endpoints.
* Supports both JSON and SSE (text/event-stream) responses.
*/
export class MockApi {
constructor(private page: Page) {}
/** Mock the SSE outline streaming endpoint */
async mockSceneOutlinesStream(outlines = mockOutlines) {
await this.page.route('**/api/generate/scene-outlines-stream', (route) => {
const events = outlines
.map(
(outline, i) =>
`data: ${JSON.stringify({ type: 'outline', data: outline, index: i })}\n\n`,
)
.join('');
const done = `data: ${JSON.stringify({ type: 'done', outlines })}\n\n`;
route.fulfill({
status: 200,
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
},
body: events + done,
});
});
}
/** Mock the scene content generation endpoint */
async mockSceneContent(response = mockSceneContentResponse) {
await this.page.route('**/api/generate/scene-content', (route) => {
route.fulfill({
status: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(response),
});
});
}
/** Mock the scene actions generation endpoint */
async mockSceneActions(stageId = 'test-stage') {
await this.page.route('**/api/generate/scene-actions', (route) => {
route.fulfill({
status: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(createMockSceneActionsResponse(stageId)),
});
});
}
/** Mock the server providers endpoint (returns empty — client-side config only) */
async mockServerProviders() {
await this.page.route('**/api/server-providers', (route) => {
route.fulfill({
status: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ providers: {} }),
});
});
}
/** Set up API mocks for the generation flow. Note: server-providers is already mocked by the base fixture. */
async setupGenerationMocks(stageId = 'test-stage') {
await this.mockSceneOutlinesStream();
await this.mockSceneContent();
await this.mockSceneActions(stageId);
}
}