| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import assert from 'node:assert/strict';
- import { spawn } from 'node:child_process';
- import { createServer } from 'node:http';
- import test from 'node:test';
- test('one worker tick requests every configured data set exactly once', async () => {
- const requests = [];
- const server = createServer((request, response) => {
- let body = '';
- request.setEncoding('utf8');
- request.on('data', (chunk) => { body += chunk; });
- request.on('end', () => {
- requests.push({ url: request.url, body: JSON.parse(body) });
- response.writeHead(200, { 'Content-Type': 'application/json' });
- response.end(JSON.stringify({ success: true, data: { processed: 0, failures: 0, hasMore: false } }));
- });
- });
- await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
- const address = server.address();
- assert(address && typeof address === 'object');
- const datasets = ['learning-records', 'appointments', 'lessons'];
- const child = spawn(process.execPath, ['scripts/run-legacy-sync-worker.mjs', '--once'], {
- cwd: process.cwd(),
- env: {
- ...process.env,
- XIAOSHU_FUNCTION_URL: `http://127.0.0.1:${address.port}`,
- XIAOSHU_SYNC_OPERATOR_TOKEN: 'test-session',
- XIAOSHU_SYNC_DATASETS: datasets.join(','),
- XIAOSHU_SYNC_HEALTH_PORT: '0',
- },
- stdio: ['ignore', 'pipe', 'pipe'],
- });
- let stderr = '';
- child.stderr.on('data', (chunk) => { stderr += chunk; });
- const exitCode = await new Promise((resolve) => child.on('close', resolve));
- await new Promise((resolve) => server.close(resolve));
- assert.equal(exitCode, 0, stderr);
- assert.deepEqual(requests.map((entry) => entry.url), datasets.map(() => '/xiaoshu/ops/gateway-v3'));
- assert.deepEqual(requests.map((entry) => entry.body.params.dataset).sort(), datasets.slice().sort());
- assert(requests.every((entry) => entry.body.params.operation === 'ops/sync/run'));
- assert(requests.every((entry) => entry.body.token === 'test-session'));
- });
|