import { test } from 'node:test'; import assert from 'node:assert/strict'; import { createMedia } from '../server/media.mjs'; const env = { CONNECT_SECRET: 's'.repeat(64), TIKTOK_CLIENT_KEY: 'tt-key', TIKTOK_CLIENT_SECRET: 'tt-secret' }; const jpeg = Buffer.concat([Buffer.from([0xff, 0xd8, 0xff, 0xe0]), Buffer.alloc(300)]); function harness(clock = { now: 1790000000000 }, tiktokOk = true) { const stored = new Map(); const bucket = { put: async (key, bytes, options) => { assert.equal(options.httpMetadata.contentType, 'image/jpeg'); stored.set(key, bytes); }, get: async key => { const bytes = stored.get(key); return bytes ? { body: new Blob([bytes]).stream(), size: bytes.length } : null; }, list: async () => ({ objects: [...stored.keys()].map(key => ({ key })) }), delete: async keys => { for (const key of [].concat(keys)) stored.delete(key); }, }; const fetchImpl = async (url, options) => { assert.equal(url, 'https://open.tiktokapis.com/v2/user/info/?fields=open_id'); assert.equal(options.headers.Authorization, 'Bearer tiktok-user-token'); return Response.json(tiktokOk ? { data: { user: { open_id: 'open-1' } }, error: { code: 'ok' } } : { error: { code: 'access_token_invalid' } }, { status: tiktokOk ? 200 : 401 }); }; return { media: createMedia({ env: { ...env, TIKTOK_MEDIA: bucket }, fetchImpl, now: () => clock.now }), stored, clock }; } test('a TikTok-authorized desktop can host one JPEG briefly at a verified inkspring.ai URL and delete it', async () => { const { media, stored, clock } = harness(); await assert.rejects(media.upload({ authorization: 'Bearer tiktok-user-token', bytes: Buffer.from(''.repeat(20)) }), /JPEG/); const hosted = await media.upload({ authorization: 'Bearer tiktok-user-token', bytes: jpeg }); assert.match(hosted.url, /^https:\/\/inkspring\.ai\/tiktok-media\/\d{13}-[A-Za-z0-9_-]{43}\.jpg$/); assert.equal(stored.size, 1); const file = hosted.url.split('/').pop(); assert.equal((await media.serve(file)).blob.size, jpeg.length); await assert.rejects(media.serve('../secret.jpg'), /Not found/); await assert.rejects(media.remove({ url: hosted.url, deleteKey: 'wrong' }), /Not found/); clock.now += 3 * 3600000; await assert.rejects(media.serve(file), /Not found/); clock.now -= 3 * 3600000; await media.remove(hosted); assert.equal(stored.size, 0); }); test('uploads need a working TikTok token, and stale images are swept on the next upload', async () => { await assert.rejects(harness(undefined, false).media.upload({ authorization: 'Bearer tiktok-user-token', bytes: jpeg }), /Unauthorized/); const { media, stored, clock } = harness(); await media.upload({ authorization: 'Bearer tiktok-user-token', bytes: jpeg }); clock.now += 3 * 3600000; await media.upload({ authorization: 'Bearer tiktok-user-token', bytes: jpeg }); assert.equal(stored.size, 1); }); test('legacy HTTP media endpoint is closed without reading request body, auth, or R2', async () => { const { default: handler } = await import('../api/media.js'); for (const method of ['GET', 'HEAD', 'POST', 'DELETE']) { let code, data; const headers = {}; const req = { method, get body() { throw new Error('must not read'); }, get headers() { throw new Error('must not authenticate'); } }; const res = { setHeader(name, value) { headers[name] = value; }, status(value) { code = value; return this; }, json(value) { data = value; return this; } }; await handler(req, res); assert.equal(code, 410); assert.match(data.error, /retired/); assert.equal(headers['Cache-Control'], 'no-store'); } }); test('media hosting requires the private R2 binding', () => { assert.equal(createMedia({ env }).available(), false); assert.equal(harness().media.available(), true); });