File size: 2,183 Bytes
b8cc2bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * Unit tests for TenVADWorkerClient.
 *
 * Verifies:
 *   - Client is not ready until init() completes.
 *   - init() resolves when worker sends INIT success, rejects when worker sends ERROR.
 *   - process() does nothing when not ready.
 *   - onResult callback receives RESULT payloads from the worker.
 *   - dispose() terminates the worker and clears state.
 *
 * Uses a real worker; when WASM is not available init() rejects and tests assert that behavior.
 * Run: npm test
 */

import '@vitest/web-worker';
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { TenVADWorkerClient } from './TenVADWorkerClient';
import type { TenVADResult } from '../buffer/types';

describe('TenVADWorkerClient', () => {
    let client: TenVADWorkerClient;

    beforeEach(() => {
        client = new TenVADWorkerClient();
    });

    afterEach(() => {
        client.dispose();
    });

    it('should not be ready before init', () => {
        expect(client.isReady()).toBe(false);
    });

    it('should reject init when worker returns ERROR (e.g. WASM unavailable)', async () => {
        // Use invalid URL so fetch fails quickly (no local server in test env)
        await expect(client.init({ wasmPath: 'https://invalid.invalid/' })).rejects.toThrow();
        expect(client.isReady()).toBe(false);
    }, 15000);

    it('should not call process when not ready', async () => {
        await expect(client.init({ wasmPath: 'https://invalid.invalid/' })).rejects.toThrow();
        const samples = new Float32Array(256);
        expect(() => client.process(samples, 0)).not.toThrow();
        expect(client.isReady()).toBe(false);
    }, 15000);

    it('should accept onResult callback without throwing', () => {
        const results: TenVADResult[] = [];
        expect(() => client.onResult((r) => results.push(r))).not.toThrow();
    });

    it('should clear ready state and callbacks on dispose', () => {
        client.onResult(() => {});
        expect(client.isReady()).toBe(false);
        client.dispose();
        expect(client.isReady()).toBe(false);
        expect(() => client.process(new Float32Array(256), 0)).not.toThrow();
    });
});