import assert from "node:assert/strict";
import {
InMemoryCallIntentStore, OutboundCallService, TelephonyError,
type OutboundCallProvider, type CallStatus,
} from "@agentium/core/telephony";
// A local provider fixture. No HTTP client, credentials, or phone network is used.
let creates = 0;
let status: CallStatus = "queued";
const provider: OutboundCallProvider = {
id: "fixture", routeId: "demo-v1",
capabilities: {
transport: "http", automaticCreateRetry: false,
callbackVerification: "host", hangup: "call",
},
async create() {
creates++;
return { ref: { providerId: "fixture", routeId: "demo-v1", callId: "call-1" }, status, providerStatus: status };
},
async get(ref) { return { ref, status, providerStatus: status }; },
async hangup(ref) { status = "cancelled"; return { ref, acknowledged: true }; },
normalizeVerifiedEvent() { throw new Error("This fixture does not accept webhooks"); },
};
const identity = { tenantId: "demo", userId: "alice" };
const calls = new OutboundCallService({
providers: [provider], store: new InMemoryCallIntentStore(),
authorize: ({ identity: caller, request }) => caller.tenantId === "demo" && caller.userId === "alice"
&& request.routeId === "demo-v1" && request.from === "+14155550102" && request.to === "+14155550103",
});
const request = {
identity, intentId: "appointment-42", routeId: "demo-v1",
from: "+14155550102", to: "+14155550103",
};
const first = await calls.create(request);
const duplicate = await calls.create(request);
assert.equal(creates, 1);
assert.deepEqual(first.snapshot?.ref, duplicate.snapshot?.ref);
console.log("duplicate intent: one provider create");
await assert.rejects(
calls.get({ tenantId: "demo", userId: "bob" }, request.intentId),
(error: unknown) => error instanceof TelephonyError && error.code === "unauthorized",
);
console.log("another user: denied");
await calls.hangup(identity, request.intentId);
const ended = await calls.get(identity, request.intentId);
assert.equal(ended.snapshot?.status, "cancelled");
console.log("confirmed status:", ended.snapshot?.status);