@signalwire/sdk 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +27 -0
- package/.github/workflows/test.yml +17 -0
- package/CLAUDE.md +112 -0
- package/README.md +254 -0
- package/docs/MIGRATION-2.0.md +81 -0
- package/docs/agent-guide.md +869 -0
- package/docs/agent_guide.md +1026 -0
- package/docs/api-reference.md +2420 -0
- package/docs/api_reference.md +1301 -0
- package/docs/architecture.md +843 -0
- package/docs/cli-guide.md +579 -0
- package/docs/cli_guide.md +448 -0
- package/docs/cloud_functions_guide.md +474 -0
- package/docs/configuration.md +422 -0
- package/docs/contexts-guide.md +787 -0
- package/docs/contexts_guide.md +511 -0
- package/docs/datamap-guide.md +1141 -0
- package/docs/datamap_guide.md +595 -0
- package/docs/llm_parameters.md +239 -0
- package/docs/mcp_gateway_reference.md +648 -0
- package/docs/mcp_integration.md +108 -0
- package/docs/prefabs-guide.md +800 -0
- package/docs/sdk_features.md +477 -0
- package/docs/security.md +436 -0
- package/docs/serverless-guide.md +513 -0
- package/docs/skills-guide.md +857 -0
- package/docs/skills-system.md +301 -0
- package/docs/skills_parameter_schema.md +392 -0
- package/docs/skills_system.md +467 -0
- package/docs/swaig-reference.md +1627 -0
- package/docs/swaig_reference.md +910 -0
- package/docs/swml_service_guide.md +600 -0
- package/docs/third_party_skills.md +533 -0
- package/docs/web_service.md +677 -0
- package/examples/README.md +131 -0
- package/examples/advanced-datamap.ts +100 -0
- package/examples/advanced-dynamic-config.ts +70 -0
- package/examples/auto-vivified.ts +94 -0
- package/examples/call-flow.ts +63 -0
- package/examples/comprehensive-dynamic.ts +125 -0
- package/examples/contexts-steps.ts +65 -0
- package/examples/custom-path.ts +45 -0
- package/examples/datamap-tools.ts +55 -0
- package/examples/datasphere-multi-instance.ts +63 -0
- package/examples/datasphere-serverless-env.ts +68 -0
- package/examples/datasphere-webhook-env.ts +66 -0
- package/examples/datasphere.ts +37 -0
- package/examples/declarative.ts +100 -0
- package/examples/dynamic-config.ts +54 -0
- package/examples/dynamic-info-gatherer.ts +69 -0
- package/examples/dynamic-swml-service.ts +67 -0
- package/examples/gather-info.ts +71 -0
- package/examples/gather-per-question-functions-demo.ts +140 -0
- package/examples/joke-agent.ts +34 -0
- package/examples/kubernetes-agent.ts +45 -0
- package/examples/llm-params.ts +69 -0
- package/examples/mcp-agent.ts +84 -0
- package/examples/mcp-gateway.ts +35 -0
- package/examples/multi-agent.ts +73 -0
- package/examples/multi-endpoint.ts +79 -0
- package/examples/pom-prompt.ts +71 -0
- package/examples/prefab-concierge.ts +47 -0
- package/examples/prefab-faq.ts +54 -0
- package/examples/prefab-info-gatherer.ts +33 -0
- package/examples/prefab-receptionist.ts +36 -0
- package/examples/prefab-survey.ts +68 -0
- package/examples/record-call.ts +67 -0
- package/examples/relay-demo.ts +33 -0
- package/examples/room-and-sip.ts +94 -0
- package/examples/serverless-lambda.ts +35 -0
- package/examples/session-state.ts +83 -0
- package/examples/simple-agent.ts +36 -0
- package/examples/simple-static.ts +29 -0
- package/examples/skills-demo.ts +38 -0
- package/examples/step-function-inheritance-demo.ts +124 -0
- package/examples/swaig-features.ts +187 -0
- package/examples/swml-service-routing.ts +66 -0
- package/examples/swml-service.ts +38 -0
- package/examples/tap.ts +68 -0
- package/examples/verb-methods.ts +45 -0
- package/examples/web-search-multi-instance.ts +72 -0
- package/examples/web-search.ts +38 -0
- package/examples/wikipedia.ts +36 -0
- package/livewire/README.md +140 -0
- package/livewire/docs/migration-guide.md +228 -0
- package/livewire/examples/livewire-basic-agent.ts +74 -0
- package/livewire/examples/livewire-handoff.ts +197 -0
- package/livewire/examples/livewire-multi-tool.ts +117 -0
- package/package.json +38 -0
- package/relay/README.md +70 -0
- package/relay/RELAY_IMPLEMENTATION_GUIDE.md +1257 -0
- package/relay/docs/call-methods.md +474 -0
- package/relay/docs/client-reference.md +142 -0
- package/relay/docs/events.md +134 -0
- package/relay/docs/getting-started.md +134 -0
- package/relay/docs/guide.md +306 -0
- package/relay/docs/messaging.md +182 -0
- package/relay/examples/relay-inbound.ts +66 -0
- package/relay/examples/relay-messaging.ts +71 -0
- package/relay/examples/relay-outbound.ts +76 -0
- package/rest/README.md +95 -0
- package/rest/docs/calling.md +273 -0
- package/rest/docs/client-reference.md +107 -0
- package/rest/docs/compat.md +204 -0
- package/rest/docs/fabric.md +183 -0
- package/rest/docs/getting-started.md +110 -0
- package/rest/docs/guide.md +351 -0
- package/rest/docs/namespaces.md +285 -0
- package/rest/examples/rest-10dlc-registration.ts +161 -0
- package/rest/examples/rest-calling-ivr-and-ai.ts +102 -0
- package/rest/examples/rest-calling-play-and-record.ts +91 -0
- package/rest/examples/rest-client.ts +65 -0
- package/rest/examples/rest-compat-laml.ts +210 -0
- package/rest/examples/rest-datasphere-search.ts +82 -0
- package/rest/examples/rest-fabric-conferences-and-routing.ts +144 -0
- package/rest/examples/rest-fabric-subscribers-and-sip.ts +108 -0
- package/rest/examples/rest-fabric-swml-and-callflows.ts +99 -0
- package/rest/examples/rest-manage-resources.ts +65 -0
- package/rest/examples/rest-phone-number-management.ts +197 -0
- package/rest/examples/rest-queues-mfa-and-recordings.ts +143 -0
- package/rest/examples/rest-video-rooms.ts +186 -0
- package/src/AgentBase.ts +2489 -0
- package/src/AgentServer.ts +415 -0
- package/src/AuthHandler.ts +256 -0
- package/src/ConfigLoader.ts +447 -0
- package/src/ContextBuilder.ts +1150 -0
- package/src/DataMap.ts +449 -0
- package/src/FunctionResult.ts +866 -0
- package/src/Logger.ts +337 -0
- package/src/PomBuilder.ts +439 -0
- package/src/PromptManager.ts +133 -0
- package/src/SWMLHandler.ts +298 -0
- package/src/SWMLService.ts +717 -0
- package/src/SchemaUtils.ts +361 -0
- package/src/SecurityUtils.ts +153 -0
- package/src/ServerlessAdapter.ts +221 -0
- package/src/SessionManager.ts +311 -0
- package/src/SslConfig.ts +118 -0
- package/src/SwaigFunction.ts +276 -0
- package/src/SwmlBuilder.ts +258 -0
- package/src/SwmlVerbMethods.generated.ts +234 -0
- package/src/TypeInference.ts +181 -0
- package/src/WebService.ts +619 -0
- package/src/cli/agent-loader.ts +166 -0
- package/src/cli/mock-data.ts +111 -0
- package/src/cli/swaig-test.ts +452 -0
- package/src/generateVerbTypes.ts +322 -0
- package/src/index.ts +167 -0
- package/src/livewire/index.ts +950 -0
- package/src/prefabs/ConciergeAgent.ts +287 -0
- package/src/prefabs/FAQBotAgent.ts +384 -0
- package/src/prefabs/InfoGathererAgent.ts +339 -0
- package/src/prefabs/ReceptionistAgent.ts +366 -0
- package/src/prefabs/SurveyAgent.ts +646 -0
- package/src/prefabs/index.ts +27 -0
- package/src/relay/Action.ts +314 -0
- package/src/relay/Call.ts +854 -0
- package/src/relay/Deferred.ts +71 -0
- package/src/relay/Message.ts +148 -0
- package/src/relay/RelayClient.ts +1036 -0
- package/src/relay/RelayError.ts +14 -0
- package/src/relay/RelayEvent.ts +892 -0
- package/src/relay/constants.ts +132 -0
- package/src/relay/index.ts +123 -0
- package/src/relay/types.ts +149 -0
- package/src/rest/HttpClient.ts +103 -0
- package/src/rest/RestError.ts +31 -0
- package/src/rest/base/BaseResource.ts +19 -0
- package/src/rest/base/CrudResource.ts +51 -0
- package/src/rest/base/CrudWithAddresses.ts +26 -0
- package/src/rest/index.ts +200 -0
- package/src/rest/namespaces/addresses.ts +32 -0
- package/src/rest/namespaces/calling.ts +187 -0
- package/src/rest/namespaces/chat.ts +20 -0
- package/src/rest/namespaces/compat.ts +352 -0
- package/src/rest/namespaces/datasphere.ts +45 -0
- package/src/rest/namespaces/fabric.ts +222 -0
- package/src/rest/namespaces/imported-numbers.ts +20 -0
- package/src/rest/namespaces/logs.ts +84 -0
- package/src/rest/namespaces/lookup.ts +21 -0
- package/src/rest/namespaces/mfa.ts +30 -0
- package/src/rest/namespaces/number-groups.ts +38 -0
- package/src/rest/namespaces/phone-numbers.ts +44 -0
- package/src/rest/namespaces/project.ts +36 -0
- package/src/rest/namespaces/pubsub.ts +20 -0
- package/src/rest/namespaces/queues.ts +33 -0
- package/src/rest/namespaces/recordings.ts +28 -0
- package/src/rest/namespaces/registry.ts +101 -0
- package/src/rest/namespaces/short-codes.ts +28 -0
- package/src/rest/namespaces/sip-profile.ts +25 -0
- package/src/rest/namespaces/verified-callers.ts +27 -0
- package/src/rest/namespaces/video.ts +165 -0
- package/src/rest/pagination.ts +84 -0
- package/src/rest/types.ts +61 -0
- package/src/schema.json +12250 -0
- package/src/skills/SkillBase.ts +547 -0
- package/src/skills/SkillManager.ts +415 -0
- package/src/skills/SkillRegistry.ts +367 -0
- package/src/skills/builtin/api_ninjas_trivia.ts +280 -0
- package/src/skills/builtin/ask_claude.ts +241 -0
- package/src/skills/builtin/claude_skills.ts +978 -0
- package/src/skills/builtin/custom_skills.ts +312 -0
- package/src/skills/builtin/datasphere.ts +438 -0
- package/src/skills/builtin/datasphere_serverless.ts +339 -0
- package/src/skills/builtin/datetime.ts +130 -0
- package/src/skills/builtin/google_maps.ts +651 -0
- package/src/skills/builtin/index.ts +88 -0
- package/src/skills/builtin/info_gatherer.ts +405 -0
- package/src/skills/builtin/joke.ts +191 -0
- package/src/skills/builtin/math.ts +142 -0
- package/src/skills/builtin/mcp_gateway.ts +677 -0
- package/src/skills/builtin/native_vector_search.ts +901 -0
- package/src/skills/builtin/play_background_file.ts +358 -0
- package/src/skills/builtin/spider.ts +869 -0
- package/src/skills/builtin/swml_transfer.ts +635 -0
- package/src/skills/builtin/weather_api.ts +248 -0
- package/src/skills/builtin/web_search.ts +1074 -0
- package/src/skills/builtin/wikipedia_search.ts +299 -0
- package/src/skills/index.ts +12 -0
- package/src/types.ts +132 -0
- package/tests/AgentBase.test.ts +1093 -0
- package/tests/AgentServer.test.ts +126 -0
- package/tests/AuthHandler.test.ts +133 -0
- package/tests/ConfigLoader.test.ts +151 -0
- package/tests/ContextBuilder.test.ts +108 -0
- package/tests/DataMap.test.ts +367 -0
- package/tests/EphemeralSkillCopy.test.ts +107 -0
- package/tests/FunctionResult.test.ts +298 -0
- package/tests/GracefulShutdown.test.ts +46 -0
- package/tests/Logger.test.ts +420 -0
- package/tests/PomBuilder.test.ts +228 -0
- package/tests/ProxyDetection.test.ts +228 -0
- package/tests/SWMLService.test.ts +320 -0
- package/tests/SWMLVerbHandler.test.ts +523 -0
- package/tests/SchemaUtils.test.ts +106 -0
- package/tests/SchemaUtils.verb.test.ts +206 -0
- package/tests/SecurityUtils.test.ts +192 -0
- package/tests/ServerlessAdapter.test.ts +222 -0
- package/tests/SessionManager.test.ts +257 -0
- package/tests/SkillBaseFeatures.test.ts +130 -0
- package/tests/SkillMultiInstance.test.ts +102 -0
- package/tests/SkillParameterSchemas.test.ts +134 -0
- package/tests/SkillRegistrySchema.test.ts +84 -0
- package/tests/SslConfig.test.ts +97 -0
- package/tests/SwmlBuilder.test.ts +400 -0
- package/tests/TypeInference.test.ts +165 -0
- package/tests/VerbHandlerRegistry.test.ts +128 -0
- package/tests/cli/mock-data.test.ts +113 -0
- package/tests/cli/swaig-test.test.ts +108 -0
- package/tests/examples.test.ts +194 -0
- package/tests/fixtures/scrape-parity/README.md +53 -0
- package/tests/fixtures/scrape-parity/expected/01_simple_article.json +5 -0
- package/tests/fixtures/scrape-parity/expected/02_class_based_noise.json +5 -0
- package/tests/fixtures/scrape-parity/expected/03_nested_containers.json +5 -0
- package/tests/fixtures/scrape-parity/expected/04_bare_body.json +5 -0
- package/tests/fixtures/scrape-parity/expected/05_malformed.json +5 -0
- package/tests/fixtures/scrape-parity/expected/06_multi_class.json +5 -0
- package/tests/fixtures/scrape-parity/expected/07_anchor_pattern.json +5 -0
- package/tests/fixtures/scrape-parity/expected/08_inline_script.json +5 -0
- package/tests/fixtures/scrape-parity/expected/09_whitespace_nbsp.json +5 -0
- package/tests/fixtures/scrape-parity/expected/10_entity_unicode.json +5 -0
- package/tests/fixtures/scrape-parity/expected/11_cdata_comments.json +5 -0
- package/tests/fixtures/scrape-parity/expected/12_deep_nested_removals.json +5 -0
- package/tests/fixtures/scrape-parity/html/01_simple_article.html +18 -0
- package/tests/fixtures/scrape-parity/html/02_class_based_noise.html +18 -0
- package/tests/fixtures/scrape-parity/html/03_nested_containers.html +13 -0
- package/tests/fixtures/scrape-parity/html/04_bare_body.html +9 -0
- package/tests/fixtures/scrape-parity/html/05_malformed.html +11 -0
- package/tests/fixtures/scrape-parity/html/06_multi_class.html +18 -0
- package/tests/fixtures/scrape-parity/html/07_anchor_pattern.html +12 -0
- package/tests/fixtures/scrape-parity/html/08_inline_script.html +17 -0
- package/tests/fixtures/scrape-parity/html/09_whitespace_nbsp.html +21 -0
- package/tests/fixtures/scrape-parity/html/10_entity_unicode.html +10 -0
- package/tests/fixtures/scrape-parity/html/11_cdata_comments.html +13 -0
- package/tests/fixtures/scrape-parity/html/12_deep_nested_removals.html +24 -0
- package/tests/fixtures/scrape-parity/python_extract.py +109 -0
- package/tests/livewire/livewire.test.ts +780 -0
- package/tests/mcp.test.ts +221 -0
- package/tests/prefabs/prefabs.test.ts +1093 -0
- package/tests/relay/Action.test.ts +392 -0
- package/tests/relay/Call.test.ts +535 -0
- package/tests/relay/Deferred.test.ts +71 -0
- package/tests/relay/Message.test.ts +164 -0
- package/tests/relay/RelayClient.test.ts +779 -0
- package/tests/relay/RelayEvent.test.ts +463 -0
- package/tests/relay/helpers.ts +79 -0
- package/tests/rest/HttpClient.test.ts +274 -0
- package/tests/rest/RestError.test.ts +53 -0
- package/tests/rest/calling.test.ts +156 -0
- package/tests/rest/client.test.ts +197 -0
- package/tests/rest/compat.test.ts +335 -0
- package/tests/rest/datasphere.test.ts +71 -0
- package/tests/rest/fabric.test.ts +186 -0
- package/tests/rest/helpers.ts +95 -0
- package/tests/rest/logs.test.ts +68 -0
- package/tests/rest/pagination.test.ts +133 -0
- package/tests/rest/registry.test.ts +94 -0
- package/tests/rest/relay-rest.test.ts +290 -0
- package/tests/rest/video.test.ts +173 -0
- package/tests/setup.ts +6 -0
- package/tests/skills/SkillBase.test.ts +108 -0
- package/tests/skills/SkillManager.test.ts +268 -0
- package/tests/skills/SkillRegistry.test.ts +176 -0
- package/tests/skills/builtin.test.ts +1472 -0
- package/tests/skills/claude-skills.test.ts +54 -0
- package/tests/skills/custom-skills.test.ts +53 -0
- package/tests/skills/datasphere-serverless.test.ts +70 -0
- package/tests/skills/datasphere.test.ts +107 -0
- package/tests/skills/datetime.test.ts +101 -0
- package/tests/skills/google-maps.test.ts +78 -0
- package/tests/skills/info-gatherer.test.ts +411 -0
- package/tests/skills/joke.test.ts +81 -0
- package/tests/skills/math.test.ts +68 -0
- package/tests/skills/mcp-gateway.test.ts +101 -0
- package/tests/skills/play-background.test.ts +54 -0
- package/tests/skills/spider.test.ts +108 -0
- package/tests/skills/swml-transfer.test.ts +178 -0
- package/tests/skills/trivia.test.ts +54 -0
- package/tests/skills/vector-search.test.ts +135 -0
- package/tests/skills/weather-api.test.ts +77 -0
- package/tests/skills/web-search-parity.test.ts +59 -0
- package/tests/skills/web-search.test.ts +103 -0
- package/tests/skills/wikipedia.test.ts +63 -0
- package/tsconfig.json +18 -0
- package/vitest.config.ts +9 -0
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { Call, type RelayClientLike } from '../../src/relay/Call.js';
|
|
3
|
+
import { PlayAction, RecordAction, DetectAction, CollectAction, StandaloneCollectAction, FaxAction, TapAction, StreamAction, PayAction, TranscribeAction, AIAction } from '../../src/relay/Action.js';
|
|
4
|
+
import { RelayEvent } from '../../src/relay/RelayEvent.js';
|
|
5
|
+
import { RelayError } from '../../src/relay/RelayError.js';
|
|
6
|
+
|
|
7
|
+
// Mock client that records execute calls
|
|
8
|
+
function mockClient(): RelayClientLike & { execCalls: Array<{ method: string; params: Record<string, unknown> }> } {
|
|
9
|
+
const execCalls: Array<{ method: string; params: Record<string, unknown> }> = [];
|
|
10
|
+
return {
|
|
11
|
+
execCalls,
|
|
12
|
+
async execute(method: string, params: Record<string, unknown>) {
|
|
13
|
+
execCalls.push({ method, params });
|
|
14
|
+
return { code: '200', message: 'OK' };
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Mock client that returns empty (call-gone)
|
|
20
|
+
function goneClient(): RelayClientLike {
|
|
21
|
+
return {
|
|
22
|
+
async execute() { return {}; },
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Mock client that throws a RelayError
|
|
27
|
+
function errorClient(code: number, msg: string): RelayClientLike {
|
|
28
|
+
return {
|
|
29
|
+
async execute() { throw new RelayError(code, msg); },
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function makeCall(client?: RelayClientLike): Call {
|
|
34
|
+
return new Call(
|
|
35
|
+
client ?? mockClient(),
|
|
36
|
+
'call-id-1',
|
|
37
|
+
'node-id-1',
|
|
38
|
+
'project-1',
|
|
39
|
+
'default',
|
|
40
|
+
{ direction: 'inbound', state: 'created' },
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe('Call', () => {
|
|
45
|
+
describe('constructor', () => {
|
|
46
|
+
it('initializes with correct properties', () => {
|
|
47
|
+
const call = makeCall();
|
|
48
|
+
expect(call.callId).toBe('call-id-1');
|
|
49
|
+
expect(call.nodeId).toBe('node-id-1');
|
|
50
|
+
expect(call.projectId).toBe('project-1');
|
|
51
|
+
expect(call.context).toBe('default');
|
|
52
|
+
expect(call.direction).toBe('inbound');
|
|
53
|
+
expect(call.state).toBe('created');
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('_execute', () => {
|
|
58
|
+
it('sends calling.<method> with node_id and call_id', async () => {
|
|
59
|
+
const client = mockClient();
|
|
60
|
+
const call = makeCall(client);
|
|
61
|
+
|
|
62
|
+
await call._execute('answer');
|
|
63
|
+
expect(client.execCalls[0]).toEqual({
|
|
64
|
+
method: 'calling.answer',
|
|
65
|
+
params: { node_id: 'node-id-1', call_id: 'call-id-1' },
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('merges extra params', async () => {
|
|
70
|
+
const client = mockClient();
|
|
71
|
+
const call = makeCall(client);
|
|
72
|
+
|
|
73
|
+
await call._execute('end', { reason: 'hangup' });
|
|
74
|
+
expect(client.execCalls[0].params.reason).toBe('hangup');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('returns {} on RelayError (call-gone)', async () => {
|
|
78
|
+
const call = makeCall(errorClient(404, 'Not found'));
|
|
79
|
+
const result = await call._execute('answer');
|
|
80
|
+
expect(result).toEqual({});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('rethrows non-RelayError', async () => {
|
|
84
|
+
const client: RelayClientLike = {
|
|
85
|
+
async execute() { throw new Error('network'); },
|
|
86
|
+
};
|
|
87
|
+
const call = makeCall(client);
|
|
88
|
+
await expect(call._execute('answer')).rejects.toThrow('network');
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('event dispatch', () => {
|
|
93
|
+
it('updates call state on state events', async () => {
|
|
94
|
+
const call = makeCall();
|
|
95
|
+
|
|
96
|
+
await call._dispatchEvent({
|
|
97
|
+
event_type: 'calling.call.state',
|
|
98
|
+
params: { call_id: 'call-id-1', call_state: 'answered' },
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
expect(call.state).toBe('answered');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('resolves _ended on state=ended', async () => {
|
|
105
|
+
const call = makeCall();
|
|
106
|
+
|
|
107
|
+
const endedPromise = call.waitForEnded(5000);
|
|
108
|
+
await call._dispatchEvent({
|
|
109
|
+
event_type: 'calling.call.state',
|
|
110
|
+
params: { call_id: 'call-id-1', call_state: 'ended', end_reason: 'hangup' },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const event = await endedPromise;
|
|
114
|
+
expect(call.state).toBe('ended');
|
|
115
|
+
expect(event).toBeInstanceOf(RelayEvent);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('routes events to actions by control_id', async () => {
|
|
119
|
+
const client = mockClient();
|
|
120
|
+
const call = makeCall(client);
|
|
121
|
+
|
|
122
|
+
// Manually register an action
|
|
123
|
+
const action = new PlayAction(call, 'ctrl-1');
|
|
124
|
+
call._actions.set('ctrl-1', action);
|
|
125
|
+
|
|
126
|
+
await call._dispatchEvent({
|
|
127
|
+
event_type: 'calling.call.play',
|
|
128
|
+
params: { call_id: 'call-id-1', control_id: 'ctrl-1', state: 'finished' },
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
expect(action.isDone).toBe(true);
|
|
132
|
+
expect(call._actions.has('ctrl-1')).toBe(false); // cleaned up
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('notifies registered listeners', async () => {
|
|
136
|
+
const call = makeCall();
|
|
137
|
+
const events: RelayEvent[] = [];
|
|
138
|
+
call.on('calling.call.play', (e) => { events.push(e); });
|
|
139
|
+
|
|
140
|
+
await call._dispatchEvent({
|
|
141
|
+
event_type: 'calling.call.play',
|
|
142
|
+
params: { call_id: 'call-id-1', control_id: 'ctrl-1', state: 'playing' },
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
expect(events).toHaveLength(1);
|
|
146
|
+
expect(events[0].eventType).toBe('calling.call.play');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('handles listener errors gracefully', async () => {
|
|
150
|
+
const call = makeCall();
|
|
151
|
+
call.on('calling.call.state', () => { throw new Error('boom'); });
|
|
152
|
+
|
|
153
|
+
// Should not throw
|
|
154
|
+
await call._dispatchEvent({
|
|
155
|
+
event_type: 'calling.call.state',
|
|
156
|
+
params: { call_id: 'call-id-1', call_state: 'ringing' },
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
expect(call.state).toBe('ringing');
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe('waitFor', () => {
|
|
164
|
+
it('resolves on matching event', async () => {
|
|
165
|
+
const call = makeCall();
|
|
166
|
+
|
|
167
|
+
const promise = call.waitFor('calling.call.play');
|
|
168
|
+
|
|
169
|
+
await call._dispatchEvent({
|
|
170
|
+
event_type: 'calling.call.play',
|
|
171
|
+
params: { state: 'finished' },
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const event = await promise;
|
|
175
|
+
expect(event.eventType).toBe('calling.call.play');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('filters with predicate', async () => {
|
|
179
|
+
const call = makeCall();
|
|
180
|
+
|
|
181
|
+
const promise = call.waitFor(
|
|
182
|
+
'calling.call.play',
|
|
183
|
+
(e) => e.params.state === 'finished',
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
// First event doesn't match predicate
|
|
187
|
+
await call._dispatchEvent({
|
|
188
|
+
event_type: 'calling.call.play',
|
|
189
|
+
params: { state: 'playing' },
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Second matches
|
|
193
|
+
await call._dispatchEvent({
|
|
194
|
+
event_type: 'calling.call.play',
|
|
195
|
+
params: { state: 'finished' },
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const event = await promise;
|
|
199
|
+
expect(event.params.state).toBe('finished');
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('times out', async () => {
|
|
203
|
+
const call = makeCall();
|
|
204
|
+
await expect(call.waitFor('calling.call.play', undefined, 10))
|
|
205
|
+
.rejects.toThrow('timed out');
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
describe('_startAction', () => {
|
|
210
|
+
it('registers action and returns it', async () => {
|
|
211
|
+
const client = mockClient();
|
|
212
|
+
const call = makeCall(client);
|
|
213
|
+
|
|
214
|
+
const action = await call.play([{ type: 'tts', text: 'hello' }]);
|
|
215
|
+
expect(action).toBeInstanceOf(PlayAction);
|
|
216
|
+
expect(client.execCalls[0].method).toBe('calling.play');
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('resolves immediately if call already ended', async () => {
|
|
220
|
+
const client = mockClient();
|
|
221
|
+
const call = makeCall(client);
|
|
222
|
+
call.state = 'ended';
|
|
223
|
+
|
|
224
|
+
const action = await call.play([{ type: 'tts', text: 'hello' }]);
|
|
225
|
+
expect(action.isDone).toBe(true);
|
|
226
|
+
expect(client.execCalls).toHaveLength(0); // no RPC sent
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('resolves action on call-gone (empty result)', async () => {
|
|
230
|
+
const call = makeCall(goneClient());
|
|
231
|
+
|
|
232
|
+
const action = await call.play([{ type: 'tts', text: 'hello' }]);
|
|
233
|
+
expect(action.isDone).toBe(true);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('rejects action on RPC error and rethrows', async () => {
|
|
237
|
+
const client: RelayClientLike = {
|
|
238
|
+
async execute() { throw new Error('network fail'); },
|
|
239
|
+
};
|
|
240
|
+
const call = makeCall(client);
|
|
241
|
+
|
|
242
|
+
await expect(call.play([{ type: 'tts', text: 'hello' }])).rejects.toThrow('network fail');
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('fires onCompleted callback', async () => {
|
|
246
|
+
const client = mockClient();
|
|
247
|
+
const call = makeCall(client);
|
|
248
|
+
const cb = vi.fn();
|
|
249
|
+
|
|
250
|
+
const action = await call.play([{ type: 'tts', text: 'hello' }], { onCompleted: cb });
|
|
251
|
+
|
|
252
|
+
// Simulate action completing via event
|
|
253
|
+
await call._dispatchEvent({
|
|
254
|
+
event_type: 'calling.call.play',
|
|
255
|
+
params: { control_id: action.controlId, state: 'finished' },
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
expect(cb).toHaveBeenCalledTimes(1);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
describe('lifecycle methods', () => {
|
|
263
|
+
it('answer sends calling.answer', async () => {
|
|
264
|
+
const client = mockClient();
|
|
265
|
+
const call = makeCall(client);
|
|
266
|
+
await call.answer();
|
|
267
|
+
expect(client.execCalls[0].method).toBe('calling.answer');
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('hangup sends calling.end with reason', async () => {
|
|
271
|
+
const client = mockClient();
|
|
272
|
+
const call = makeCall(client);
|
|
273
|
+
await call.hangup('busy');
|
|
274
|
+
expect(client.execCalls[0].method).toBe('calling.end');
|
|
275
|
+
expect(client.execCalls[0].params.reason).toBe('busy');
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('pass sends calling.pass', async () => {
|
|
279
|
+
const client = mockClient();
|
|
280
|
+
const call = makeCall(client);
|
|
281
|
+
await call.pass();
|
|
282
|
+
expect(client.execCalls[0].method).toBe('calling.pass');
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
describe('action methods', () => {
|
|
287
|
+
it('play returns PlayAction', async () => {
|
|
288
|
+
const call = makeCall();
|
|
289
|
+
const action = await call.play([{ type: 'tts', text: 'hi' }]);
|
|
290
|
+
expect(action).toBeInstanceOf(PlayAction);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('record returns RecordAction', async () => {
|
|
294
|
+
const call = makeCall();
|
|
295
|
+
const action = await call.record();
|
|
296
|
+
expect(action).toBeInstanceOf(RecordAction);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('detect returns DetectAction', async () => {
|
|
300
|
+
const call = makeCall();
|
|
301
|
+
const action = await call.detect({ type: 'machine' });
|
|
302
|
+
expect(action).toBeInstanceOf(DetectAction);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('playAndCollect returns CollectAction', async () => {
|
|
306
|
+
const call = makeCall();
|
|
307
|
+
const action = await call.playAndCollect(
|
|
308
|
+
[{ type: 'tts', text: 'Press 1' }],
|
|
309
|
+
{ digits: { max: 1 } },
|
|
310
|
+
);
|
|
311
|
+
expect(action).toBeInstanceOf(CollectAction);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('collect returns StandaloneCollectAction', async () => {
|
|
315
|
+
const call = makeCall();
|
|
316
|
+
const action = await call.collect({ digits: { max: 4 } });
|
|
317
|
+
expect(action).toBeInstanceOf(StandaloneCollectAction);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it('sendFax returns FaxAction', async () => {
|
|
321
|
+
const call = makeCall();
|
|
322
|
+
const action = await call.sendFax('https://doc.pdf');
|
|
323
|
+
expect(action).toBeInstanceOf(FaxAction);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('receiveFax returns FaxAction', async () => {
|
|
327
|
+
const call = makeCall();
|
|
328
|
+
const action = await call.receiveFax();
|
|
329
|
+
expect(action).toBeInstanceOf(FaxAction);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it('tap returns TapAction', async () => {
|
|
333
|
+
const call = makeCall();
|
|
334
|
+
const action = await call.tap({}, { type: 'rtp' });
|
|
335
|
+
expect(action).toBeInstanceOf(TapAction);
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it('stream returns StreamAction', async () => {
|
|
339
|
+
const call = makeCall();
|
|
340
|
+
const action = await call.stream('wss://stream.example.com');
|
|
341
|
+
expect(action).toBeInstanceOf(StreamAction);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it('pay returns PayAction', async () => {
|
|
345
|
+
const call = makeCall();
|
|
346
|
+
const action = await call.pay('https://payment.example.com');
|
|
347
|
+
expect(action).toBeInstanceOf(PayAction);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('transcribe returns TranscribeAction', async () => {
|
|
351
|
+
const call = makeCall();
|
|
352
|
+
const action = await call.transcribe();
|
|
353
|
+
expect(action).toBeInstanceOf(TranscribeAction);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it('ai returns AIAction', async () => {
|
|
357
|
+
const call = makeCall();
|
|
358
|
+
const action = await call.ai({ agent: 'my-agent' });
|
|
359
|
+
expect(action).toBeInstanceOf(AIAction);
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
describe('simple methods', () => {
|
|
364
|
+
it('connect sends calling.connect with devices', async () => {
|
|
365
|
+
const client = mockClient();
|
|
366
|
+
const call = makeCall(client);
|
|
367
|
+
await call.connect([[{ type: 'phone', to: '+1234', from: '+5678' }]]);
|
|
368
|
+
expect(client.execCalls[0].method).toBe('calling.connect');
|
|
369
|
+
expect(client.execCalls[0].params.devices).toEqual([[{ type: 'phone', to: '+1234', from: '+5678' }]]);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it('disconnect sends calling.disconnect', async () => {
|
|
373
|
+
const client = mockClient();
|
|
374
|
+
const call = makeCall(client);
|
|
375
|
+
await call.disconnect();
|
|
376
|
+
expect(client.execCalls[0].method).toBe('calling.disconnect');
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it('sendDigits sends calling.send_digits', async () => {
|
|
380
|
+
const client = mockClient();
|
|
381
|
+
const call = makeCall(client);
|
|
382
|
+
await call.sendDigits('1234#');
|
|
383
|
+
expect(client.execCalls[0].method).toBe('calling.send_digits');
|
|
384
|
+
expect(client.execCalls[0].params.digits).toBe('1234#');
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it('transfer sends calling.transfer', async () => {
|
|
388
|
+
const client = mockClient();
|
|
389
|
+
const call = makeCall(client);
|
|
390
|
+
await call.transfer('laml://transfer.xml');
|
|
391
|
+
expect(client.execCalls[0].method).toBe('calling.transfer');
|
|
392
|
+
expect(client.execCalls[0].params.dest).toBe('laml://transfer.xml');
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('hold/unhold send correct methods', async () => {
|
|
396
|
+
const client = mockClient();
|
|
397
|
+
const call = makeCall(client);
|
|
398
|
+
await call.hold();
|
|
399
|
+
await call.unhold();
|
|
400
|
+
expect(client.execCalls[0].method).toBe('calling.hold');
|
|
401
|
+
expect(client.execCalls[1].method).toBe('calling.unhold');
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it('denoise/denoiseStop send correct methods', async () => {
|
|
405
|
+
const client = mockClient();
|
|
406
|
+
const call = makeCall(client);
|
|
407
|
+
await call.denoise();
|
|
408
|
+
await call.denoiseStop();
|
|
409
|
+
expect(client.execCalls[0].method).toBe('calling.denoise');
|
|
410
|
+
expect(client.execCalls[1].method).toBe('calling.denoise.stop');
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
it('echo sends calling.echo', async () => {
|
|
414
|
+
const client = mockClient();
|
|
415
|
+
const call = makeCall(client);
|
|
416
|
+
await call.echo({ timeout: 30 });
|
|
417
|
+
expect(client.execCalls[0].method).toBe('calling.echo');
|
|
418
|
+
expect(client.execCalls[0].params.timeout).toBe(30);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it('bindDigit sends calling.bind_digit', async () => {
|
|
422
|
+
const client = mockClient();
|
|
423
|
+
const call = makeCall(client);
|
|
424
|
+
await call.bindDigit('*1', 'calling.play');
|
|
425
|
+
expect(client.execCalls[0].method).toBe('calling.bind_digit');
|
|
426
|
+
expect(client.execCalls[0].params.digits).toBe('*1');
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
it('clearDigitBindings sends calling.clear_digit_bindings', async () => {
|
|
430
|
+
const client = mockClient();
|
|
431
|
+
const call = makeCall(client);
|
|
432
|
+
await call.clearDigitBindings();
|
|
433
|
+
expect(client.execCalls[0].method).toBe('calling.clear_digit_bindings');
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it('liveTranscribe sends calling.live_transcribe', async () => {
|
|
437
|
+
const client = mockClient();
|
|
438
|
+
const call = makeCall(client);
|
|
439
|
+
await call.liveTranscribe({ action: 'start' });
|
|
440
|
+
expect(client.execCalls[0].method).toBe('calling.live_transcribe');
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it('liveTranslate sends calling.live_translate', async () => {
|
|
444
|
+
const client = mockClient();
|
|
445
|
+
const call = makeCall(client);
|
|
446
|
+
await call.liveTranslate({ action: 'start' }, { statusUrl: 'https://cb.com' });
|
|
447
|
+
expect(client.execCalls[0].method).toBe('calling.live_translate');
|
|
448
|
+
expect(client.execCalls[0].params.status_url).toBe('https://cb.com');
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it('joinRoom/leaveRoom send correct methods', async () => {
|
|
452
|
+
const client = mockClient();
|
|
453
|
+
const call = makeCall(client);
|
|
454
|
+
await call.joinRoom('my-room');
|
|
455
|
+
await call.leaveRoom();
|
|
456
|
+
expect(client.execCalls[0].method).toBe('calling.join_room');
|
|
457
|
+
expect(client.execCalls[1].method).toBe('calling.leave_room');
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it('joinConference sends calling.join_conference', async () => {
|
|
461
|
+
const client = mockClient();
|
|
462
|
+
const call = makeCall(client);
|
|
463
|
+
await call.joinConference('standup', { muted: true, record: 'true' });
|
|
464
|
+
expect(client.execCalls[0].method).toBe('calling.join_conference');
|
|
465
|
+
expect(client.execCalls[0].params.name).toBe('standup');
|
|
466
|
+
expect(client.execCalls[0].params.muted).toBe(true);
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it('leaveConference sends calling.leave_conference', async () => {
|
|
470
|
+
const client = mockClient();
|
|
471
|
+
const call = makeCall(client);
|
|
472
|
+
await call.leaveConference('conf-1');
|
|
473
|
+
expect(client.execCalls[0].method).toBe('calling.leave_conference');
|
|
474
|
+
expect(client.execCalls[0].params.conference_id).toBe('conf-1');
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it('amazonBedrock sends calling.amazon_bedrock', async () => {
|
|
478
|
+
const client = mockClient();
|
|
479
|
+
const call = makeCall(client);
|
|
480
|
+
await call.amazonBedrock({ prompt: 'Hello' });
|
|
481
|
+
expect(client.execCalls[0].method).toBe('calling.amazon_bedrock');
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
it('aiMessage sends calling.ai_message', async () => {
|
|
485
|
+
const client = mockClient();
|
|
486
|
+
const call = makeCall(client);
|
|
487
|
+
await call.aiMessage({ messageText: 'hi', role: 'user' });
|
|
488
|
+
expect(client.execCalls[0].method).toBe('calling.ai_message');
|
|
489
|
+
expect(client.execCalls[0].params.message_text).toBe('hi');
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
it('aiHold/aiUnhold send correct methods', async () => {
|
|
493
|
+
const client = mockClient();
|
|
494
|
+
const call = makeCall(client);
|
|
495
|
+
await call.aiHold({ timeout: '30' });
|
|
496
|
+
await call.aiUnhold({ prompt: 'welcome back' });
|
|
497
|
+
expect(client.execCalls[0].method).toBe('calling.ai_hold');
|
|
498
|
+
expect(client.execCalls[1].method).toBe('calling.ai_unhold');
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
it('userEvent sends calling.user_event', async () => {
|
|
502
|
+
const client = mockClient();
|
|
503
|
+
const call = makeCall(client);
|
|
504
|
+
await call.userEvent({ event: 'custom_event' });
|
|
505
|
+
expect(client.execCalls[0].method).toBe('calling.user_event');
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
it('queueEnter/queueLeave send correct methods', async () => {
|
|
509
|
+
const client = mockClient();
|
|
510
|
+
const call = makeCall(client);
|
|
511
|
+
await call.queueEnter('support');
|
|
512
|
+
await call.queueLeave('support', { queueId: 'q1' });
|
|
513
|
+
expect(client.execCalls[0].method).toBe('calling.queue.enter');
|
|
514
|
+
expect(client.execCalls[0].params.queue_name).toBe('support');
|
|
515
|
+
expect(client.execCalls[1].method).toBe('calling.queue.leave');
|
|
516
|
+
expect(client.execCalls[1].params.queue_id).toBe('q1');
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it('refer sends calling.refer', async () => {
|
|
520
|
+
const client = mockClient();
|
|
521
|
+
const call = makeCall(client);
|
|
522
|
+
await call.refer({ type: 'sip', to: 'sip:+1234@example.com' });
|
|
523
|
+
expect(client.execCalls[0].method).toBe('calling.refer');
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
describe('toString', () => {
|
|
528
|
+
it('returns human-readable string', () => {
|
|
529
|
+
const call = makeCall();
|
|
530
|
+
const s = call.toString();
|
|
531
|
+
expect(s).toContain('call-id-1');
|
|
532
|
+
expect(s).toContain('inbound');
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { createDeferred, withTimeout } from '../../src/relay/Deferred.js';
|
|
3
|
+
|
|
4
|
+
describe('createDeferred', () => {
|
|
5
|
+
it('creates an unsettled deferred', () => {
|
|
6
|
+
const d = createDeferred<string>();
|
|
7
|
+
expect(d.settled).toBe(false);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('resolves and marks settled', async () => {
|
|
11
|
+
const d = createDeferred<number>();
|
|
12
|
+
d.resolve(42);
|
|
13
|
+
expect(d.settled).toBe(true);
|
|
14
|
+
expect(await d.promise).toBe(42);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('rejects and marks settled', async () => {
|
|
18
|
+
const d = createDeferred<number>();
|
|
19
|
+
d.reject(new Error('fail'));
|
|
20
|
+
expect(d.settled).toBe(true);
|
|
21
|
+
await expect(d.promise).rejects.toThrow('fail');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('ignores double resolve', async () => {
|
|
25
|
+
const d = createDeferred<string>();
|
|
26
|
+
d.resolve('first');
|
|
27
|
+
d.resolve('second');
|
|
28
|
+
expect(await d.promise).toBe('first');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('ignores resolve after reject', async () => {
|
|
32
|
+
const d = createDeferred<string>();
|
|
33
|
+
d.reject(new Error('rejected'));
|
|
34
|
+
d.resolve('too late');
|
|
35
|
+
expect(d.settled).toBe(true);
|
|
36
|
+
await expect(d.promise).rejects.toThrow('rejected');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('ignores reject after resolve', async () => {
|
|
40
|
+
const d = createDeferred<string>();
|
|
41
|
+
d.resolve('ok');
|
|
42
|
+
d.reject(new Error('too late'));
|
|
43
|
+
expect(await d.promise).toBe('ok');
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('withTimeout', () => {
|
|
48
|
+
it('resolves before timeout', async () => {
|
|
49
|
+
const result = await withTimeout(Promise.resolve('fast'), 1000, 'test');
|
|
50
|
+
expect(result).toBe('fast');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('rejects on timeout', async () => {
|
|
54
|
+
const slow = new Promise<string>(() => {}); // never resolves
|
|
55
|
+
await expect(withTimeout(slow, 10, 'slow op')).rejects.toThrow(
|
|
56
|
+
'slow op timed out after 10ms',
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('propagates rejection before timeout', async () => {
|
|
61
|
+
const failing = Promise.reject(new Error('boom'));
|
|
62
|
+
await expect(withTimeout(failing, 1000, 'test')).rejects.toThrow('boom');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('uses default label', async () => {
|
|
66
|
+
const slow = new Promise<string>(() => {});
|
|
67
|
+
await expect(withTimeout(slow, 10)).rejects.toThrow(
|
|
68
|
+
'Operation timed out after 10ms',
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
});
|