mirror of https://github.com/openclaw/openclaw.git
refactor: share voice restore test setup
This commit is contained in:
parent
eec1b3a512
commit
bf631b5872
|
|
@ -9,121 +9,87 @@ import {
|
||||||
} from "./manager.test-harness.js";
|
} from "./manager.test-harness.js";
|
||||||
|
|
||||||
describe("CallManager verification on restore", () => {
|
describe("CallManager verification on restore", () => {
|
||||||
it("skips stale calls reported terminal by provider", async () => {
|
async function initializeManager(params?: {
|
||||||
|
callOverrides?: Parameters<typeof makePersistedCall>[0];
|
||||||
|
providerResult?: FakeProvider["getCallStatusResult"];
|
||||||
|
configureProvider?: (provider: FakeProvider) => void;
|
||||||
|
configOverrides?: Partial<{ maxDurationSeconds: number }>;
|
||||||
|
}) {
|
||||||
const storePath = createTestStorePath();
|
const storePath = createTestStorePath();
|
||||||
const call = makePersistedCall();
|
const call = makePersistedCall(params?.callOverrides);
|
||||||
writeCallsToStore(storePath, [call]);
|
writeCallsToStore(storePath, [call]);
|
||||||
|
|
||||||
const provider = new FakeProvider();
|
const provider = new FakeProvider();
|
||||||
provider.getCallStatusResult = { status: "completed", isTerminal: true };
|
if (params?.providerResult) {
|
||||||
|
provider.getCallStatusResult = params.providerResult;
|
||||||
|
}
|
||||||
|
params?.configureProvider?.(provider);
|
||||||
|
|
||||||
const config = VoiceCallConfigSchema.parse({
|
const config = VoiceCallConfigSchema.parse({
|
||||||
enabled: true,
|
enabled: true,
|
||||||
provider: "plivo",
|
provider: "plivo",
|
||||||
fromNumber: "+15550000000",
|
fromNumber: "+15550000000",
|
||||||
|
...params?.configOverrides,
|
||||||
});
|
});
|
||||||
const manager = new CallManager(config, storePath);
|
const manager = new CallManager(config, storePath);
|
||||||
await manager.initialize(provider, "https://example.com/voice/webhook");
|
await manager.initialize(provider, "https://example.com/voice/webhook");
|
||||||
|
|
||||||
|
return { call, manager };
|
||||||
|
}
|
||||||
|
|
||||||
|
it("skips stale calls reported terminal by provider", async () => {
|
||||||
|
const { manager } = await initializeManager({
|
||||||
|
providerResult: { status: "completed", isTerminal: true },
|
||||||
|
});
|
||||||
|
|
||||||
expect(manager.getActiveCalls()).toHaveLength(0);
|
expect(manager.getActiveCalls()).toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps calls reported active by provider", async () => {
|
it("keeps calls reported active by provider", async () => {
|
||||||
const storePath = createTestStorePath();
|
const { call, manager } = await initializeManager({
|
||||||
const call = makePersistedCall();
|
providerResult: { status: "in-progress", isTerminal: false },
|
||||||
writeCallsToStore(storePath, [call]);
|
|
||||||
|
|
||||||
const provider = new FakeProvider();
|
|
||||||
provider.getCallStatusResult = { status: "in-progress", isTerminal: false };
|
|
||||||
|
|
||||||
const config = VoiceCallConfigSchema.parse({
|
|
||||||
enabled: true,
|
|
||||||
provider: "plivo",
|
|
||||||
fromNumber: "+15550000000",
|
|
||||||
});
|
});
|
||||||
const manager = new CallManager(config, storePath);
|
|
||||||
await manager.initialize(provider, "https://example.com/voice/webhook");
|
|
||||||
|
|
||||||
expect(manager.getActiveCalls()).toHaveLength(1);
|
expect(manager.getActiveCalls()).toHaveLength(1);
|
||||||
expect(manager.getActiveCalls()[0]?.callId).toBe(call.callId);
|
expect(manager.getActiveCalls()[0]?.callId).toBe(call.callId);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps calls when provider returns unknown (transient error)", async () => {
|
it("keeps calls when provider returns unknown (transient error)", async () => {
|
||||||
const storePath = createTestStorePath();
|
const { manager } = await initializeManager({
|
||||||
const call = makePersistedCall();
|
providerResult: { status: "error", isTerminal: false, isUnknown: true },
|
||||||
writeCallsToStore(storePath, [call]);
|
|
||||||
|
|
||||||
const provider = new FakeProvider();
|
|
||||||
provider.getCallStatusResult = { status: "error", isTerminal: false, isUnknown: true };
|
|
||||||
|
|
||||||
const config = VoiceCallConfigSchema.parse({
|
|
||||||
enabled: true,
|
|
||||||
provider: "plivo",
|
|
||||||
fromNumber: "+15550000000",
|
|
||||||
});
|
});
|
||||||
const manager = new CallManager(config, storePath);
|
|
||||||
await manager.initialize(provider, "https://example.com/voice/webhook");
|
|
||||||
|
|
||||||
expect(manager.getActiveCalls()).toHaveLength(1);
|
expect(manager.getActiveCalls()).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips calls older than maxDurationSeconds", async () => {
|
it("skips calls older than maxDurationSeconds", async () => {
|
||||||
const storePath = createTestStorePath();
|
const { manager } = await initializeManager({
|
||||||
const call = makePersistedCall({
|
callOverrides: {
|
||||||
startedAt: Date.now() - 600_000,
|
startedAt: Date.now() - 600_000,
|
||||||
answeredAt: Date.now() - 590_000,
|
answeredAt: Date.now() - 590_000,
|
||||||
|
},
|
||||||
|
configOverrides: { maxDurationSeconds: 300 },
|
||||||
});
|
});
|
||||||
writeCallsToStore(storePath, [call]);
|
|
||||||
|
|
||||||
const provider = new FakeProvider();
|
|
||||||
|
|
||||||
const config = VoiceCallConfigSchema.parse({
|
|
||||||
enabled: true,
|
|
||||||
provider: "plivo",
|
|
||||||
fromNumber: "+15550000000",
|
|
||||||
maxDurationSeconds: 300,
|
|
||||||
});
|
|
||||||
const manager = new CallManager(config, storePath);
|
|
||||||
await manager.initialize(provider, "https://example.com/voice/webhook");
|
|
||||||
|
|
||||||
expect(manager.getActiveCalls()).toHaveLength(0);
|
expect(manager.getActiveCalls()).toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips calls without providerCallId", async () => {
|
it("skips calls without providerCallId", async () => {
|
||||||
const storePath = createTestStorePath();
|
const { manager } = await initializeManager({
|
||||||
const call = makePersistedCall({ providerCallId: undefined, state: "initiated" });
|
callOverrides: { providerCallId: undefined, state: "initiated" },
|
||||||
writeCallsToStore(storePath, [call]);
|
|
||||||
|
|
||||||
const provider = new FakeProvider();
|
|
||||||
|
|
||||||
const config = VoiceCallConfigSchema.parse({
|
|
||||||
enabled: true,
|
|
||||||
provider: "plivo",
|
|
||||||
fromNumber: "+15550000000",
|
|
||||||
});
|
});
|
||||||
const manager = new CallManager(config, storePath);
|
|
||||||
await manager.initialize(provider, "https://example.com/voice/webhook");
|
|
||||||
|
|
||||||
expect(manager.getActiveCalls()).toHaveLength(0);
|
expect(manager.getActiveCalls()).toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps call when getCallStatus throws (verification failure)", async () => {
|
it("keeps call when getCallStatus throws (verification failure)", async () => {
|
||||||
const storePath = createTestStorePath();
|
const { manager } = await initializeManager({
|
||||||
const call = makePersistedCall();
|
configureProvider: (provider) => {
|
||||||
writeCallsToStore(storePath, [call]);
|
provider.getCallStatus = async () => {
|
||||||
|
throw new Error("network failure");
|
||||||
const provider = new FakeProvider();
|
};
|
||||||
provider.getCallStatus = async () => {
|
},
|
||||||
throw new Error("network failure");
|
|
||||||
};
|
|
||||||
|
|
||||||
const config = VoiceCallConfigSchema.parse({
|
|
||||||
enabled: true,
|
|
||||||
provider: "plivo",
|
|
||||||
fromNumber: "+15550000000",
|
|
||||||
});
|
});
|
||||||
const manager = new CallManager(config, storePath);
|
|
||||||
await manager.initialize(provider, "https://example.com/voice/webhook");
|
|
||||||
|
|
||||||
expect(manager.getActiveCalls()).toHaveLength(1);
|
expect(manager.getActiveCalls()).toHaveLength(1);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue