| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 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);
- }
- }
|