mock-api.ts 2.5 KB

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