legacy-sync-worker.test.mjs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import assert from 'node:assert/strict';
  2. import { spawn } from 'node:child_process';
  3. import { createServer } from 'node:http';
  4. import test from 'node:test';
  5. test('one worker tick requests every configured data set exactly once', async () => {
  6. const requests = [];
  7. const server = createServer((request, response) => {
  8. let body = '';
  9. request.setEncoding('utf8');
  10. request.on('data', (chunk) => { body += chunk; });
  11. request.on('end', () => {
  12. requests.push({ url: request.url, body: JSON.parse(body) });
  13. response.writeHead(200, { 'Content-Type': 'application/json' });
  14. response.end(JSON.stringify({ success: true, data: { processed: 0, failures: 0, hasMore: false } }));
  15. });
  16. });
  17. await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
  18. const address = server.address();
  19. assert(address && typeof address === 'object');
  20. const datasets = ['learning-records', 'appointments', 'lessons'];
  21. const child = spawn(process.execPath, ['scripts/run-legacy-sync-worker.mjs', '--once'], {
  22. cwd: process.cwd(),
  23. env: {
  24. ...process.env,
  25. XIAOSHU_FUNCTION_URL: `http://127.0.0.1:${address.port}`,
  26. XIAOSHU_SYNC_OPERATOR_TOKEN: 'test-session',
  27. XIAOSHU_SYNC_DATASETS: datasets.join(','),
  28. XIAOSHU_SYNC_HEALTH_PORT: '0',
  29. },
  30. stdio: ['ignore', 'pipe', 'pipe'],
  31. });
  32. let stderr = '';
  33. child.stderr.on('data', (chunk) => { stderr += chunk; });
  34. const exitCode = await new Promise((resolve) => child.on('close', resolve));
  35. await new Promise((resolve) => server.close(resolve));
  36. assert.equal(exitCode, 0, stderr);
  37. assert.deepEqual(requests.map((entry) => entry.url), datasets.map(() => '/xiaoshu/ops/gateway-v3'));
  38. assert.deepEqual(requests.map((entry) => entry.body.params.dataset).sort(), datasets.slice().sort());
  39. assert(requests.every((entry) => entry.body.params.operation === 'ops/sync/run'));
  40. assert(requests.every((entry) => entry.body.token === 'test-session'));
  41. });