Files
streamline/control-server/test/eddy_handoff.integration.test.ts
T
2026-11-16 17:26:44 -08:00

102 lines
6.4 KiB
TypeScript

import './setup_env';
import { afterAll, expect, mock, test } from 'bun:test';
import { createHash, randomBytes } from 'node:crypto';
// Real PostgreSQL only (see docs/eddy/client-sso.md); without TEST_DATABASE_URL nothing below is
// imported or mocked, so the ordinary suite is untouched.
const run = process.env.TEST_DATABASE_URL ? test : test.skip;
const EMAIL = 'handoff-employee@bytedance.com';
const featureIds: string[] = [];
let ctx: any = null;
if (process.env.TEST_DATABASE_URL) {
mock.module('../src/sso', () => ({ resolveSsoEmail: async (h: Headers) => h.get('x-tt-session-v2') === 'fixture-handoff' ? EMAIL : null }));
const { eddyApi } = await import('../src/routes_eddy');
const { db, closePool } = await import('../src/db_client');
const s = await import('../src/schema');
const { eq, inArray } = await import('drizzle-orm');
const { readProductProfiles, saveProductProfiles } = await import('../src/product_profiles');
ctx = { eddyApi, db, closePool, s, eq, inArray, readProductProfiles, saveProductProfiles, profiles: await readProductProfiles() };
}
const post = (path: string, body: unknown, headers: Record<string, string> = {}) => ctx.eddyApi.request(path, {
method: 'POST', headers: { 'content-type': 'application/json', ...headers }, body: JSON.stringify(body),
});
async function session() {
const verifier = randomBytes(32).toString('base64url');
const body = { state: randomBytes(32).toString('base64url'), code_challenge: createHash('sha256').update(verifier).digest('base64url'),
code_challenge_method: 'S256', redirect_uri: 'http://127.0.0.1:49152/eddy/sso/callback' };
const redirect = new URL((await (await post('/auth/authorize', body, { 'x-tt-session-v2': 'fixture-handoff' })).json() as any).redirect);
const token: any = await (await post('/auth/token', { code: redirect.searchParams.get('code'), code_verifier: verifier, redirect_uri: body.redirect_uri })).json();
return token.access_token as string;
}
const zip = Buffer.from('PK fixture handoff bundle').toString('base64');
const ship = (baseline: string, headers: Record<string, string>) =>
post('/handoffs', { manifest: { task: 't-' + baseline, title: 'Handoff fixture ' + baseline, baseline, area: 'camera', registryKeys: ['edit:x'] }, zip_b64: zip }, headers);
async function handoffCount() { return (await ctx.db.select().from(ctx.s.eddy_handoffs).where(ctx.eq(ctx.s.eddy_handoffs.author, 'handoff-employee'))).length; }
run('a handoff needs a person; API keys, X-Eddy-User and stale sessions create nothing', async () => {
for (const headers of [{}, { 'x-eddy-user': 'handoff-employee' }, { authorization: 'Bearer some-open-platform-key' },
{ authorization: 'Bearer eddy_sso_' + 'x'.repeat(43) }]) {
const r = await ship('tiktokm-47.4.0-474000-5e813df6', headers);
expect(r.status).toBe(401);
expect((await r.json() as any).code).toBe('eddy_login_required');
}
expect(await handoffCount()).toBe(0);
});
run('the Eddy session owns the feature, the baseline picks the product, the zip link resolves', async () => {
const token = await session();
const r = await ship('tiktokm-47.4.0-474000-5e813df6', { authorization: 'Bearer ' + token, 'x-eddy-user': 'spoofed' });
expect(r.status).toBe(200);
const body: any = await r.json();
expect(body.featureError).toBeNull();
featureIds.push(body.featureId);
const [feature] = await ctx.db.select().from(ctx.s.features).where(ctx.eq(ctx.s.features.id, body.featureId));
expect(feature.creatorEmail).toBe(EMAIL);
expect(JSON.parse(feature.watchers)).toContain(EMAIL);
expect(JSON.parse(feature.productProfile).profileId).toBe('tiktok');
expect(feature.description).toContain('作者 handoff-employee');
expect(feature.remark).toBe('Eddy handoff ' + body.id);
// People open these links: the panel host over https, not the request origin.
expect(body.url).toBe('https://streamline.bytedance.net/?feature_id=' + body.featureId);
const [href] = feature.description.match(/https?:\/\/\S+\/api\/eddy\/handoffs\/\S+\/zip/);
expect(href).toBe(`https://streamline.bytedance.net/api/eddy/handoffs/${body.id}/zip`);
expect(feature.description).toContain(`https://streamline-internal-api.bytedance.net/api/eddy/handoffs/${body.id}/zip`);
expect(feature.description).toContain(createHash('sha256').update(Buffer.from(zip, 'base64')).digest('hex'));
const link = new URL(href);
const [row] = await ctx.db.select().from(ctx.s.eddy_handoffs).where(ctx.eq(ctx.s.eddy_handoffs.id, body.id));
expect(row.featureId).toBe(body.featureId);
const download = await ctx.eddyApi.request(link.pathname.replace('/api/eddy', ''));
expect(download.status).toBe(200);
expect(Buffer.from(await download.arrayBuffer()).toString('base64')).toBe(zip);
});
run('a baseline whose product is unknown or disabled is refused before anything is stored; enabled Notes routes to notes/develop', async () => {
const headers = { authorization: 'Bearer ' + await session() };
const before = await handoffCount();
expect((await ship('lemon8-1.0.0-1-abcdef01', headers)).status).toBe(400);
const profiles = await ctx.readProductProfiles();
const notesOff = { ...profiles, profiles: profiles.profiles.map((p: any) => p.id === 'notes' ? { ...p, enabled: false } : p) };
const off = await ctx.saveProductProfiles(notesOff.profiles, profiles.revision);
expect((await ship('notes-15.1.0-151000-2e05a02f', headers)).status).toBe(400);
expect(await handoffCount()).toBe(before);
await ctx.saveProductProfiles(off.profiles.map((p: any) => p.id === 'notes' ? { ...p, enabled: true } : p), off.revision);
const r = await ship('notes-15.1.0-151000-2e05a02f', headers);
expect(r.status).toBe(200);
const body: any = await r.json();
featureIds.push(body.featureId);
const [feature] = await ctx.db.select().from(ctx.s.features).where(ctx.eq(ctx.s.features.id, body.featureId));
expect(JSON.parse(feature.productProfile).profileId).toBe('notes');
expect(feature.baseRef).toBe('notes/develop');
});
afterAll(async () => {
if (!ctx) return;
const { db, s, eq, inArray } = ctx;
await db.delete(s.eddy_handoffs).where(eq(s.eddy_handoffs.author, 'handoff-employee'));
if (featureIds.length) await db.delete(s.features).where(inArray(s.features.id, featureIds));
await db.delete(s.eddy_sso_codes).where(eq(s.eddy_sso_codes.email, EMAIL));
await db.delete(s.eddy_sso_sessions).where(eq(s.eddy_sso_sessions.email, EMAIL));
await ctx.saveProductProfiles(ctx.profiles.profiles, (await ctx.readProductProfiles()).revision);
await ctx.closePool();
});