test-speaking-api-config.mjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import assert from 'node:assert/strict'
  2. import { readFile } from 'node:fs/promises'
  3. import ts from 'typescript'
  4. const sourceUrl = new URL('../src/views/Editor/EnglishSpeaking/services/speakingApiConfig.ts', import.meta.url)
  5. const source = await readFile(sourceUrl, 'utf8')
  6. async function loadConfig(env = {}) {
  7. const compiled = ts.transpileModule(source.replaceAll('import.meta.env', JSON.stringify(env)), {
  8. compilerOptions: {
  9. module: ts.ModuleKind.ESNext,
  10. target: ts.ScriptTarget.ES2020,
  11. },
  12. }).outputText
  13. return import(`data:text/javascript,${encodeURIComponent(compiled)}#${Math.random()}`)
  14. }
  15. const defaults = await loadConfig()
  16. assert.equal(
  17. defaults.FALLBACK_SPEAKING_API_HOST,
  18. 'https://ppt-english-speaking-api.cocorobo.cn',
  19. )
  20. assert.equal(
  21. defaults.SPEAKING_DIALOGUE_API_BASE_URL,
  22. 'https://ppt-english-speaking-api.cocorobo.cn/api/speaking/dialogue',
  23. )
  24. assert.equal(
  25. defaults.SPEAKING_CONFIG_API_BASE_URL,
  26. 'https://ppt-english-speaking-api.cocorobo.cn/api/speaking/config',
  27. )
  28. assert.equal(
  29. defaults.SPEAKING_TRANSPORT,
  30. 'websocket',
  31. )
  32. assert.equal(
  33. defaults.buildSpeakingWsUrl('/speak-stream'),
  34. 'wss://ppt-english-speaking-api.cocorobo.cn/api/speaking/dialogue/speak-stream',
  35. )
  36. const custom = await loadConfig({
  37. VITE_SPEAKING_API_HOST: 'https://example.com/',
  38. VITE_SPEAKING_TRANSPORT: 'http',
  39. })
  40. assert.equal(
  41. custom.SPEAKING_DIALOGUE_API_BASE_URL,
  42. 'https://example.com/api/speaking/dialogue',
  43. )
  44. assert.equal(
  45. custom.SPEAKING_CONFIG_API_BASE_URL,
  46. 'https://example.com/api/speaking/config',
  47. )
  48. assert.equal(
  49. custom.SPEAKING_TRANSPORT,
  50. 'http',
  51. )