| 1 | import { test } from 'node:test' |
| 2 | import assert from 'node:assert/strict' |
| 3 | |
| 4 | import { createUploader, checkFile, checkClip, clipMessage, sha256Hex, refusal, UploadError, MAX_BYTES, MAX_CLIP_BYTES } from './uploads.mjs' |
| 5 | import { checkPoster, MAX_POSTER_BYTES } from './clip-poster.mjs' |
| 6 | |
| 7 | // fakeFile is enough of a File for this module: a name, a type, a size, |
| 8 | // and bytes when something wants to hash them. |
| 9 | function fakeFile (name, type, bytes = new Uint8Array([1, 2, 3])) { |
| 10 | return { |
| 11 | name, |
| 12 | type, |
| 13 | size: bytes.length, |
| 14 | arrayBuffer: async () => bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | // rig wires an uploader over fakes and records what each step was |
| 19 | // asked, so a test can assert on the ORDER as well as the arguments. |
| 20 | function rig (over = {}) { |
| 21 | const calls = [] |
| 22 | const states = [] |
| 23 | const uploader = createUploader({ |
| 24 | sign: async body => { calls.push(['sign', body]); return { ref: 'abc123', url: '/put/abc123', headers: { 'Content-Type': body.type } } }, |
| 25 | put: async (url, file, headers) => { calls.push(['put', url, file.name, headers]) }, |
| 26 | register: async body => { calls.push(['register', body]); return { id: 'a1', ref: 'abc123', name: body.name, width: body.width, height: body.height, alt: body.alt } }, |
| 27 | applyOp: async record => { calls.push(['op', record]) }, |
| 28 | measure: async () => ({ width: 40, height: 30 }), |
| 29 | hash: async () => 'abc123', |
| 30 | onState: (s, d) => states.push([s, d]), |
| 31 | ...over |
| 32 | }) |
| 33 | return { uploader, calls, states } |
| 34 | } |
| 35 | |
| 36 | test('the state machine runs choose → sign → put → register → op', async () => { |
| 37 | const { uploader, calls, states } = rig() |
| 38 | const record = await uploader.upload(fakeFile('chart.png', 'image/png'), { alt: 'A bar chart' }) |
| 39 | |
| 40 | assert.deepEqual(calls.map(c => c[0]), ['sign', 'put', 'register', 'op']) |
| 41 | assert.deepEqual(states.map(s => s[0]), ['sign', 'put', 'register', 'op', 'done']) |
| 42 | assert.deepEqual(calls[0][1], { name: 'chart.png', size: 3, type: 'image/png', hash: 'abc123' }) |
| 43 | assert.equal(calls[1][1], '/put/abc123') |
| 44 | assert.deepEqual(calls[2][1], { |
| 45 | ref: 'abc123', name: 'chart.png', width: 40, height: 30, alt: 'A bar chart', decorative: false |
| 46 | }) |
| 47 | // The op is applied with the SERVER's record, never with what the |
| 48 | // client thought it uploaded. |
| 49 | assert.deepEqual(calls[3][1], record) |
| 50 | assert.equal(record.id, 'a1') |
| 51 | }) |
| 52 | |
| 53 | test('a file the deck will not take never reaches the network', async () => { |
| 54 | const { uploader, calls } = rig() |
| 55 | await assert.rejects(() => uploader.upload(fakeFile('notes.pdf', 'application/pdf'), { alt: 'x' }), |
| 56 | err => err instanceof UploadError && err.code === 'type') |
| 57 | |
| 58 | const big = fakeFile('huge.png', 'image/png') |
| 59 | big.size = MAX_BYTES + 1 |
| 60 | await assert.rejects(() => uploader.upload(big, { alt: 'x' }), |
| 61 | err => err.code === 'toolarge') |
| 62 | |
| 63 | assert.deepEqual(calls, [], 'nothing was signed, put or registered') |
| 64 | }) |
| 65 | |
| 66 | test('alt text is required before the bytes move, unless the image is decorative', async () => { |
| 67 | const { uploader, calls } = rig() |
| 68 | await assert.rejects(() => uploader.upload(fakeFile('x.png', 'image/png'), { alt: ' ' }), |
| 69 | err => err.code === 'altfirst') |
| 70 | assert.deepEqual(calls, []) |
| 71 | |
| 72 | await uploader.upload(fakeFile('x.png', 'image/png'), { alt: '', decorative: true }) |
| 73 | assert.deepEqual(calls.map(c => c[0]), ['sign', 'put', 'register', 'op']) |
| 74 | assert.equal(calls[2][1].decorative, true) |
| 75 | assert.equal(calls[2][1].alt, '') |
| 76 | }) |
| 77 | |
| 78 | test('a step that fails stops the ones after it and reports its own code', async () => { |
| 79 | for (const [failing, code] of [['sign', 'no_store'], ['put', 'failed'], ['register', 'unsafe_svg']]) { |
| 80 | const { uploader, calls, states } = rig({ |
| 81 | [failing]: async () => { throw new UploadError(code, 'in words') } |
| 82 | }) |
| 83 | await assert.rejects(() => uploader.upload(fakeFile('x.png', 'image/png'), { alt: 'a' }), |
| 84 | err => err.code === code && err.serverMessage === 'in words') |
| 85 | assert.ok(!calls.some(c => c[0] === 'op'), `${failing}: the op ran anyway`) |
| 86 | assert.equal(states.at(-1)[0], 'error') |
| 87 | } |
| 88 | }) |
| 89 | |
| 90 | test('a failure that is not ours still becomes an UploadError', async () => { |
| 91 | const { uploader } = rig({ put: async () => { throw new TypeError('network down') } }) |
| 92 | await assert.rejects(() => uploader.upload(fakeFile('x.png', 'image/png'), { alt: 'a' }), |
| 93 | err => err instanceof UploadError && err.code === 'failed' && err.serverMessage === 'network down') |
| 94 | }) |
| 95 | |
| 96 | test('a second pick while one is in flight is refused, not raced', async () => { |
| 97 | // The first PUT hangs until the test lets it go; every later one |
| 98 | // resolves at once, so the "and then the next one works" half of this |
| 99 | // is not itself a hang. |
| 100 | const held = [] |
| 101 | const { uploader, calls } = rig({ |
| 102 | put: () => (held.length ? Promise.resolve() : new Promise(r => held.push(r))) |
| 103 | }) |
| 104 | const first = uploader.upload(fakeFile('a.png', 'image/png'), { alt: 'a' }) |
| 105 | await assert.rejects(() => uploader.upload(fakeFile('b.png', 'image/png'), { alt: 'b' }), |
| 106 | err => err.code === 'busy') |
| 107 | held[0]() |
| 108 | await first |
| 109 | await uploader.upload(fakeFile('c.png', 'image/png'), { alt: 'c' }) |
| 110 | assert.deepEqual(calls.filter(c => c[0] === 'op').length, 2, 'b.png never became an op') |
| 111 | }) |
| 112 | |
| 113 | test('an unreadable size is zero rather than a failure', async () => { |
| 114 | const { uploader, calls } = rig({ measure: async () => ({ width: 0, height: 0 }) }) |
| 115 | await uploader.upload(fakeFile('x.svg', 'image/svg+xml'), { alt: 'a' }) |
| 116 | assert.equal(calls[2][1].width, 0) |
| 117 | assert.equal(calls[2][1].height, 0) |
| 118 | }) |
| 119 | |
| 120 | test('a per-call applyOp wins, so an upload lands on the block it was chosen for', async () => { |
| 121 | const { uploader, calls } = rig() |
| 122 | const landed = [] |
| 123 | await uploader.upload(fakeFile('x.png', 'image/png'), |
| 124 | { alt: 'a', applyOp: record => landed.push(record.id) }) |
| 125 | assert.deepEqual(landed, ['a1']) |
| 126 | assert.ok(!calls.some(c => c[0] === 'op'), 'the constructor\'s applyOp ran as well') |
| 127 | }) |
| 128 | |
| 129 | test('checkFile takes exactly the four types', () => { |
| 130 | for (const type of ['image/png', 'image/jpeg', 'image/webp', 'image/svg+xml']) { |
| 131 | assert.doesNotThrow(() => checkFile(fakeFile('x', type))) |
| 132 | } |
| 133 | for (const type of ['image/gif', 'text/html', 'image/svg', '']) { |
| 134 | assert.throws(() => checkFile(fakeFile('x', type)), /type/) |
| 135 | } |
| 136 | assert.throws(() => checkFile(null), /nofile/) |
| 137 | }) |
| 138 | |
| 139 | test('sha256Hex is the hex digest of the bytes', async () => { |
| 140 | // sha256("abc"), the vector everybody knows. |
| 141 | const file = fakeFile('abc.png', 'image/png', new TextEncoder().encode('abc')) |
| 142 | assert.equal(await sha256Hex(file), |
| 143 | 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad') |
| 144 | }) |
| 145 | |
| 146 | test('refusal reads this app\'s JSON error shape, and survives one that is not', async () => { |
| 147 | const json = { status: 503, json: async () => ({ error: 'no_store', message: 'No object store.' }) } |
| 148 | const a = await refusal(json) |
| 149 | assert.equal(a.code, 'no_store') |
| 150 | assert.equal(a.serverMessage, 'No object store.') |
| 151 | |
| 152 | const html = { status: 502, json: async () => { throw new SyntaxError('not JSON') } } |
| 153 | const b = await refusal(html) |
| 154 | assert.equal(b.code, 'http_502') |
| 155 | assert.equal(b.serverMessage, '') |
| 156 | }) |
| 157 | |
| 158 | // A clip (scenes and clips spec §2, §4): a video and the picture of its |
| 159 | // first frame, which goes up first, as a decorative picture, so the |
| 160 | // video's registration can name it as its poster. |
| 161 | function clipRig (over = {}) { |
| 162 | let n = 0 |
| 163 | const r = rig({ |
| 164 | sign: async body => { r.calls.push(['sign', body]); const ref = `staged${++n}`; return { ref, url: `/put/${ref}`, headers: { 'Content-Type': body.type } } }, |
| 165 | register: async body => { r.calls.push(['register', body]); return { id: `id${body.ref}`, ref: `final${body.ref}`, name: body.name, width: body.width, height: body.height, alt: body.alt, ...(body.poster ? { poster: body.poster, duration: body.duration } : {}) } }, |
| 166 | staged: ref => `/back/${ref}`, |
| 167 | capture: async (url, name) => { r.calls.push(['capture', url]); return { file: fakeFile(name.replace(/\.\w+$/, '.webp'), 'image/webp'), width: 640, height: 360, duration: 2500 } }, |
| 168 | ...over |
| 169 | }) |
| 170 | return r |
| 171 | } |
| 172 | |
| 173 | test('a clip is staged, its first frame drawn from the staged bytes and registered, then the video naming it', async () => { |
| 174 | const { uploader, calls } = clipRig() |
| 175 | const landed = [] |
| 176 | const record = await uploader.uploadClip(fakeFile('demo.webm', 'video/webm'), { applyOp: (video, poster) => landed.push([video.id, poster.id]) }) |
| 177 | assert.deepEqual(calls.map(c => c[0]), ['sign', 'put', 'capture', 'sign', 'put', 'register', 'register']) |
| 178 | assert.equal(calls[0][1].type, 'video/webm') |
| 179 | assert.equal(calls[2][1], '/back/staged1', 'the frame is drawn from the bytes the app serves back') |
| 180 | assert.equal(calls[3][1].type, 'image/webp') |
| 181 | assert.deepEqual(calls[5][1], { ref: 'staged2', name: 'demo.webp', width: 640, height: 360, alt: '', decorative: true }) |
| 182 | assert.deepEqual(calls[6][1], { ref: 'staged1', name: 'demo.webm', width: 640, height: 360, alt: '', decorative: false, poster: 'finalstaged2', duration: 2500 }) |
| 183 | assert.deepEqual(landed, [['idstaged1', 'idstaged2']]) |
| 184 | assert.equal(record.poster, 'finalstaged2') |
| 185 | }) |
| 186 | |
| 187 | test('a clip that is not an MP4 or WebM, or over 50 MiB, never reaches the network; one with no frame is never registered', async () => { |
| 188 | const { uploader, calls } = clipRig() |
| 189 | await assert.rejects(() => uploader.uploadClip(fakeFile('a.mov', 'video/quicktime')), err => err.code === 'type') |
| 190 | const big = fakeFile('big.mp4', 'video/mp4') |
| 191 | big.size = MAX_CLIP_BYTES + 1 |
| 192 | await assert.rejects(() => uploader.uploadClip(big), err => err.code === 'toolarge') |
| 193 | assert.equal(MAX_CLIP_BYTES, 50 * 1024 * 1024) |
| 194 | assert.deepEqual(calls, []) |
| 195 | // A file this browser cannot draw a first frame of is no video it can |
| 196 | // show: staged, never registered. |
| 197 | const { uploader: blind, calls: none } = clipRig({ capture: async () => { throw new Error('no decoder') } }) |
| 198 | await assert.rejects(() => blind.uploadClip(fakeFile('a.webm', 'video/webm')), err => err.code === 'type') |
| 199 | assert.deepEqual(none.map(c => c[0]), ['sign', 'put']) |
| 200 | for (const type of ['video/mp4', 'video/webm']) assert.doesNotThrow(() => checkClip(fakeFile('x', type))) |
| 201 | assert.throws(() => checkClip(fakeFile('x', 'image/png')), /type/) |
| 202 | }) |
| 203 | |
| 204 | test('a clip\'s refusal is said with its name, in the approved words', () => { |
| 205 | const labels = { clip: '{name} isn\'t an MP4 or WebM video.', clipBig: '{name} is over 50 MB.', failed: 'That upload didn\'t finish. Try again.' } |
| 206 | assert.equal(clipMessage(new UploadError('type'), labels, 'a.mov'), 'a.mov isn\'t an MP4 or WebM video.') |
| 207 | assert.equal(clipMessage(new UploadError('not_a_video', 'server words'), labels, 'b.webm'), 'b.webm isn\'t an MP4 or WebM video.') |
| 208 | assert.equal(clipMessage(new UploadError('too_large'), labels, 'c.mp4'), 'c.mp4 is over 50 MB.') |
| 209 | assert.equal(clipMessage(new UploadError('toolarge'), labels, 'c.mp4'), 'c.mp4 is over 50 MB.') |
| 210 | assert.equal(clipMessage(new UploadError('no_store', 'No store.'), labels, 'd.mp4'), labels.failed) |
| 211 | }) |
| 212 | |
| 213 | test('a poster is at most 4 MiB, checked before it goes up; too big, the upload fails in the usual words', async () => { |
| 214 | assert.equal(MAX_POSTER_BYTES, 4 * 1024 * 1024) |
| 215 | assert.doesNotThrow(() => checkPoster({ size: MAX_POSTER_BYTES })) |
| 216 | assert.throws(() => checkPoster({ size: MAX_POSTER_BYTES + 1 }), err => err instanceof UploadError && err.code === 'poster_too_large') |
| 217 | const { uploader, calls } = clipRig({ capture: async () => { checkPoster({ size: MAX_POSTER_BYTES + 1 }) } }) |
| 218 | let refused |
| 219 | await assert.rejects(() => uploader.uploadClip(fakeFile('a.webm', 'video/webm')), err => { refused = err; return err.code === 'poster_too_large' }) |
| 220 | assert.ok(!calls.some(c => c[0] === 'register'), 'nothing was registered') |
| 221 | const labels = { clip: '{name} isn\'t an MP4 or WebM video.', clipBig: '{name} is over 50 MB.', failed: 'That upload didn\'t finish. Try again.' } |
| 222 | assert.equal(clipMessage(refused, labels, 'a.webm'), labels.failed) |
| 223 | }) |
| 224 | |