From 5daf06784f219ecf01ed517b01633410d72dd552 Mon Sep 17 00:00:00 2001 From: 16808507970f18ea89a07ad1d30b53e7e3a3891d <16808507970f18ea89a07ad1d30b53e7e3a3891d@users.noreply.invalid> Date: Fri, 2 Oct 2026 07:27:43 +0000 Subject: [PATCH] Solution 0x51341970906a1fd3227c4224d8c075bd722f9c06dcc276acceeb52c37efa2774 T2T-Item: 0x51341970906a1fd3227c4224d8c075bd722f9c06dcc276acceeb52c37efa2774 T2T-Issue: 0x98f19ce946d4ef692a1354cdda7c9c22c3deb8168f1eb224b4d56b790e2ca305 --- diarize.mjs | 13 ++++++++++++- tests/acceptance/1/CRITERIA.json | 30 +++++++++++++++++++++++++++++ tests/acceptance/1/criteria.test.js | 25 ++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/acceptance/1/CRITERIA.json create mode 100644 tests/acceptance/1/criteria.test.js diff --git a/diarize.mjs b/diarize.mjs index 622b526..b47059b 100644 --- a/diarize.mjs +++ b/diarize.mjs @@ -25,7 +25,18 @@ export async function loadDiarizer(TF, { device = 'wasm' } = {}) { return { seg, segP, emb, embP }; } -const cos = (a, b) => { let dt = 0, na = 0, nb = 0; for (let i = 0; i < a.length; i++) { dt += a[i]*b[i]; na += a[i]*a[i]; nb += b[i]*b[i]; } return dt / (Math.sqrt(na)*Math.sqrt(nb) || 1); }; +const cos = (a, b) => { + if (a.length !== b.length) { + throw new Error('cosine: vectors must have the same length'); + } + let dt = 0, na = 0, nb = 0; + for (let i = 0; i < a.length; i++) { + dt += a[i]*b[i]; + na += a[i]*a[i]; + nb += b[i]*b[i]; + } + return dt / (Math.sqrt(na)*Math.sqrt(nb) || 1); +}; const rms = (a) => { let s = 0; for (const v of a) s += v*v; return Math.sqrt(s/a.length); }; async function embed(D, slice) { diff --git a/tests/acceptance/1/CRITERIA.json b/tests/acceptance/1/CRITERIA.json new file mode 100644 index 0000000..b7be116 --- /dev/null +++ b/tests/acceptance/1/CRITERIA.json @@ -0,0 +1,30 @@ +{ + "schema": "elffuss-t2t/criteria@2", + "issue": 1, + "criteria": [ + { + "index": 1, + "tests": [ + "tests/acceptance/1/criteria.test.js::criterion 1" + ] + }, + { + "index": 2, + "tests": [ + "tests/acceptance/1/criteria.test.js::criterion 2" + ] + }, + { + "index": 3, + "tests": [ + "tests/acceptance/1/criteria.test.js::criterion 3" + ] + }, + { + "index": 4, + "tests": [ + "tests/acceptance/1/criteria.test.js::criterion 4" + ] + } + ] +} diff --git a/tests/acceptance/1/criteria.test.js b/tests/acceptance/1/criteria.test.js new file mode 100644 index 0000000..ac04ed4 --- /dev/null +++ b/tests/acceptance/1/criteria.test.js @@ -0,0 +1,25 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import * as mod from '../../../diarize.mjs' // the code under test: call it as mod.(...) +const source = readFileSync(new URL('../../../diarize.mjs', import.meta.url), 'utf8') // the text of diarize.mjs, for a criterion about how the file is written + +test('criterion 1', () => { + // `cosine([1, 2, 3], [1, 2])` throws an Error whose message starts with `cosine:`. + assert.throws(() => mod.cosine([1, 2, 3], [1, 2]), /cosine:/) +}) + +test('criterion 2', () => { + // `cosine([1, 0], [1, 0, 5])` throws an Error whose message starts with `cosine:`. + assert.throws(() => mod.cosine([1, 0], [1, 0, 5]), /cosine:/) +}) + +test('criterion 3', () => { + // `cosine([1, 0], [1, 0])` returns 1. + assert.deepEqual(mod.cosine([1, 0], [1, 0]), 1) +}) + +test('criterion 4', () => { + // `cosine([0, 0], [0, 0])` returns 0. + assert.deepEqual(mod.cosine([0, 0], [0, 0]), 0) +}) -- 2.49.1