|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { buildFrame, buildHand } from '@/test/handFixtures'; |
| 3 | +import { SIGNATURE_LENGTH, similarity, windowSignature } from '../windowSignature'; |
| 4 | + |
| 5 | +type Curls = [number, number, number, number, number]; |
| 6 | + |
| 7 | +/** A held handshape, repeated for `frames` — a static sign. */ |
| 8 | +function hold(curls: Curls, frames: number, handedness: 'left' | 'right' = 'right') { |
| 9 | + return Array.from({ length: frames }, (_, i) => |
| 10 | + buildFrame(i * 33, buildHand({ curls, handedness })), |
| 11 | + ); |
| 12 | +} |
| 13 | + |
| 14 | +/** A handshape travelling across the frame — a dynamic sign. */ |
| 15 | +function travel(curls: Curls, frames: number, step = 0.04) { |
| 16 | + return Array.from({ length: frames }, (_, i) => |
| 17 | + buildFrame(i * 33, buildHand({ curls, offset: { x: i * step, y: 0 } })), |
| 18 | + ); |
| 19 | +} |
| 20 | + |
| 21 | +const FIST: Curls = [0.9, 0.9, 0.9, 0.9, 0.9]; |
| 22 | +const OPEN: Curls = [0, 0, 0, 0, 0]; |
| 23 | +const POINT: Curls = [0.9, 0, 0.9, 0.9, 0.9]; |
| 24 | + |
| 25 | +describe('windowSignature', () => { |
| 26 | + it('always produces the same length, whatever the sign lasted', () => { |
| 27 | + expect(windowSignature(hold(OPEN, 3))).toHaveLength(SIGNATURE_LENGTH); |
| 28 | + expect(windowSignature(hold(OPEN, 40))).toHaveLength(SIGNATURE_LENGTH); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns a zero signature for an empty window rather than throwing', () => { |
| 32 | + const signature = windowSignature([]); |
| 33 | + expect(signature).toHaveLength(SIGNATURE_LENGTH); |
| 34 | + expect(signature.every((value) => value === 0)).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('matches the same sign performed at different speeds', () => { |
| 38 | + // The whole point of resampling: duration must not be what distinguishes two signs. |
| 39 | + const fast = windowSignature(travel(POINT, 6)); |
| 40 | + const slow = windowSignature(travel(POINT, 30)); |
| 41 | + expect(similarity(fast, slow)).toBeGreaterThan(0.95); |
| 42 | + }); |
| 43 | + |
| 44 | + it('separates two genuinely different handshapes by a wide margin', () => { |
| 45 | + // Asserting a real number, not just an ordering: an earlier cosine metric ranked these |
| 46 | + // "correctly" while scoring them 0.965, which no threshold could have used. |
| 47 | + const fist = windowSignature(hold(FIST, 12)); |
| 48 | + const open = windowSignature(hold(OPEN, 12)); |
| 49 | + expect(similarity(fist, open)).toBeLessThan(0.3); |
| 50 | + }); |
| 51 | + |
| 52 | + it('keeps the closest distinct pair below the recognition threshold', () => { |
| 53 | + // A fist and an index point share four curled fingers, so they are the hardest pair on |
| 54 | + // this fixture set. If even they scored above 0.86 the classifier would be guessing. |
| 55 | + const fist = windowSignature(hold(FIST, 12)); |
| 56 | + const point = windowSignature(hold(POINT, 12)); |
| 57 | + expect(similarity(fist, point)).toBeLessThan(0.86); |
| 58 | + }); |
| 59 | + |
| 60 | + it('still matches a re-recording of the same sign with natural variation', () => { |
| 61 | + const first = windowSignature(hold(POINT, 12)); |
| 62 | + const wobbly = Array.from({ length: 14 }, (_, i) => |
| 63 | + buildFrame( |
| 64 | + i * 33, |
| 65 | + buildHand({ |
| 66 | + curls: [0.85, 0.06, 0.96, 0.84, 0.95], |
| 67 | + offset: { x: 0.03, y: 0.02 }, |
| 68 | + }), |
| 69 | + ), |
| 70 | + ); |
| 71 | + expect(similarity(first, windowSignature(wobbly))).toBeGreaterThan(0.86); |
| 72 | + }); |
| 73 | + |
| 74 | + it('is unaffected by where in frame the sign happens', () => { |
| 75 | + const here = windowSignature(hold(POINT, 12)); |
| 76 | + const shifted = hold(POINT, 12).map((frame) => |
| 77 | + buildFrame(frame.timestampMs, buildHand({ curls: POINT, offset: { x: 0.25, y: -0.2 } })), |
| 78 | + ); |
| 79 | + expect(similarity(here, windowSignature(shifted))).toBeGreaterThan(0.99); |
| 80 | + }); |
| 81 | + |
| 82 | + it('keeps the two hands in separate slots', () => { |
| 83 | + // A right-handed sign and its left-handed twin must not collide, or a two-handed sign |
| 84 | + // could never be told apart from a one-handed one. |
| 85 | + const right = windowSignature(hold(POINT, 10, 'right')); |
| 86 | + const left = windowSignature(hold(POINT, 10, 'left')); |
| 87 | + expect(similarity(right, left)).toBeLessThan(0.99); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe('similarity', () => { |
| 92 | + it('scores a vector against itself at the top of the range', () => { |
| 93 | + const signature = windowSignature(hold(POINT, 10)); |
| 94 | + expect(similarity(signature, signature)).toBeCloseTo(1, 5); |
| 95 | + }); |
| 96 | + |
| 97 | + it('scores zero against a signature where nothing was tracked', () => { |
| 98 | + expect(similarity(new Float32Array(SIGNATURE_LENGTH), windowSignature(hold(OPEN, 5)))).toBe(0); |
| 99 | + }); |
| 100 | + |
| 101 | + it('does not call two empty signatures a perfect match', () => { |
| 102 | + // They sit at distance zero from each other, so the raw metric would say 1.0 — "I saw |
| 103 | + // no hand" agreeing with "I saw no hand" is not a recognised sign. |
| 104 | + const empty = new Float32Array(SIGNATURE_LENGTH); |
| 105 | + expect(similarity(empty, new Float32Array(SIGNATURE_LENGTH))).toBe(0); |
| 106 | + }); |
| 107 | + |
| 108 | + it('refuses to compare signatures of different lengths', () => { |
| 109 | + expect(similarity(new Float32Array(4), new Float32Array(8))).toBe(0); |
| 110 | + }); |
| 111 | +}); |
0 commit comments