seed-avatar-fixture.mjs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Prepare only the disposable workspace fixture for the iOS cross-client test.
  2. // Start tests/fixtures/workspace-server.ts on 18804 / 19124 before running this.
  3. import { readFileSync } from 'node:fs'
  4. const base = 'http://127.0.0.1:18804'
  5. let cookie = '', csrf = ''
  6. async function request(path, body, method) {
  7. const response = await fetch(base + path, {
  8. method: method ?? (body ? 'POST' : 'GET'),
  9. headers: { Origin: base, Cookie: cookie, 'X-CSRF-Token': csrf, 'Content-Type': 'application/json' },
  10. body: body ? JSON.stringify(body) : undefined,
  11. })
  12. const cookies = response.headers.getSetCookie()
  13. if (cookies.length) cookie = cookies.map(value => value.split(';')[0]).join('; ')
  14. if (!response.ok) throw new Error(`Fixture ${path}: ${response.status}`)
  15. return response.json()
  16. }
  17. csrf = (await request('/api/app/bootstrap')).csrfToken
  18. await request('/api/app/login', { username: 'fixture', password: 'fixture-pass' })
  19. csrf = (await request('/api/app/bootstrap')).csrfToken
  20. const source = (await request('/api/app/agents/sources')).sources[0]
  21. if (!source) throw new Error('No isolated fixture profile')
  22. const agents = (await request('/api/app/agents')).agents
  23. const photo = 'data:image/png;base64,' + readFileSync(new URL('../public/brand/AppIcon-1024.png', import.meta.url)).toString('base64')
  24. await request('/api/app/account/avatar', { avatar: photo }, 'PUT')
  25. for (const [name, image] of [['跨端造型', false], ['跨端照片', true]]) {
  26. const avatar = 'yaoyao-avatar:v2:' + JSON.stringify({
  27. version: 2, avatarMode: image ? 'image' : 'mascot', shape: 'circle', color: '#1488ff',
  28. expression: 'proud', bodyId: image ? null : 'star', imageCrop: 'circle',
  29. ...(image ? { imageDataURL: photo } : {}),
  30. })
  31. const existing = agents.find(agent => agent.name === name)
  32. if (existing) await request(`/api/app/agents/${existing.id}`, { avatar }, 'PATCH')
  33. else await request('/api/app/agents', { name, avatar, nodeId: source.nodeId, profile: source.profile })
  34. }
  35. console.log('Prepared two avatar fixtures on 127.0.0.1:18804')