| 1 | import { test } from 'node:test' |
| 2 | import assert from 'node:assert/strict' |
| 3 | import { selection, download, MAX_BYTES } from './google-import.mjs' |
| 4 | |
| 5 | const item = { id: 'chosen-file_123', name: '../A\\B', mimeType: 'application/vnd.google-apps.document' } |
| 6 | test('selection only permits native file IDs on the fixed Google export origin', () => { |
| 7 | const chosen = selection('docs', item) |
| 8 | assert.equal(chosen.name, '.._A_B.docx') |
| 9 | assert.equal(new URL(chosen.url).origin, 'https://www.googleapis.com') |
| 10 | assert.equal(new URL(chosen.url).pathname, '/drive/v3/files/chosen-file_123/export') |
| 11 | for (const bad of [{...item,id:undefined},{...item,id:'../private'},{...item,id:'https://evil.test/x'},{...item,mimeType:'application/vnd.google-apps.shortcut'},null]) { |
| 12 | assert.throws(() => selection('docs',bad), /unavailable/) |
| 13 | } |
| 14 | assert.throws(() => selection('wrong',item)) |
| 15 | }) |
| 16 | |
| 17 | test('export uses bearer header only, no credentials or redirects, and returns a File', async () => { |
| 18 | const signal = new AbortController().signal |
| 19 | let calls = 0 |
| 20 | const file = await download('docs', item, 'private-token', signal, async (url, init) => { |
| 21 | calls++ |
| 22 | assert(!url.includes('private-token')) |
| 23 | assert.equal(init.headers.Authorization, 'Bearer private-token') |
| 24 | assert.equal(init.credentials, 'omit') |
| 25 | assert.equal(init.redirect, 'error') |
| 26 | assert.equal(init.signal, signal) |
| 27 | return new Response(new Uint8Array([80,75,1,2])) |
| 28 | }) |
| 29 | assert.equal(calls,1) |
| 30 | assert.equal(file.name,'.._A_B.docx') |
| 31 | assert.equal(file.size,4) |
| 32 | assert.equal(file.type,'application/vnd.openxmlformats-officedocument.wordprocessingml.document') |
| 33 | }) |
| 34 | |
| 35 | test('size limit is enforced on actual bytes even without or with a false Content-Length', async () => { |
| 36 | for (const headers of [{}, {'Content-Length':'1'}]) { |
| 37 | let cancelled = false |
| 38 | const stream = new ReadableStream({pull(c){c.enqueue(new Uint8Array(MAX_BYTES+1))},cancel(){cancelled=true}}) |
| 39 | await assert.rejects(download('docs',item,'token',undefined,async()=>new Response(stream,{headers})), /tooLarge/) |
| 40 | assert(cancelled) |
| 41 | } |
| 42 | await assert.rejects(download('docs',item,'token',undefined,async()=>new Response('x',{headers:{'Content-Length':String(MAX_BYTES+1)}})),/tooLarge/) |
| 43 | }) |
| 44 | |
| 45 | test('Google error messages cannot leak through and split size refusals retain their meaning', async () => { |
| 46 | const body = JSON.stringify({error:{message:'secret document title',errors:[{reason:'exportSizeLimitExceeded'}]}}) |
| 47 | const chunks = [body.slice(0,13),body.slice(13)] |
| 48 | const stream = new ReadableStream({start(c){ for(const s of chunks)c.enqueue(new TextEncoder().encode(s));c.close()}}) |
| 49 | await assert.rejects(download('docs',item,'token',undefined,async()=>new Response(stream,{status:403})), /^Error: tooLarge$/) |
| 50 | for (const [status,code] of [[401,'denied'],[403,'denied'],[404,'unavailable'],[429,'unavailable'],[500,'unavailable']]) { |
| 51 | await assert.rejects(download('docs',item,'token',undefined,async()=>new Response('private data',{status})),new RegExp('^Error: '+code+'$')) |
| 52 | } |
| 53 | await assert.rejects(download('docs',item,'token',undefined,async()=>new Response('')),/^Error: failed$/) |
| 54 | }) |
| 55 | |
| 56 | test('an invalid selection is rejected before any network request', async () => { |
| 57 | await assert.rejects(download('docs',{...item,id:'foo/bar'},'token',undefined,()=>assert.fail('must not fetch')),/unavailable/) |
| 58 | }) |
| 59 | |