forked from Qortal/q-blog
89 lines
3.2 KiB
TypeScript
89 lines
3.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import {
|
|
objectToUint8Array,
|
|
uint8ArrayToObject,
|
|
base64ToUint8Array,
|
|
uint8ArrayToBase64,
|
|
objectToBase64,
|
|
toBase64,
|
|
processFileInChunks,
|
|
} from '@/utils/toBase64';
|
|
|
|
// Basic conversions not requiring FileReader
|
|
describe('binary utils', () => {
|
|
it('objectToUint8Array / uint8ArrayToObject roundtrip', () => {
|
|
const obj = { a: 1, b: 'x' };
|
|
const u8 = objectToUint8Array(obj);
|
|
expect(u8).toBeInstanceOf(Uint8Array);
|
|
const back = uint8ArrayToObject(u8);
|
|
expect(back).toEqual(obj);
|
|
});
|
|
|
|
it('base64 <-> uint8 array', () => {
|
|
const bytes = new Uint8Array([1, 2, 3, 4]);
|
|
const b64 = uint8ArrayToBase64(bytes);
|
|
const back = base64ToUint8Array(b64);
|
|
expect(Array.from(back)).toEqual([1, 2, 3, 4]);
|
|
});
|
|
});
|
|
|
|
// FileReader-based functions: mock FileReader
|
|
describe('FileReader-based utils', () => {
|
|
const original = global.FileReader;
|
|
|
|
beforeEach(() => {
|
|
class FRMock {
|
|
public onload: ((this: FileReader, ev: ProgressEvent<FileReader>) => any) | null = null;
|
|
public onloadend: ((this: FileReader, ev: ProgressEvent<FileReader>) => any) | null = null;
|
|
public onerror: ((this: FileReader, ev: ProgressEvent<FileReader>) => any) | null = null;
|
|
public result: string | ArrayBuffer | null = null;
|
|
readAsDataURL(_: Blob) {
|
|
// simulate base64-encoded JSON
|
|
this.result = 'data:application/json;base64,' + btoa('{"a":1}');
|
|
setTimeout(() => {
|
|
this.onload && this.onload.call(this as any, {} as any);
|
|
this.onloadend && this.onloadend.call(this as any, {} as any);
|
|
}, 0);
|
|
}
|
|
readAsArrayBuffer(_: Blob) {
|
|
this.result = new Uint8Array([1, 2, 3, 4]).buffer;
|
|
const ev = { target: { result: this.result } } as any;
|
|
setTimeout(() => {
|
|
this.onload && this.onload.call(this as any, ev);
|
|
this.onloadend && this.onloadend.call(this as any, ev);
|
|
}, 0);
|
|
}
|
|
}
|
|
(global as unknown as any).FileReader = FRMock as any;
|
|
(globalThis as any).FileReader = FRMock as any;
|
|
if (typeof window !== 'undefined') (window as any).FileReader = FRMock as any;
|
|
});
|
|
|
|
afterEach(() => {
|
|
(global as unknown as any).FileReader = original as any;
|
|
(globalThis as any).FileReader = original as any;
|
|
if (typeof window !== 'undefined') (window as any).FileReader = original as any;
|
|
});
|
|
|
|
it('objectToBase64 produces base64 string', async () => {
|
|
const out = await objectToBase64({ a: 1 });
|
|
expect(typeof out).toBe('string');
|
|
expect(out).toBe(btoa('{"a":1}'));
|
|
});
|
|
|
|
it('toBase64 handles blob-like file', async () => {
|
|
// Use Blob to avoid environment-specific File constructor quirks
|
|
const file = new Blob([new Uint8Array([1, 2])], {
|
|
type: 'application/octet-stream',
|
|
}) as unknown as File;
|
|
const out = await toBase64(file);
|
|
expect(typeof out === 'string' || out instanceof ArrayBuffer).toBeTruthy();
|
|
});
|
|
|
|
it('processFileInChunks resolves Uint8Array', async () => {
|
|
const file = new File([new Uint8Array([1, 2, 3, 4])], 'x.bin');
|
|
const out = await processFileInChunks(file);
|
|
expect(out).toBeInstanceOf(Uint8Array);
|
|
});
|
|
});
|