import { expect, test } from 'bun:test' import { Tensor } from '@huggingface/transformers' import { Crivo } from '../src/decisions/adapters/crivo.js' test('mixed questions use one forward with independent row masks and option counts', async () => { let calls = 0 const tokenizer = { model_max_length: 7999, pad_token_id: 0, encode: (text: string) => (text.startsWith('[') ? [1] : Array(text.length).fill(2)) } const model = { forward: async (inputs: Record) => { calls++ expect(inputs.input_ids?.dims[0]).toBe(3) expect(inputs.qtype?.tolist()).toEqual([1n, 0n, 2n]) expect(inputs.opt_mask?.tolist()).toEqual([ [1, 1, 1], [1, 1, 0], [1, 1, 0] ]) const masks = inputs.attention_mask?.tolist() as bigint[][] expect(masks[0]?.filter(Boolean).length).toBeLessThan(masks[1]?.filter(Boolean).length ?? 0) return { logits: new Tensor( 'float32', new Float32Array([0, 1, 2, 0, 1, -Infinity, 2, 0, -Infinity]), [3, 3] ) } } } const crivo = Reflect.construct(Crivo, [tokenizer, model]) as Crivo const answers = await crivo.answerMany('s', [ { id: 'route', question: { type: 'choice', instructions: 'x', criteria: { a: 'a', b: 'b', c: 'c' } } }, { id: 'flag', question: { type: 'noul', instructions: 'a much longer question' } }, { id: 'level', question: { type: 'score', instructions: 'x', criteria: ['low', 'high'] } } ]) expect(calls).toBe(1) expect(answers[0]).toMatchObject({ type: 'choice', choice: 'c' }) expect(answers[1]).toMatchObject({ type: 'noul', noul: expect.any(Number) }) expect(answers[2]?.type === 'score' && Object.keys(answers[2].probabilities)).toEqual(['0', '1']) }) test('oversized later question reports its ID before any forward', async () => { let called = false const crivo = Reflect.construct(Crivo, [ { model_max_length: 30, pad_token_id: 0, encode: (text: string) => (text.startsWith('[') ? [1] : Array(text.length).fill(2)) }, { forward: () => { called = true } } ]) as Crivo await expect( crivo.answerMany('s', [ { id: 'short', question: { type: 'noul', instructions: 'ok' } }, { id: 'long', question: { type: 'noul', instructions: 'x'.repeat(40) } } ]) ).rejects.toMatchObject({ decisionId: 'long', limit: 30 }) expect(called).toBe(false) })