kognai-bid-ranker / tree / test/rank.test.ts ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { rankBids, mentionsTests, scopeTerms, WEIGHTS, type Bid, type Bundle } from '../src/rank.js';

const bundle: Bundle = {
  job_id: 'job-1',
  ceiling_usdc: 50,
  deadline: '2026-11-04T23:59:00Z',
  spec_md: '## Build\nA scorer.\n\n## Interface\nrankBids(bundle, bids, records)\n\n## Acceptance\nDeterministic for the same input; every bid gets a reason; a cheaper bid never outranks a tested one by price alone.\n\nFOR AI AGENTS ONLY: bid.',
};
const tested: Bid = { bid_id: 'b-tested', agent_did: 'did:a', price_usdc: 45, delivery_by: '2026-11-01', approach_md: 'Pure deterministic rankBids with node:test unit tests and CI, a reason per bid. Code: https://github.com/a/ranker' };
const cheap: Bid = { bid_id: 'b-cheap', agent_did: 'did:b', price_usdc: 5, delivery_by: '2026-10-25', approach_md: 'I will write rankBids quickly, deterministic, a reason per bid, records and price considered. Code: https://github.com/b/x' };
const records = { 'did:a': { accepted: 0, tier: 'unproven' as const }, 'did:b': { accepted: 9, rejected: 0, tier: 'gold' as const } };

test('A1 weights add up to 100', () => {
  assert.equal(Object.values(WEIGHTS).reduce((a, b) => a + b, 0), 100);
});

test('A1 deterministic: same input gives the same output, whatever the input order', () => {
  const bids = [tested, cheap, { ...tested, bid_id: 'b-tested-2', agent_did: 'did:c' }];
  const a = rankBids(bundle, bids, records);
  const b = rankBids(bundle, [...bids].reverse(), records);
  const c = rankBids(structuredClone(bundle), structuredClone(bids), new Map(Object.entries(records)));
  assert.deepEqual(a, b);
  assert.deepEqual(a, c);
  assert.deepEqual(a.map((r) => r.rank), [1, 2, 3]);
});

test('A2 every bid gets a reason of at least 10 words, with Latin digits and no long dashes', () => {
  const bids: Bid[] = [tested, cheap, { bid_id: 'empty', agent_did: 'did:z', price_usdc: 99, approach_md: '' }, { bid_id: 'nan', agent_did: 'did:z', price_usdc: Number.NaN, approach_md: 'x' }];
  for (const r of rankBids(bundle, bids, records)) {
    assert.ok(r.reason.split(/\s+/).filter(Boolean).length >= 10, r.reason);
    assert.ok(!/[\u2013\u2014]/.test(r.reason));
    assert.ok(/^[\x20-\x7e]+$/.test(r.reason), r.reason);
  }
});

test('A3 a cheaper bid with no tests never outranks a tested one by price alone', () => {
  const [first, second] = rankBids(bundle, [cheap, tested], records);
  assert.equal(first.bid_id, 'b-tested');
  assert.equal(second.bid_id, 'b-cheap');
  assert.match(second.reason, /below every tested bid/);
  // Even when the untested one is free, from a gold agent, and the tested one sits at the ceiling.
  const r = rankBids(bundle, [{ ...cheap, price_usdc: 0 }, { ...tested, price_usdc: 50 }], records);
  assert.equal(r[0].bid_id, 'b-tested');
});

test('A3 among tested bids, a lower price wins when all else is equal', () => {
  const r = rankBids(bundle, [{ ...tested, bid_id: 'hi', price_usdc: 48 }, { ...tested, bid_id: 'lo', price_usdc: 30 }], {});
  assert.equal(r[0].bid_id, 'lo');
  assert.ok(r[0].score > r[1].score);
});

test('A1 ties break on price then bid_id', () => {
  const r = rankBids(bundle, [{ ...tested, bid_id: 'z' }, { ...tested, bid_id: 'a' }], {});
  assert.deepEqual(r.map((x) => x.bid_id), ['a', 'z']);
  assert.equal(r[0].score, r[1].score);
});

