Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/google

Google helpers for Gmail, Calendar, Drive, People, YouTube (including upload), and Analytics.

FollowFork

Files

drive.test.ts

62 lines · 3.0 KB · TypeScript
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { downloadFile, listFolderFiles } from './drive.ts'
import { requestGoogle } from './core.ts'

const file = new Uint8Array(5_000_000).map((_, i) => (i * 7) % 256)

function driveFetch(input: RequestInfo | URL, init?: RequestInit) {
	const url = String(input)
	const range = new Headers(init?.headers).get('range')
	if (url.includes('alt=media')) {
		const match = /bytes=(\d+)-(\d+)/.exec(range || '')
		if (!match) return new Response(file, { status: 200, headers: { 'content-type': 'application/pdf' } })
		const start = Number(match[1])
		if (start >= file.length) return new Response('', { status: 416, headers: { 'content-range': 'bytes */' + file.length } })
		const end = Math.min(Number(match[2]), file.length - 1)
		return new Response(file.slice(start, end + 1), { status: 206, headers: { 'content-type': 'application/pdf', 'content-range': `bytes ${start}-${end}/${file.length}` } })
	}
	return new Response(JSON.stringify({ files: [] }), { headers: { 'content-type': 'application/json' } })
}

test('downloadFile walks a binary file in byte-exact ranges', async () => {
	;(globalThis as any).__kodyTestFetch = driveFetch
	const out: Uint8Array[] = []
	let start = 0
	for (;;) {
		const r = await downloadFile({ account: 'personal', fileId: 'abc', start })
		assert.equal(r.totalSize, file.length)
		assert.ok(r.bytes.byteLength <= 2.5 * 1024 * 1024)
		out.push(r.bytes)
		start = r.end
		if (r.done) break
	}
	const joined = Buffer.concat(out)
	assert.equal(joined.length, file.length)
	assert.ok(joined.equals(Buffer.from(file)))
	const past = await downloadFile({ account: 'personal', fileId: 'abc', start: file.length })
	assert.equal(past.done, true)
	assert.equal(past.bytes.byteLength, 0)
})

test('requestGoogle responseType bytes keeps binary intact and api drive resolves', async () => {
	;(globalThis as any).__kodyTestFetch = driveFetch
	const bytes = await requestGoogle({ account: 'personal', api: 'drive', path: '/drive/v3/files/abc', query: { alt: 'media' }, responseType: 'bytes' })
	assert.ok(bytes instanceof Uint8Array)
	assert.equal((bytes as Uint8Array).byteLength, file.length)
	const listed = await listFolderFiles({ account: 'personal', folderId: 'f' })
	assert.deepEqual(listed, { files: [], truncated: false })
})

test('downloadFile cancels ignored Range before consuming an unbounded body', async () => {
 let cancelled=false
 ;(globalThis as any).__kodyTestFetch=()=>new Response(new ReadableStream({cancel(){cancelled=true}}),{status:200})
 await assert.rejects(downloadFile({account:'personal',fileId:'f',maxBytes:1024}),/invalid or ignored Range/)
 assert.equal(cancelled,true)
})
test('downloadFile rejects partial and oversized bodies and disposes readers',async()=>{
 for(const body of [new Uint8Array(1),new Uint8Array(4)]){
  ;(globalThis as any).__kodyTestFetch=()=>new Response(body,{status:206,headers:{'content-range':'bytes 0-2/3'}})
  await assert.rejects(downloadFile({account:'personal',fileId:'f',maxBytes:3}),/partial range|exceeds declared range/)
 }
})