Inkspring / website / tests / media.test.mjs
| 1 | import { test } from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { createMedia } from '../server/media.mjs'; |
| 4 | const env = { CONNECT_SECRET: 's'.repeat(64), TIKTOK_CLIENT_KEY: 'tt-key', TIKTOK_CLIENT_SECRET: 'tt-secret' }; |
| 5 | const jpeg = Buffer.concat([Buffer.from([0xff, 0xd8, 0xff, 0xe0]), Buffer.alloc(300)]); |
| 6 | function harness(clock = { now: 1790000000000 }, tiktokOk = true) { |
| 7 | const stored = new Map(); |
| 8 | const bucket = { |
| 9 | put: async (key, bytes, options) => { |
| 10 | assert.equal(options.httpMetadata.contentType, 'image/jpeg'); |
| 11 | stored.set(key, bytes); |
| 12 | }, |
| 13 | get: async key => { |
| 14 | const bytes = stored.get(key); |
| 15 | return bytes ? { body: new Blob([bytes]).stream(), size: bytes.length } : null; |
| 16 | }, |
| 17 | list: async () => ({ objects: [...stored.keys()].map(key => ({ key })) }), |
| 18 | delete: async keys => { for (const key of [].concat(keys)) stored.delete(key); }, |
| 19 | }; |
| 20 | 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 }); }; |
| 21 | return { media: createMedia({ env: { ...env, TIKTOK_MEDIA: bucket }, fetchImpl, now: () => clock.now }), stored, clock }; |
| 22 | } |
| 23 | test('a TikTok-authorized desktop can host one JPEG briefly at a verified inkspring.ai URL and delete it', async () => { |
| 24 | const { media, stored, clock } = harness(); |
| 25 | await assert.rejects(media.upload({ authorization: 'Bearer tiktok-user-token', bytes: Buffer.from('<svg></svg>'.repeat(20)) }), /JPEG/); |
| 26 | const hosted = await media.upload({ authorization: 'Bearer tiktok-user-token', bytes: jpeg }); |
| 27 | assert.match(hosted.url, /^https:\/\/inkspring\.ai\/tiktok-media\/\d{13}-[A-Za-z0-9_-]{43}\.jpg$/); assert.equal(stored.size, 1); |
| 28 | const file = hosted.url.split('/').pop(); |
| 29 | assert.equal((await media.serve(file)).blob.size, jpeg.length); |
| 30 | await assert.rejects(media.serve('../secret.jpg'), /Not found/); |
| 31 | await assert.rejects(media.remove({ url: hosted.url, deleteKey: 'wrong' }), /Not found/); |
| 32 | clock.now += 3 * 3600000; await assert.rejects(media.serve(file), /Not found/); clock.now -= 3 * 3600000; |
| 33 | await media.remove(hosted); assert.equal(stored.size, 0); |
| 34 | }); |
| 35 | test('uploads need a working TikTok token, and stale images are swept on the next upload', async () => { |
| 36 | await assert.rejects(harness(undefined, false).media.upload({ authorization: 'Bearer tiktok-user-token', bytes: jpeg }), /Unauthorized/); |
| 37 | const { media, stored, clock } = harness(); |
| 38 | await media.upload({ authorization: 'Bearer tiktok-user-token', bytes: jpeg }); |
| 39 | clock.now += 3 * 3600000; await media.upload({ authorization: 'Bearer tiktok-user-token', bytes: jpeg }); |
| 40 | assert.equal(stored.size, 1); |
| 41 | }); |
| 42 | test('legacy HTTP media endpoint is closed without reading request body, auth, or R2', async () => { |
| 43 | const { default: handler } = await import('../api/media.js'); |
| 44 | for (const method of ['GET', 'HEAD', 'POST', 'DELETE']) { |
| 45 | let code, data; |
| 46 | const headers = {}; |
| 47 | const req = { method, get body() { throw new Error('must not read'); }, get headers() { throw new Error('must not authenticate'); } }; |
| 48 | const res = { setHeader(name, value) { headers[name] = value; }, status(value) { code = value; return this; }, json(value) { data = value; return this; } }; |
| 49 | await handler(req, res); |
| 50 | assert.equal(code, 410); |
| 51 | assert.match(data.error, /retired/); |
| 52 | assert.equal(headers['Cache-Control'], 'no-store'); |
| 53 | } |
| 54 | }); |
| 55 | |
| 56 | test('media hosting requires the private R2 binding', () => { |
| 57 | assert.equal(createMedia({ env }).available(), false); |
| 58 | assert.equal(harness().media.available(), true); |
| 59 | }); |