|
| 1 | +// Streaming blob reads by key/path — repo.readBlobStream + sheet.getAttachmentStream. |
| 2 | +// See specs/behaviors/attachments.md#streaming-reads-by-keypath. |
| 3 | + |
| 4 | +import { mkdir, writeFile } from 'node:fs/promises'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import type { Readable } from 'node:stream'; |
| 7 | + |
| 8 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 9 | + |
| 10 | +import { NotFoundError, RefError } from './errors.js'; |
| 11 | +import { openRepo, type Repository } from './repository.js'; |
| 12 | +import { testRepo, type TestRepoHandle } from './test-helpers/test-repo.js'; |
| 13 | + |
| 14 | +const handles: TestRepoHandle[] = []; |
| 15 | +afterEach(async () => { |
| 16 | + while (handles.length > 0) { |
| 17 | + const h = handles.pop(); |
| 18 | + if (h) await h.cleanup(); |
| 19 | + } |
| 20 | +}); |
| 21 | + |
| 22 | +const USERS = `[gitsheet] |
| 23 | +root = 'users' |
| 24 | +path = '\${{ slug }}' |
| 25 | +`; |
| 26 | + |
| 27 | +async function seededRepo(): Promise<{ fixture: TestRepoHandle; repo: Repository }> { |
| 28 | + const fixture = await testRepo({ withInitialCommit: true }); |
| 29 | + handles.push(fixture); |
| 30 | + await mkdir(join(fixture.path, '.gitsheets'), { recursive: true }); |
| 31 | + await writeFile(join(fixture.path, '.gitsheets', 'users.toml'), USERS); |
| 32 | + await fixture.git('add', '.gitsheets/'); |
| 33 | + await fixture.git('commit', '-m', 'add users sheet'); |
| 34 | + const repo = await openRepo({ gitDir: fixture.gitDir }); |
| 35 | + return { fixture, repo }; |
| 36 | +} |
| 37 | + |
| 38 | +async function collect(stream: Readable): Promise<Buffer> { |
| 39 | + const chunks: Buffer[] = []; |
| 40 | + for await (const chunk of stream) { |
| 41 | + chunks.push(chunk as Buffer); |
| 42 | + } |
| 43 | + return Buffer.concat(chunks); |
| 44 | +} |
| 45 | + |
| 46 | +/** Binary bytes (not valid UTF-8) to prove byte fidelity end-to-end. */ |
| 47 | +const BINARY = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x00, 0xff, 0xfe, 0x0a, 0x1b]); |
| 48 | + |
| 49 | +async function commitAvatar(repo: Repository, bytes: Buffer): Promise<void> { |
| 50 | + await repo.transact({ message: 'avatar' }, async (tx) => { |
| 51 | + const sheet = tx.sheet('users'); |
| 52 | + await sheet.upsert({ slug: 'jane' }); |
| 53 | + const blob = await repo.writeBlob(bytes); |
| 54 | + await sheet.setAttachment('jane', 'avatar.png', blob); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +describe('repo.readBlobStream', () => { |
| 59 | + it('streams byte-identical content for a committed attachment', async () => { |
| 60 | + const { repo } = await seededRepo(); |
| 61 | + await commitAvatar(repo, BINARY); |
| 62 | + |
| 63 | + const stream = await repo.readBlobStream('HEAD', 'users/jane/avatar.png'); |
| 64 | + expect(Buffer.compare(await collect(stream), BINARY)).toBe(0); |
| 65 | + }); |
| 66 | + |
| 67 | + it('resolves the ref at call time — a later commit is immediately visible', async () => { |
| 68 | + const { repo } = await seededRepo(); |
| 69 | + await commitAvatar(repo, Buffer.from('v1')); |
| 70 | + await commitAvatar(repo, Buffer.from('v2')); |
| 71 | + |
| 72 | + const stream = await repo.readBlobStream('HEAD', 'users/jane/avatar.png'); |
| 73 | + expect((await collect(stream)).toString()).toBe('v2'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('throws NotFoundError(record_not_found) for a missing path', async () => { |
| 77 | + const { repo } = await seededRepo(); |
| 78 | + const err = await repo.readBlobStream('HEAD', 'users/nope/avatar.png').catch((e: unknown) => e); |
| 79 | + expect(err).toBeInstanceOf(NotFoundError); |
| 80 | + expect((err as NotFoundError).code).toBe('record_not_found'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('throws NotFoundError(record_not_found) for a directory path', async () => { |
| 84 | + const { repo } = await seededRepo(); |
| 85 | + await commitAvatar(repo, BINARY); |
| 86 | + const err = await repo.readBlobStream('HEAD', 'users/jane').catch((e: unknown) => e); |
| 87 | + expect(err).toBeInstanceOf(NotFoundError); |
| 88 | + }); |
| 89 | + |
| 90 | + it('throws RefError(ref_not_found) for a ref that does not resolve', async () => { |
| 91 | + const { repo } = await seededRepo(); |
| 92 | + const err = await repo.readBlobStream('no-such-ref', 'users/jane/avatar.png').catch((e: unknown) => e); |
| 93 | + expect(err).toBeInstanceOf(RefError); |
| 94 | + expect((err as RefError).code).toBe('ref_not_found'); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('sheet.getAttachmentStream', () => { |
| 99 | + it('streams the attachment bytes by record path without materializing the record', async () => { |
| 100 | + const { repo } = await seededRepo(); |
| 101 | + await commitAvatar(repo, BINARY); |
| 102 | + const users = await repo.openSheet('users'); |
| 103 | + |
| 104 | + const stream = await users.getAttachmentStream('jane', 'avatar.png'); |
| 105 | + expect(stream).not.toBeNull(); |
| 106 | + expect(Buffer.compare(await collect(stream!), BINARY)).toBe(0); |
| 107 | + }); |
| 108 | + |
| 109 | + it('accepts a record object too', async () => { |
| 110 | + const { repo } = await seededRepo(); |
| 111 | + await commitAvatar(repo, BINARY); |
| 112 | + const users = await repo.openSheet('users'); |
| 113 | + |
| 114 | + const stream = await users.getAttachmentStream({ slug: 'jane' }, 'avatar.png'); |
| 115 | + expect(Buffer.compare(await collect(stream!), BINARY)).toBe(0); |
| 116 | + }); |
| 117 | + |
| 118 | + it('returns null when the attachment is absent', async () => { |
| 119 | + const { repo } = await seededRepo(); |
| 120 | + await commitAvatar(repo, BINARY); |
| 121 | + const users = await repo.openSheet('users'); |
| 122 | + |
| 123 | + expect(await users.getAttachmentStream('jane', 'missing.png')).toBeNull(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('reads through the fresh snapshot after this repository commits (auto-refresh)', async () => { |
| 127 | + const { repo } = await seededRepo(); |
| 128 | + const users = await repo.openSheet('users'); |
| 129 | + |
| 130 | + await commitAvatar(repo, Buffer.from('v1')); |
| 131 | + let stream = await users.getAttachmentStream('jane', 'avatar.png'); |
| 132 | + expect((await collect(stream!)).toString()).toBe('v1'); |
| 133 | + |
| 134 | + await commitAvatar(repo, Buffer.from('v2')); |
| 135 | + stream = await users.getAttachmentStream('jane', 'avatar.png'); |
| 136 | + expect((await collect(stream!)).toString()).toBe('v2'); |
| 137 | + }); |
| 138 | +}); |
0 commit comments