Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { IDBFactory } from 'fake-indexeddb';import { resetDbForTests } from './db';import * as blobs from './blobs';
// blobs.ts talks to the real IndexedDB layer (faked here) and to the server via// apiFetch/hasServerBase, which we mock per case to drive hit/miss/error paths.const hasServerBase = vi.fn(() => true);const apiFetch = vi.fn();vi.mock('../api', () => ({ hasServerBase: () => hasServerBase(), apiFetch: (path: string, init?: RequestInit) => apiFetch(path, init),}));
function response(body: Uint8Array, init: { ok?: boolean; contentType?: string } = {}): Response { return { ok: init.ok ?? true, headers: { get: () => init.contentType ?? null }, arrayBuffer: async () => body.buffer, } as unknown as Response;}
describe('blobs', () => { beforeEach(() => { globalThis.indexedDB = new IDBFactory(); resetDbForTests(); hasServerBase.mockReturnValue(true); apiFetch.mockReset(); }); afterEach(() => resetDbForTests());
it('hashes bytes as lowercase hex sha-256, accepting a Blob or Uint8Array', async () => { const hash = await blobs.hashBytes(new Uint8Array([1, 2, 3])); expect(hash).toMatch(/^[0-9a-f]{64}$/); // Same content via the ArrayBuffer branch hashes identically. expect(await blobs.hashBytes(new Uint8Array([1, 2, 3]).buffer)).toBe(hash); });
it('stores a blob locally and reads it straight back without touching the server', async () => { const hash = await blobs.putLocal(new Blob([new Uint8Array([9, 8, 7])]), 'application/pdf'); const got = await blobs.getOrFetch(hash); expect(got).not.toBeNull(); expect(new Uint8Array(await got!.arrayBuffer())).toEqual(new Uint8Array([9, 8, 7])); expect(apiFetch).not.toHaveBeenCalled(); });
it('returns null on a local miss when no server is configured', async () => { hasServerBase.mockReturnValue(false); expect(await blobs.getOrFetch('deadbeef')).toBeNull(); expect(apiFetch).not.toHaveBeenCalled(); });
it('fetches from the server on a miss, caches the bytes, and serves the next hit locally', async () => { const bytes = new Uint8Array([4, 5, 6]); const hash = await blobs.hashBytes(bytes); apiFetch.mockResolvedValueOnce(response(bytes, { contentType: 'application/pdf' })); const first = await blobs.getOrFetch(hash); expect(new Uint8Array(await first!.arrayBuffer())).toEqual(bytes); // Cached: a second call does not hit the server again. const second = await blobs.getOrFetch(hash); expect(new Uint8Array(await second!.arrayBuffer())).toEqual(bytes); expect(apiFetch).toHaveBeenCalledTimes(1); });
it('drops a server response whose bytes do not match the requested hash', async () => { apiFetch.mockResolvedValueOnce( response(new Uint8Array([1, 1, 1]), { contentType: 'application/pdf' }), ); // The hash asks for other content; a mismatched (tampered) response is refused // and nothing is cached, so a later honest fetch can still succeed. expect(await blobs.getOrFetch('b'.repeat(64))).toBeNull(); });
it('returns null when the server responds not-ok or throws', async () => { apiFetch.mockResolvedValueOnce(response(new Uint8Array(), { ok: false })); expect(await blobs.getOrFetch('missing')).toBeNull(); apiFetch.mockRejectedValueOnce(new Error('offline')); expect(await blobs.getOrFetch('boom')).toBeNull(); });
it('pushes a locally-stored blob to the server, reporting the response ok flag', async () => { const hash = await blobs.putLocal(new Blob([new Uint8Array([1])]), 'text/plain'); apiFetch.mockResolvedValueOnce(response(new Uint8Array(), { ok: true })); expect(await blobs.pushToServer(hash)).toBe(true); const [, init] = apiFetch.mock.calls[0]; expect(init.method).toBe('PUT'); });
it('declines to push when there is no server or the blob is not stored', async () => { hasServerBase.mockReturnValue(false); expect(await blobs.pushToServer('anything')).toBe(false); hasServerBase.mockReturnValue(true); expect(await blobs.pushToServer('never-stored')).toBe(false); });
it('returns false when the push request throws', async () => { const hash = await blobs.putLocal(new Blob([new Uint8Array([2])]), 'text/plain'); apiFetch.mockRejectedValueOnce(new Error('offline')); expect(await blobs.pushToServer(hash)).toBe(false); });
it('textOf decodes a stored blob and yields null when absent', async () => { const hash = await blobs.putLocal( new Blob([new TextEncoder().encode('bonjour')]), 'text/plain', ); expect(await blobs.textOf(hash)).toBe('bonjour'); hasServerBase.mockReturnValue(false); expect(await blobs.textOf('absent')).toBeNull(); });});