api.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { Page } from '@playwright/test'
  2. // Mock data
  3. export const mockPatterns = [
  4. { path: 'patterns/star.thr', name: 'star.thr', category: 'geometric', date_modified: Date.now(), coordinates_count: 150 },
  5. { path: 'patterns/spiral.thr', name: 'spiral.thr', category: 'organic', date_modified: Date.now(), coordinates_count: 200 },
  6. { path: 'patterns/wave.thr', name: 'wave.thr', category: 'organic', date_modified: Date.now(), coordinates_count: 175 },
  7. ]
  8. export const mockPlaylists = {
  9. default: ['patterns/star.thr', 'patterns/spiral.thr'],
  10. favorites: ['patterns/star.thr'],
  11. }
  12. // Mutable status for simulating playback
  13. let currentStatus = {
  14. is_running: false,
  15. is_paused: false,
  16. current_file: null as string | null,
  17. speed: 100,
  18. progress: 0,
  19. playlist_mode: false,
  20. playlist_name: null as string | null,
  21. queue: [] as string[],
  22. connection_status: 'connected',
  23. theta: 0,
  24. rho: 0.5,
  25. }
  26. export function resetMockStatus() {
  27. currentStatus = {
  28. is_running: false,
  29. is_paused: false,
  30. current_file: null,
  31. speed: 100,
  32. progress: 0,
  33. playlist_mode: false,
  34. playlist_name: null,
  35. queue: [],
  36. connection_status: 'connected',
  37. theta: 0,
  38. rho: 0.5,
  39. }
  40. }
  41. export async function setupApiMocks(page: Page) {
  42. // Pattern endpoints
  43. await page.route('**/list_theta_rho_files_with_metadata', async route => {
  44. await route.fulfill({ json: mockPatterns })
  45. })
  46. await page.route('**/list_theta_rho_files', async route => {
  47. await route.fulfill({ json: mockPatterns.map(p => ({ name: p.name, path: p.path })) })
  48. })
  49. await page.route('**/preview_thr_batch', async route => {
  50. const request = route.request()
  51. const body = request.postDataJSON() as { files: string[] }
  52. const previews: Record<string, unknown> = {}
  53. for (const file of body?.files || []) {
  54. previews[file] = {
  55. image_data: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
  56. first_coordinate: { x: 0, y: 0 },
  57. last_coordinate: { x: 100, y: 100 },
  58. }
  59. }
  60. await route.fulfill({ json: previews })
  61. })
  62. await page.route('**/run_theta_rho', async route => {
  63. const body = route.request().postDataJSON() as { file_name?: string; file?: string }
  64. const file = body?.file_name || body?.file || null
  65. currentStatus.is_running = true
  66. currentStatus.current_file = file
  67. await route.fulfill({ json: { success: true } })
  68. })
  69. // Playlist endpoints
  70. await page.route('**/list_all_playlists', async route => {
  71. await route.fulfill({ json: Object.keys(mockPlaylists) })
  72. })
  73. await page.route('**/get_playlist**', async route => {
  74. const url = new URL(route.request().url())
  75. const name = url.searchParams.get('name') || ''
  76. await route.fulfill({
  77. json: { name, files: mockPlaylists[name as keyof typeof mockPlaylists] || [] }
  78. })
  79. })
  80. await page.route('**/run_playlist', async route => {
  81. const body = route.request().postDataJSON() as { name: string }
  82. const playlist = mockPlaylists[body?.name as keyof typeof mockPlaylists]
  83. if (playlist && playlist.length > 0) {
  84. currentStatus.is_running = true
  85. currentStatus.playlist_mode = true
  86. currentStatus.playlist_name = body.name
  87. currentStatus.current_file = playlist[0]
  88. currentStatus.queue = playlist.slice(1)
  89. }
  90. await route.fulfill({ json: { success: true } })
  91. })
  92. // Playback control endpoints
  93. await page.route('**/pause_execution', async route => {
  94. currentStatus.is_paused = true
  95. await route.fulfill({ json: { success: true } })
  96. })
  97. await page.route('**/resume_execution', async route => {
  98. currentStatus.is_paused = false
  99. await route.fulfill({ json: { success: true } })
  100. })
  101. await page.route('**/stop_execution', async route => {
  102. currentStatus.is_running = false
  103. currentStatus.is_paused = false
  104. currentStatus.current_file = null
  105. currentStatus.playlist_mode = false
  106. currentStatus.queue = []
  107. await route.fulfill({ json: { success: true } })
  108. })
  109. // Status endpoint
  110. await page.route('**/serial_status', async route => {
  111. await route.fulfill({ json: currentStatus })
  112. })
  113. // Table info (for TableContext)
  114. await page.route('**/api/table-info', async route => {
  115. await route.fulfill({
  116. json: { id: 'test-table', name: 'Test Table', version: '1.0.0' }
  117. })
  118. })
  119. // Settings
  120. await page.route('**/api/settings', async route => {
  121. await route.fulfill({ json: { app: { name: 'Dune Weaver' } } })
  122. })
  123. // Known tables
  124. await page.route('**/api/known-tables', async route => {
  125. await route.fulfill({ json: { tables: [] } })
  126. })
  127. // Pattern history
  128. await page.route('**/api/pattern_history_all', async route => {
  129. await route.fulfill({ json: {} })
  130. })
  131. // WebSocket - return empty to prevent connection attempts
  132. // Playwright doesn't intercept WebSocket by default, but we can handle the fallback
  133. }
  134. export function getMockStatus() {
  135. return { ...currentStatus }
  136. }
  137. export function setMockStatus(updates: Partial<typeof currentStatus>) {
  138. Object.assign(currentStatus, updates)
  139. }