/* * The approval screen's AI advisor — it is asked with the tenant, and every way of not answering is named. * * 2026-09-17: the twin app's advisor called its tools with no domain, because `advise` had no place for one. * Every tool answered "no domain context" and the opinion was written from the prompt alone. The first test * holds the domain on the way through; the resolver test holds that the resolver hands it over. */ import { test } from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { askAdvisor, setActuationAdvisor, type ActuationAdvisor } from '../server/service/actuation/actuation-advisor.ts' const command = { id: 'cmd-1', instanceId: 'wms-1', type: 'order.hold', args: {} } as any const twinNow = { attentions: [] } as any function install(advise: ActuationAdvisor['advise']) { setActuationAdvisor({ label: 'AI', advise }) } test('the advisor is asked with the domain the command belongs to', async () => { let seen: any install(async input => { seen = input return { stance: 'unsure', text: 'not enough to go on' } }) const r = await askAdvisor({ domainId: 'tenant-a', command, twinNow, locale: 'ko' }) assert.equal(seen.domainId, 'tenant-a') assert.equal(seen.command, command) assert.equal(seen.locale, 'ko') assert.equal(r.opinion?.stance, 'unsure') }) test('an advisor that throws is an absent opinion with its reason, not silence', async () => { install(async () => { throw new Error('model unavailable') }) const r = await askAdvisor({ domainId: 'tenant-a', command, twinNow }) assert.equal(r.opinion, undefined) assert.match(String(r.absent), /model unavailable/) }) test('an advisor that does not answer in time is absent, and says it was time', async () => { install(() => new Promise(() => {})) const r = await askAdvisor({ domainId: 'tenant-a', command, twinNow, budgetMs: 10 }) assert.equal(r.opinion, undefined) assert.ok(r.absent) }) test('the resolver hands the tenant to the advisor', () => { const src = readFileSync(new URL('../server/service/actuation/twin-command-resolver.ts', import.meta.url), 'utf-8') const call = src.match(/askAdvisor\(\{([^}]*)\}/) assert.ok(call, 'askAdvisor call not found in the resolver') assert.match(call[1], /\bdomainId\b/) })