import { test } from 'node:test' import assert from 'node:assert/strict' import { selection, download, MAX_BYTES } from './google-import.mjs' const item = { id: 'chosen-file_123', name: '../A\\B', mimeType: 'application/vnd.google-apps.document' } test('selection only permits native file IDs on the fixed Google export origin', () => { const chosen = selection('docs', item) assert.equal(chosen.name, '.._A_B.docx') assert.equal(new URL(chosen.url).origin, 'https://www.googleapis.com') assert.equal(new URL(chosen.url).pathname, '/drive/v3/files/chosen-file_123/export') for (const bad of [{...item,id:undefined},{...item,id:'../private'},{...item,id:'https://evil.test/x'},{...item,mimeType:'application/vnd.google-apps.shortcut'},null]) { assert.throws(() => selection('docs',bad), /unavailable/) } assert.throws(() => selection('wrong',item)) }) test('export uses bearer header only, no credentials or redirects, and returns a File', async () => { const signal = new AbortController().signal let calls = 0 const file = await download('docs', item, 'private-token', signal, async (url, init) => { calls++ assert(!url.includes('private-token')) assert.equal(init.headers.Authorization, 'Bearer private-token') assert.equal(init.credentials, 'omit') assert.equal(init.redirect, 'error') assert.equal(init.signal, signal) return new Response(new Uint8Array([80,75,1,2])) }) assert.equal(calls,1) assert.equal(file.name,'.._A_B.docx') assert.equal(file.size,4) assert.equal(file.type,'application/vnd.openxmlformats-officedocument.wordprocessingml.document') }) test('size limit is enforced on actual bytes even without or with a false Content-Length', async () => { for (const headers of [{}, {'Content-Length':'1'}]) { let cancelled = false const stream = new ReadableStream({pull(c){c.enqueue(new Uint8Array(MAX_BYTES+1))},cancel(){cancelled=true}}) await assert.rejects(download('docs',item,'token',undefined,async()=>new Response(stream,{headers})), /tooLarge/) assert(cancelled) } await assert.rejects(download('docs',item,'token',undefined,async()=>new Response('x',{headers:{'Content-Length':String(MAX_BYTES+1)}})),/tooLarge/) }) test('Google error messages cannot leak through and split size refusals retain their meaning', async () => { const body = JSON.stringify({error:{message:'secret document title',errors:[{reason:'exportSizeLimitExceeded'}]}}) const chunks = [body.slice(0,13),body.slice(13)] const stream = new ReadableStream({start(c){ for(const s of chunks)c.enqueue(new TextEncoder().encode(s));c.close()}}) await assert.rejects(download('docs',item,'token',undefined,async()=>new Response(stream,{status:403})), /^Error: tooLarge$/) for (const [status,code] of [[401,'denied'],[403,'denied'],[404,'unavailable'],[429,'unavailable'],[500,'unavailable']]) { await assert.rejects(download('docs',item,'token',undefined,async()=>new Response('private data',{status})),new RegExp('^Error: '+code+'$')) } await assert.rejects(download('docs',item,'token',undefined,async()=>new Response('')),/^Error: failed$/) }) test('an invalid selection is rejected before any network request', async () => { await assert.rejects(download('docs',{...item,id:'foo/bar'},'token',undefined,()=>assert.fail('must not fetch')),/unavailable/) })