Add product source job resolver
Build, Push and Deploy / build (push) Successful in 7s
Build, Push and Deploy / deploy (push) Successful in 18s

This commit is contained in:
2026-09-13 18:00:35 +08:00
parent 807b1252ad
commit 293e346f0a
19 changed files with 269 additions and 64 deletions
+62
View File
@@ -3,6 +3,68 @@ import assert from 'node:assert/strict';
import { createRealtimeServer } from './realtime-server.mjs';
import WebSocket from 'ws';
const productDetail = {
product_id: 'prod_abc123',
versions: [
{
version_id: 'ver_old',
created_at: '2026-09-13T08:00:00Z',
wordcloud_archives: [
{ source_job_id: 'job_old', created_at: '2026-09-13T08:00:00Z' },
],
},
{
version_id: 'ver_new',
created_at: '2026-09-13T09:00:00Z',
wordcloud_archives: [
{ source_job_id: 'job_new', created_at: '2026-09-13T09:00:00Z' },
],
},
],
};
test('resolves a product id to the latest source job id', async () => {
const requests = [];
const fetchImpl = async (input, init) => {
requests.push({ input: String(input), init });
if (input === 'http://wordcloud.test/api/login') {
return new Response(JSON.stringify({ token: 'product-token' }), { status: 200 });
}
if (input === 'http://wordcloud.test/api/products/prod_abc123') {
return new Response(JSON.stringify(productDetail), { status: 200 });
}
return new Response('not found', { status: 404 });
};
const server = createRealtimeServer(0, {
upstreamUrl: 'http://wordcloud.test',
adminPassword: 'test-password',
tokenSecret: 'test-secret',
fetchImpl,
});
await new Promise((resolve) => server.listen(resolve));
const { port } = server.address();
try {
const response = await fetch(`http://127.0.0.1:${port}/api/products/prod_abc123/source-job`);
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), {
product_id: 'prod_abc123',
source_job_id: 'job_new',
});
assert.deepEqual(requests.map((request) => request.input), [
'http://wordcloud.test/api/login',
'http://wordcloud.test/api/products/prod_abc123',
]);
const loginBody = JSON.parse(requests[0].init.body);
assert.equal(loginBody.password, 'test-password');
assert.equal(requests[1].init.headers.Authorization, 'Bearer product-token');
} finally {
server.close();
}
});
test('broadcasts focus events to other connected clients', async () => {
const server = createRealtimeServer(0);
await new Promise((resolve) => server.listen(resolve));