import { __awaiter } from "tslib";
import { assert, createLogicSuite } from '@genesislcap/foundation-testing';
// Side-effect import — MUST precede `./orchestrating-driver` so the driver
// subclasses jsdom's EventTarget (CustomEvent dispatch then works in node).
import '../chat-driver/align-event-globals';
import { OrchestratingDriver } from './orchestrating-driver';
// GENC-1508 — `recordExternalCost` is OPTIONAL on `AiDriver`, so a missing delegation here
// is not a compile error: the host's `driver?.recordExternalCost?.(…)` simply evaluates to
// undefined and every post-resolve charge from every widget disappears — silently, and only
// in orchestrated setups. That is the same invisible-when-broken failure the method exists to
// fix, so the delegation gets a test rather than trust.
const makeRegistry = (provider) => ({
    get: () => provider,
    default: () => provider,
    defaultName: () => 'test',
    names: () => ['test'],
    getStatus: () => __awaiter(void 0, void 0, void 0, function* () { return null; }),
    listStatuses: () => __awaiter(void 0, void 0, void 0, function* () { return []; }),
});
const noToolProvider = () => ({
    chat: (_history, _userMessage, _options) => __awaiter(void 0, void 0, void 0, function* () { return ({ role: 'assistant', content: 'ok' }); }),
});
const agent = (overrides) => (Object.assign({ description: 'test agent' }, overrides));
const makeDriver = () => new OrchestratingDriver(makeRegistry(noToolProvider()), [agent({ name: 'Genesis Assistant' })], {
    sessionKey: 'k',
});
/** The orchestrator does not expose `requestInteraction`, so the interaction is ARRANGED on the
 *  wrapped driver. Every assertion below goes through the orchestrator's own public surface. */
const wrapped = (driver) => driver.chatDriver;
const Suite = createLogicSuite('orchestrating-driver external cost');
Suite('post-resolve spend delegates to the wrapped driver', () => __awaiter(void 0, void 0, void 0, function* () {
    const driver = makeDriver();
    const inner = wrapped(driver);
    const pending = inner.requestInteraction('planning-question', { question: 'Pick one' });
    const id = driver.getRawHistory().at(-1).interaction.interactionId;
    driver.resolveInteraction(id, { status: 'approved', costUsd: 0.5 });
    yield pending;
    assert.is(driver.recordExternalCost(id, 0.25), true);
    const msg = driver.getRawHistory().find((m) => { var _a; return ((_a = m.interaction) === null || _a === void 0 ? void 0 : _a.interactionId) === id; });
    assert.is(msg === null || msg === void 0 ? void 0 : msg.externalCostUsd, 0.75);
}));
Suite('an unknown interaction is reported as unrecorded, not thrown', () => __awaiter(void 0, void 0, void 0, function* () {
    const driver = makeDriver();
    assert.is(driver.recordExternalCost('never-existed', 0.25), false);
}));
Suite.run();