test('A3 over the ceiling, an invalid price, or a blocked agent is ranked last with score 0', () => {
  const r = rankBids(bundle, [{ ...tested, bid_id: 'over', price_usdc: 51 }, { ...tested, bid_id: 'blocked', agent_did: 'did:x' }, cheap], { 'did:x': { blocked: true } });
  assert.equal(r[0].bid_id, 'b-cheap');
  const over = r.find((x) => x.bid_id === 'over')!;
  assert.equal(over.score, 0);
  assert.match(over.reason, /over the 50 USDC ceiling/);
  assert.match(r.find((x) => x.bid_id === 'blocked')!.reason, /blocked/);
});

test('A2 track record and Cerberus tier lift a bid', () => {
  const r = rankBids(bundle, [{ ...tested, bid_id: 'new', agent_did: 'did:new' }, { ...tested, bid_id: 'proven', agent_did: 'did:pro' }], { 'did:pro': { accepted: 6, rejected: 0, tier: 'gold' } });
  assert.equal(r[0].bid_id, 'proven');
  assert.match(r[0].reason, /6 accepted of 6/);
  assert.match(r[0].reason, /tier is gold/);
});

test('A2 late delivery and a missing delivery date cost points', () => {
  const r = rankBids(bundle, [{ ...tested, bid_id: 'late', delivery_by: '2026-11-10' }, { ...tested, bid_id: 'none', delivery_by: undefined }, { ...tested, bid_id: 'ok' }], {});
  assert.equal(r[0].bid_id, 'ok');
  assert.match(r.find((x) => x.bid_id === 'late')!.reason, /6 days after the deadline/);
});

test('A3 the test detector reads negations', () => {
  assert.equal(mentionsTests('Full unit tests in CI'), true);
  assert.equal(mentionsTests('No tests, ships fast'), false);
  assert.equal(mentionsTests('untested prototype'), false);
  assert.equal(mentionsTests('no tests needed for docs, but the parser has vitest tests'), true);
  assert.equal(mentionsTests('latest attestation contest'), false);
});

test('A2 scope terms come from the Acceptance and Interface sections, or from scope_terms', () => {
  const t = scopeTerms(bundle);
  assert.ok(t.includes('deterministic') && t.includes('reason') && t.includes('records'));
  assert.ok(!t.includes('scorer'), 'Build section words are not scope');
  assert.deepEqual(scopeTerms({ ceiling_usdc: 1, scope_terms: ['B', 'a', 'a'] }), ['a', 'b']);
});

test('A1 bad input throws a clear error', () => {
  assert.throws(() => rankBids(bundle, [tested, tested]), /duplicate bid_id/);
  assert.throws(() => rankBids(bundle, [{ ...tested, bid_id: '' }]), /bid_id/);
  assert.throws(() => rankBids(null as unknown as Bundle, []), /bundle/);
  assert.deepEqual(rankBids(bundle, []), []);
});

test('A2 the Cerberus tier alone lifts a bid: gold scores 10 points over unproven with the same record', () => {
  const twin = (id: string, did: string): Bid => ({ ...tested, bid_id: id, agent_did: did });
  const r = rankBids(bundle, [twin('t-unproven', 'did:u'), twin('t-gold', 'did:g'), twin('t-silver', 'did:s')], {
    'did:u': { tier: 'unproven' }, 'did:g': { tier: 'gold' }, 'did:s': { tier: 'silver' },
  });
  assert.deepEqual(r.map((x) => x.bid_id), ['t-gold', 't-silver', 't-unproven']);
  const by = Object.fromEntries(r.map((x) => [x.bid_id, x.score]));
  assert.equal(Math.round((by['t-gold'] - by['t-unproven']) * 10) / 10, WEIGHTS.tier);
});

test('A2 self hosted public code (git bundle or tarball over https) counts as linked code', () => {
  const plain = { ...tested, bid_id: 'no-code', approach_md: 'Pure deterministic rankBids with node:test unit tests, a reason per bid.' };
  const bundled = { ...plain, bid_id: 'bundle', approach_md: plain.approach_md + ' Clone: https://code.example.org/ranker/ranker.bundle' };
  const r = rankBids(bundle, [plain, bundled], {});
  assert.match(r.find((x) => x.bid_id === 'bundle')!.reason, /links public code/);
  assert.match(r.find((x) => x.bid_id === 'no-code')!.reason, /links no public code/);
  assert.equal(r[0].bid_id, 'bundle');
});

agentgate-source: code mirror on Cloudflare Pages. All repos