@escapeaihq/client-core 0.8.0 → 0.42.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/README.md +48 -4
- package/dist/adapters/capabilities.d.ts +10 -0
- package/dist/adapters/capabilities.d.ts.map +1 -0
- package/dist/adapters/capabilities.js +2 -0
- package/dist/adapters/capabilities.js.map +1 -0
- package/dist/adapters/chatAdapter.d.ts +38 -0
- package/dist/adapters/chatAdapter.d.ts.map +1 -0
- package/dist/adapters/chatAdapter.js +88 -0
- package/dist/adapters/chatAdapter.js.map +1 -0
- package/dist/adapters/directorAdapter.d.ts +62 -0
- package/dist/adapters/directorAdapter.d.ts.map +1 -0
- package/dist/adapters/directorAdapter.js +71 -0
- package/dist/adapters/directorAdapter.js.map +1 -0
- package/dist/adapters/imageAdapter.d.ts +44 -0
- package/dist/adapters/imageAdapter.d.ts.map +1 -0
- package/dist/adapters/imageAdapter.js +73 -0
- package/dist/adapters/imageAdapter.js.map +1 -0
- package/dist/adapters/index.d.ts +47 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +41 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/registry.d.ts +98 -0
- package/dist/adapters/registry.d.ts.map +1 -0
- package/dist/adapters/registry.js +106 -0
- package/dist/adapters/registry.js.map +1 -0
- package/dist/adapters/worldAdapter.d.ts +142 -0
- package/dist/adapters/worldAdapter.d.ts.map +1 -0
- package/dist/adapters/worldAdapter.js +164 -0
- package/dist/adapters/worldAdapter.js.map +1 -0
- package/dist/admin.d.ts +200 -0
- package/dist/admin.d.ts.map +1 -0
- package/dist/admin.js +124 -0
- package/dist/admin.js.map +1 -0
- package/dist/api.d.ts +1299 -3
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +683 -95
- package/dist/api.js.map +1 -1
- package/dist/audio.d.ts +33 -1
- package/dist/audio.d.ts.map +1 -1
- package/dist/audio.js +4 -1
- package/dist/audio.js.map +1 -1
- package/dist/castProjection.d.ts +35 -0
- package/dist/castProjection.d.ts.map +1 -0
- package/dist/castProjection.js +80 -0
- package/dist/castProjection.js.map +1 -0
- package/dist/conversation/binding.d.ts +62 -0
- package/dist/conversation/binding.d.ts.map +1 -0
- package/dist/conversation/binding.js +51 -0
- package/dist/conversation/binding.js.map +1 -0
- package/dist/conversation/controller.d.ts +209 -0
- package/dist/conversation/controller.d.ts.map +1 -0
- package/dist/conversation/controller.js +342 -0
- package/dist/conversation/controller.js.map +1 -0
- package/dist/conversation/effectiveModel.d.ts +62 -0
- package/dist/conversation/effectiveModel.d.ts.map +1 -0
- package/dist/conversation/effectiveModel.js +73 -0
- package/dist/conversation/effectiveModel.js.map +1 -0
- package/dist/conversation/index.d.ts +27 -0
- package/dist/conversation/index.d.ts.map +1 -0
- package/dist/conversation/index.js +27 -0
- package/dist/conversation/index.js.map +1 -0
- package/dist/conversation/participant.d.ts +66 -0
- package/dist/conversation/participant.d.ts.map +1 -0
- package/dist/conversation/participant.js +36 -0
- package/dist/conversation/participant.js.map +1 -0
- package/dist/conversation/presence.d.ts +56 -0
- package/dist/conversation/presence.d.ts.map +1 -0
- package/dist/conversation/presence.js +34 -0
- package/dist/conversation/presence.js.map +1 -0
- package/dist/conversation/presenceBus.d.ts +42 -0
- package/dist/conversation/presenceBus.d.ts.map +1 -0
- package/dist/conversation/presenceBus.js +81 -0
- package/dist/conversation/presenceBus.js.map +1 -0
- package/dist/conversation/presenceRegistry.d.ts +49 -0
- package/dist/conversation/presenceRegistry.d.ts.map +1 -0
- package/dist/conversation/presenceRegistry.js +59 -0
- package/dist/conversation/presenceRegistry.js.map +1 -0
- package/dist/conversation/roster.d.ts +72 -0
- package/dist/conversation/roster.d.ts.map +1 -0
- package/dist/conversation/roster.js +69 -0
- package/dist/conversation/roster.js.map +1 -0
- package/dist/conversation/rosterProjection.d.ts +52 -0
- package/dist/conversation/rosterProjection.d.ts.map +1 -0
- package/dist/conversation/rosterProjection.js +74 -0
- package/dist/conversation/rosterProjection.js.map +1 -0
- package/dist/conversation/router.d.ts +74 -0
- package/dist/conversation/router.d.ts.map +1 -0
- package/dist/conversation/router.js +58 -0
- package/dist/conversation/router.js.map +1 -0
- package/dist/conversation/runtime.d.ts +64 -0
- package/dist/conversation/runtime.d.ts.map +1 -0
- package/dist/conversation/runtime.js +172 -0
- package/dist/conversation/runtime.js.map +1 -0
- package/dist/conversation/types.d.ts +99 -0
- package/dist/conversation/types.d.ts.map +1 -0
- package/dist/conversation/types.js +13 -0
- package/dist/conversation/types.js.map +1 -0
- package/dist/conversation/voice.d.ts +185 -0
- package/dist/conversation/voice.d.ts.map +1 -0
- package/dist/conversation/voice.js +307 -0
- package/dist/conversation/voice.js.map +1 -0
- package/dist/graphSchema.d.ts +260 -0
- package/dist/graphSchema.d.ts.map +1 -0
- package/dist/graphSchema.js +354 -0
- package/dist/graphSchema.js.map +1 -0
- package/dist/http.d.ts +20 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +57 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -1
- package/dist/platformAssistant.d.ts +155 -0
- package/dist/platformAssistant.d.ts.map +1 -0
- package/dist/platformAssistant.js +256 -0
- package/dist/platformAssistant.js.map +1 -0
- package/dist/reactor/abortUtils.d.ts +16 -0
- package/dist/reactor/abortUtils.d.ts.map +1 -0
- package/dist/reactor/abortUtils.js +26 -0
- package/dist/reactor/abortUtils.js.map +1 -0
- package/dist/reactor/applyScenePrompt.d.ts +75 -0
- package/dist/reactor/applyScenePrompt.d.ts.map +1 -0
- package/dist/reactor/applyScenePrompt.js +60 -0
- package/dist/reactor/applyScenePrompt.js.map +1 -0
- package/dist/reactor/chunkDistribution.d.ts +43 -0
- package/dist/reactor/chunkDistribution.d.ts.map +1 -0
- package/dist/reactor/chunkDistribution.js +85 -0
- package/dist/reactor/chunkDistribution.js.map +1 -0
- package/dist/reactor/heliosCommandDispatcher.d.ts.map +1 -1
- package/dist/reactor/heliosCommandDispatcher.js +10 -15
- package/dist/reactor/heliosCommandDispatcher.js.map +1 -1
- package/dist/reactor/index.d.ts +4 -0
- package/dist/reactor/index.d.ts.map +1 -1
- package/dist/reactor/index.js +7 -0
- package/dist/reactor/index.js.map +1 -1
- package/dist/reactor/longLiveCommandDispatcher.d.ts +103 -0
- package/dist/reactor/longLiveCommandDispatcher.d.ts.map +1 -0
- package/dist/reactor/longLiveCommandDispatcher.js +87 -0
- package/dist/reactor/longLiveCommandDispatcher.js.map +1 -0
- package/dist/reactor/sceneEntry.d.ts +40 -6
- package/dist/reactor/sceneEntry.d.ts.map +1 -1
- package/dist/reactor/sceneEntry.js +63 -24
- package/dist/reactor/sceneEntry.js.map +1 -1
- package/dist/reactor/scenesFromEnrichment.d.ts.map +1 -1
- package/dist/reactor/scenesFromEnrichment.js +2 -66
- package/dist/reactor/scenesFromEnrichment.js.map +1 -1
- package/dist/reactor/seedImageOrchestrator.d.ts +42 -2
- package/dist/reactor/seedImageOrchestrator.d.ts.map +1 -1
- package/dist/reactor/seedImageOrchestrator.js +173 -4
- package/dist/reactor/seedImageOrchestrator.js.map +1 -1
- package/dist/reactor/storyTimeline.d.ts +44 -0
- package/dist/reactor/storyTimeline.d.ts.map +1 -0
- package/dist/reactor/storyTimeline.js +95 -0
- package/dist/reactor/storyTimeline.js.map +1 -0
- package/dist/storyGraph.d.ts +70 -0
- package/dist/storyGraph.d.ts.map +1 -0
- package/dist/storyGraph.js +178 -0
- package/dist/storyGraph.js.map +1 -0
- package/dist/timeFormat.d.ts +10 -0
- package/dist/timeFormat.d.ts.map +1 -1
- package/dist/timeFormat.js +12 -0
- package/dist/timeFormat.js.map +1 -1
- package/dist/toolSchemas.d.ts +370 -0
- package/dist/toolSchemas.d.ts.map +1 -0
- package/dist/toolSchemas.js +182 -0
- package/dist/toolSchemas.js.map +1 -0
- package/dist/vision/VisionPublisher.d.ts +115 -0
- package/dist/vision/VisionPublisher.d.ts.map +1 -0
- package/dist/vision/VisionPublisher.js +475 -0
- package/dist/vision/VisionPublisher.js.map +1 -0
- package/dist/vision/VisionSourceController.d.ts +127 -0
- package/dist/vision/VisionSourceController.d.ts.map +1 -0
- package/dist/vision/VisionSourceController.js +284 -0
- package/dist/vision/VisionSourceController.js.map +1 -0
- package/dist/vision/credentials.d.ts +22 -0
- package/dist/vision/credentials.d.ts.map +1 -0
- package/dist/vision/credentials.js +34 -0
- package/dist/vision/credentials.js.map +1 -0
- package/dist/vision/index.d.ts +18 -0
- package/dist/vision/index.d.ts.map +1 -0
- package/dist/vision/index.js +13 -0
- package/dist/vision/index.js.map +1 -0
- package/dist/vision/sourceKinds.d.ts +65 -0
- package/dist/vision/sourceKinds.d.ts.map +1 -0
- package/dist/vision/sourceKinds.js +26 -0
- package/dist/vision/sourceKinds.js.map +1 -0
- package/dist/vision/startVisionPublisher.d.ts +38 -0
- package/dist/vision/startVisionPublisher.d.ts.map +1 -0
- package/dist/vision/startVisionPublisher.js +44 -0
- package/dist/vision/startVisionPublisher.js.map +1 -0
- package/dist/vision/types.d.ts +95 -0
- package/dist/vision/types.d.ts.map +1 -0
- package/dist/vision/types.js +12 -0
- package/dist/vision/types.js.map +1 -0
- package/dist/vision/visionScheduler.d.ts +63 -0
- package/dist/vision/visionScheduler.d.ts.map +1 -0
- package/dist/vision/visionScheduler.js +67 -0
- package/dist/vision/visionScheduler.js.map +1 -0
- package/dist/websocket.d.ts +195 -0
- package/dist/websocket.d.ts.map +1 -1
- package/dist/websocket.js +179 -1
- package/dist/websocket.js.map +1 -1
- package/package.json +11 -1
- package/src/__tests__/adapters-capabilities.test.ts +46 -0
- package/src/__tests__/adapters-chat.test.ts +172 -0
- package/src/__tests__/adapters-director.test.ts +95 -0
- package/src/__tests__/adapters-image.test.ts +133 -0
- package/src/__tests__/adapters-platform.test.ts +95 -0
- package/src/__tests__/adapters-registry.test.ts +189 -0
- package/src/__tests__/adapters-world.test.ts +296 -0
- package/src/__tests__/admin.test.ts +312 -0
- package/src/__tests__/adventures-party.test.ts +219 -0
- package/src/__tests__/adventures.test.ts +282 -0
- package/src/__tests__/api-pollers.test.ts +104 -0
- package/src/__tests__/api.clip-lookup.test.ts +64 -0
- package/src/__tests__/api.list-images-filters.test.ts +103 -0
- package/src/__tests__/api.test.ts +470 -0
- package/src/__tests__/audio.test.ts +65 -0
- package/src/__tests__/cast.test.ts +95 -0
- package/src/__tests__/castProjection.test.ts +167 -0
- package/src/__tests__/chat-auth.test.ts +91 -0
- package/src/__tests__/conversation/controller.test.ts +697 -0
- package/src/__tests__/conversation/effectiveModel.test.ts +169 -0
- package/src/__tests__/conversation/headlessRenderer.ts +46 -0
- package/src/__tests__/conversation/presenceBus.test.ts +236 -0
- package/src/__tests__/conversation/presenceRegistry.test.ts +73 -0
- package/src/__tests__/conversation/roster.test.ts +100 -0
- package/src/__tests__/conversation/rosterProjection.test.ts +142 -0
- package/src/__tests__/conversation/router.test.ts +81 -0
- package/src/__tests__/conversation/runtime.test.ts +206 -0
- package/src/__tests__/conversation/voice.test.ts +440 -0
- package/src/__tests__/documents.test.ts +360 -0
- package/src/__tests__/graphSchema.test.ts +326 -0
- package/src/__tests__/longLiveCommandDispatcher.test.ts +185 -0
- package/src/__tests__/platformAssistant.test.ts +484 -0
- package/src/__tests__/reactor/applyScenePrompt.test.ts +192 -0
- package/src/__tests__/reactor/heliosCommandDispatcher.test.ts +4 -3
- package/src/__tests__/reactor/sceneEntry.test.ts +134 -10
- package/src/__tests__/reactor/seedImageOrchestrator.test.ts +51 -16
- package/src/__tests__/reactor/storyTimeline.test.ts +186 -0
- package/src/__tests__/regeneration.test.ts +486 -0
- package/src/__tests__/stories.test.ts +171 -0
- package/src/__tests__/storyGraph.test.ts +134 -0
- package/src/__tests__/timeFormat.test.ts +22 -1
- package/src/__tests__/toolSchemas.test.ts +57 -0
- package/src/__tests__/tools-wrappers.test.ts +272 -0
- package/src/__tests__/videoGraph.test.ts +94 -0
- package/src/__tests__/websocket-vision.test.ts +265 -0
- package/src/__tests__/websocket.test.ts +213 -0
- package/src/adapters/capabilities.ts +24 -0
- package/src/adapters/chatAdapter.ts +114 -0
- package/src/adapters/directorAdapter.ts +126 -0
- package/src/adapters/imageAdapter.ts +100 -0
- package/src/adapters/index.ts +67 -0
- package/src/adapters/registry.ts +159 -0
- package/src/adapters/worldAdapter.ts +272 -0
- package/src/admin.ts +310 -0
- package/src/api.ts +1890 -162
- package/src/audio.ts +35 -0
- package/src/castProjection.ts +95 -0
- package/src/conversation/binding.ts +81 -0
- package/src/conversation/controller.ts +439 -0
- package/src/conversation/effectiveModel.ts +111 -0
- package/src/conversation/index.ts +27 -0
- package/src/conversation/participant.ts +71 -0
- package/src/conversation/presence.ts +60 -0
- package/src/conversation/presenceBus.ts +109 -0
- package/src/conversation/presenceRegistry.ts +83 -0
- package/src/conversation/roster.ts +111 -0
- package/src/conversation/rosterProjection.ts +98 -0
- package/src/conversation/router.ts +96 -0
- package/src/conversation/runtime.ts +188 -0
- package/src/conversation/types.ts +100 -0
- package/src/conversation/voice.ts +402 -0
- package/src/graphSchema.ts +532 -0
- package/src/http.ts +72 -0
- package/src/index.ts +12 -0
- package/src/platformAssistant.ts +347 -0
- package/src/reactor/abortUtils.ts +22 -0
- package/src/reactor/applyScenePrompt.ts +136 -0
- package/src/reactor/chunkDistribution.ts +98 -0
- package/src/reactor/heliosCommandDispatcher.ts +10 -18
- package/src/reactor/index.ts +7 -0
- package/src/reactor/longLiveCommandDispatcher.ts +156 -0
- package/src/reactor/sceneEntry.ts +104 -33
- package/src/reactor/scenesFromEnrichment.ts +11 -76
- package/src/reactor/seedImageOrchestrator.ts +200 -4
- package/src/reactor/storyTimeline.ts +127 -0
- package/src/storyGraph.ts +298 -0
- package/src/timeFormat.ts +13 -0
- package/src/toolSchemas.ts +225 -0
- package/src/vision/VisionPublisher.ts +553 -0
- package/src/vision/VisionSourceController.ts +354 -0
- package/src/vision/__tests__/VisionPublisher.kvs.test.ts +244 -0
- package/src/vision/__tests__/VisionPublisher.test.ts +301 -0
- package/src/vision/__tests__/VisionSourceController.test.ts +719 -0
- package/src/vision/__tests__/startVisionPublisher.test.ts +251 -0
- package/src/vision/__tests__/toVisionRtcCredentials.test.ts +113 -0
- package/src/vision/__tests__/visionScheduler.test.ts +149 -0
- package/src/vision/credentials.ts +39 -0
- package/src/vision/index.ts +44 -0
- package/src/vision/sourceKinds.ts +75 -0
- package/src/vision/startVisionPublisher.ts +64 -0
- package/src/vision/types.ts +101 -0
- package/src/vision/visionScheduler.ts +102 -0
- package/src/websocket.ts +334 -4
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for dispatchLongLiveCommand — the LongLive-v2 analogue of
|
|
3
|
+
* dispatchHeliosCommand. Maps each LongLiveCommand to the right
|
|
4
|
+
* `reactor.sendCommand` call(s) over the SAME structural Reactor bridge.
|
|
5
|
+
*
|
|
6
|
+
* Coverage:
|
|
7
|
+
* - set_shot at the opening shot: set_shot + start when !hasStarted; NO start
|
|
8
|
+
* when hasStarted:true.
|
|
9
|
+
* - scene_cut (the hard, memory-purging transition Helios lacks).
|
|
10
|
+
* - schedule_shot / schedule_scene_cut: snake_case `at_session_chunk` wire field.
|
|
11
|
+
* - pause / resume / reset → correct sendCommand names with {} payload.
|
|
12
|
+
* - set_seed → sendCommand('set_seed', { seed }).
|
|
13
|
+
* - an already-aborted signal makes the dispatch a no-op.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
dispatchLongLiveCommand,
|
|
18
|
+
type LongLiveCommand,
|
|
19
|
+
type LongLiveToolContext,
|
|
20
|
+
} from '../reactor/longLiveCommandDispatcher';
|
|
21
|
+
|
|
22
|
+
type AnyMock = jest.Mock;
|
|
23
|
+
|
|
24
|
+
function makeReactor() {
|
|
25
|
+
return {
|
|
26
|
+
sendCommand: jest.fn().mockResolvedValue(undefined) as AnyMock,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Build a minimal LongLiveToolContext over a mock bridge. */
|
|
31
|
+
function makeCtx(
|
|
32
|
+
reactor: ReturnType<typeof makeReactor>,
|
|
33
|
+
overrides: Partial<LongLiveToolContext> = {},
|
|
34
|
+
): LongLiveToolContext {
|
|
35
|
+
return { reactor, ...overrides };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('dispatchLongLiveCommand', () => {
|
|
39
|
+
describe('set_shot — opening-shot-then-start guard', () => {
|
|
40
|
+
it('fires set_shot then start at the opening shot when !hasStarted', async () => {
|
|
41
|
+
const reactor = makeReactor();
|
|
42
|
+
await dispatchLongLiveCommand(
|
|
43
|
+
{ type: 'set_shot', prompt: 'a neon alley at night' },
|
|
44
|
+
makeCtx(reactor),
|
|
45
|
+
);
|
|
46
|
+
expect(reactor.sendCommand.mock.calls).toEqual([
|
|
47
|
+
['set_shot', { prompt: 'a neon alley at night' }],
|
|
48
|
+
['start', {}],
|
|
49
|
+
]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('suppresses the start re-emit when hasStarted:true', async () => {
|
|
53
|
+
const reactor = makeReactor();
|
|
54
|
+
await dispatchLongLiveCommand(
|
|
55
|
+
{ type: 'set_shot', prompt: 'a wider crane shot' },
|
|
56
|
+
makeCtx(reactor, { hasStarted: true }),
|
|
57
|
+
);
|
|
58
|
+
expect(reactor.sendCommand.mock.calls).toEqual([
|
|
59
|
+
['set_shot', { prompt: 'a wider crane shot' }],
|
|
60
|
+
]);
|
|
61
|
+
const names = reactor.sendCommand.mock.calls.map((c) => c[0]);
|
|
62
|
+
expect(names).not.toContain('start');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('scene_cut → sendCommand("scene_cut", { prompt }) — the hard transition', async () => {
|
|
67
|
+
const reactor = makeReactor();
|
|
68
|
+
await dispatchLongLiveCommand(
|
|
69
|
+
{ type: 'scene_cut', prompt: 'cut to a sunlit beach' },
|
|
70
|
+
makeCtx(reactor),
|
|
71
|
+
);
|
|
72
|
+
expect(reactor.sendCommand.mock.calls).toEqual([
|
|
73
|
+
['scene_cut', { prompt: 'cut to a sunlit beach' }],
|
|
74
|
+
]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('schedule_shot → snake_case at_session_chunk on the wire', async () => {
|
|
78
|
+
const reactor = makeReactor();
|
|
79
|
+
await dispatchLongLiveCommand(
|
|
80
|
+
{ type: 'schedule_shot', prompt: 'pan to the harbor', atSessionChunk: 42 },
|
|
81
|
+
makeCtx(reactor),
|
|
82
|
+
);
|
|
83
|
+
expect(reactor.sendCommand.mock.calls).toEqual([
|
|
84
|
+
['schedule_shot', { prompt: 'pan to the harbor', at_session_chunk: 42 }],
|
|
85
|
+
]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('schedule_scene_cut → snake_case at_session_chunk on the wire', async () => {
|
|
89
|
+
const reactor = makeReactor();
|
|
90
|
+
await dispatchLongLiveCommand(
|
|
91
|
+
{
|
|
92
|
+
type: 'schedule_scene_cut',
|
|
93
|
+
prompt: 'hard cut to the throne room',
|
|
94
|
+
atSessionChunk: 96,
|
|
95
|
+
},
|
|
96
|
+
makeCtx(reactor),
|
|
97
|
+
);
|
|
98
|
+
expect(reactor.sendCommand.mock.calls).toEqual([
|
|
99
|
+
[
|
|
100
|
+
'schedule_scene_cut',
|
|
101
|
+
{ prompt: 'hard cut to the throne room', at_session_chunk: 96 },
|
|
102
|
+
],
|
|
103
|
+
]);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it.each([
|
|
107
|
+
['pause', 'pause'],
|
|
108
|
+
['resume', 'resume'],
|
|
109
|
+
['reset', 'reset'],
|
|
110
|
+
])('%s → sendCommand("%s", {})', async (cmdType, expected) => {
|
|
111
|
+
const reactor = makeReactor();
|
|
112
|
+
await dispatchLongLiveCommand(
|
|
113
|
+
{ type: cmdType } as LongLiveCommand,
|
|
114
|
+
makeCtx(reactor),
|
|
115
|
+
);
|
|
116
|
+
expect(reactor.sendCommand.mock.calls).toEqual([[expected, {}]]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('set_seed → sendCommand("set_seed", { seed })', async () => {
|
|
120
|
+
const reactor = makeReactor();
|
|
121
|
+
await dispatchLongLiveCommand(
|
|
122
|
+
{ type: 'set_seed', seed: 1234 },
|
|
123
|
+
makeCtx(reactor),
|
|
124
|
+
);
|
|
125
|
+
expect(reactor.sendCommand.mock.calls).toEqual([
|
|
126
|
+
['set_seed', { seed: 1234 }],
|
|
127
|
+
]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('abort signal', () => {
|
|
131
|
+
it('returns immediately without calling sendCommand when already aborted', async () => {
|
|
132
|
+
const reactor = makeReactor();
|
|
133
|
+
const controller = new AbortController();
|
|
134
|
+
controller.abort();
|
|
135
|
+
|
|
136
|
+
await dispatchLongLiveCommand(
|
|
137
|
+
{ type: 'set_shot', prompt: 'should not fire' },
|
|
138
|
+
makeCtx(reactor, { signal: controller.signal }),
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
expect(reactor.sendCommand).not.toHaveBeenCalled();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('is silent and emits no start when an abort is thrown mid set_shot', async () => {
|
|
145
|
+
const reactor = makeReactor();
|
|
146
|
+
// The opening set_shot aborts (DOMException AbortError) before start fires.
|
|
147
|
+
reactor.sendCommand.mockRejectedValueOnce(
|
|
148
|
+
new DOMException('aborted', 'AbortError'),
|
|
149
|
+
);
|
|
150
|
+
await expect(
|
|
151
|
+
dispatchLongLiveCommand(
|
|
152
|
+
{ type: 'set_shot', prompt: 'x' },
|
|
153
|
+
makeCtx(reactor),
|
|
154
|
+
),
|
|
155
|
+
).resolves.toBeUndefined(); // isAbortError → silent, not a rejection
|
|
156
|
+
// set_shot was attempted; the abort short-circuited the start re-emit.
|
|
157
|
+
expect(reactor.sendCommand.mock.calls).toEqual([
|
|
158
|
+
['set_shot', { prompt: 'x' }],
|
|
159
|
+
]);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('swallows a sendCommand failure so the chat keeps working', async () => {
|
|
164
|
+
const reactor = makeReactor();
|
|
165
|
+
reactor.sendCommand.mockRejectedValueOnce(new Error('reactor down'));
|
|
166
|
+
// Must resolve (not reject) — the failure is logged + skipped.
|
|
167
|
+
await expect(
|
|
168
|
+
dispatchLongLiveCommand(
|
|
169
|
+
{ type: 'scene_cut', prompt: 'x' },
|
|
170
|
+
makeCtx(reactor),
|
|
171
|
+
),
|
|
172
|
+
).resolves.toBeUndefined();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('swallows a non-abort error during set_shot and does NOT emit start', async () => {
|
|
176
|
+
const reactor = makeReactor();
|
|
177
|
+
reactor.sendCommand.mockRejectedValueOnce(new Error('reactor down'));
|
|
178
|
+
await expect(
|
|
179
|
+
dispatchLongLiveCommand({ type: 'set_shot', prompt: 'x' }, makeCtx(reactor)),
|
|
180
|
+
).resolves.toBeUndefined();
|
|
181
|
+
// set_shot was attempted; the error short-circuited the start re-emit and
|
|
182
|
+
// was swallowed (the opening-shot path Step 3 is most likely to exercise).
|
|
183
|
+
expect(reactor.sendCommand.mock.calls).toEqual([['set_shot', { prompt: 'x' }]]);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the R4c framework-agnostic `PlatformAssistantController`
|
|
3
|
+
* (`src/platformAssistant.ts`).
|
|
4
|
+
*
|
|
5
|
+
* The controller is a thin state holder over `ApiClient.streamAssistant`:
|
|
6
|
+
* it holds the running message list + the current `routeContext`, calls
|
|
7
|
+
* `streamAssistant` on `ask(prompt)`, parses the returned SSE
|
|
8
|
+
* `ReadableStreamDefaultReader` (the SAME `data: {…}\n\n` frame format the
|
|
9
|
+
* web chat page decodes), and fans the decoded `AssistantStreamEvent`
|
|
10
|
+
* variants out to the host callbacks:
|
|
11
|
+
* - `token` -> onTextDelta(content)
|
|
12
|
+
* - `suggestion` -> onSuggestion({ targetForm, fields, rationale? })
|
|
13
|
+
* - `error` -> onError(message)
|
|
14
|
+
* - `done` -> onDone()
|
|
15
|
+
* (`tool_use` / `tool_result` / `citation` are ignored — minimal surface.)
|
|
16
|
+
*
|
|
17
|
+
* The controller MUST stay framework-agnostic (React/Vue/PlayCanvas all
|
|
18
|
+
* consume it) — the final test reads the source file and asserts it imports
|
|
19
|
+
* no UI framework (the R2.1 ConversationController portability mandate).
|
|
20
|
+
*
|
|
21
|
+
* Mirrors the SSE-reader harness + global-fetch save/restore discipline in
|
|
22
|
+
* api.test.ts (a single UTF-8 chunk reader; `jest.restoreAllMocks` only
|
|
23
|
+
* unwinds spyOn, not direct assignment to globalThis.fetch).
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { readFileSync } from 'node:fs';
|
|
27
|
+
import path from 'node:path';
|
|
28
|
+
// jest-environment-jsdom does not expose TextEncoder/TextDecoder as globals.
|
|
29
|
+
// The controller decodes the SSE stream with the BROWSER-global `TextDecoder`
|
|
30
|
+
// (correct in browsers + modern Node — it must stay framework/runtime-agnostic,
|
|
31
|
+
// so it does NOT import from `node:util`). Pull both from Node's `util` here to
|
|
32
|
+
// polyfill the jsdom env (below) so the test exercises that same global path.
|
|
33
|
+
import { TextEncoder, TextDecoder } from 'node:util';
|
|
34
|
+
|
|
35
|
+
import { ApiClient } from '../api';
|
|
36
|
+
import type { AssistantRouteContext } from '../api';
|
|
37
|
+
import {
|
|
38
|
+
PlatformAssistantController,
|
|
39
|
+
type PlatformAssistantSuggestion,
|
|
40
|
+
} from '../platformAssistant';
|
|
41
|
+
|
|
42
|
+
// Install the polyfills onto globalThis BEFORE any test constructs the
|
|
43
|
+
// controller (module-eval time — runs once after the hoisted imports above).
|
|
44
|
+
const g = globalThis as { TextEncoder?: unknown; TextDecoder?: unknown };
|
|
45
|
+
if (g.TextEncoder === undefined) g.TextEncoder = TextEncoder;
|
|
46
|
+
if (g.TextDecoder === undefined) g.TextDecoder = TextDecoder;
|
|
47
|
+
|
|
48
|
+
const TEST_BASE_URL = 'http://test.local';
|
|
49
|
+
const TEST_CHAT_BASE_URL = 'http://test.local/chat';
|
|
50
|
+
|
|
51
|
+
const makeClient = (sessionToken: string = 'test-token') =>
|
|
52
|
+
new ApiClient(TEST_BASE_URL, sessionToken, TEST_CHAT_BASE_URL);
|
|
53
|
+
|
|
54
|
+
const originalFetch = (globalThis as { fetch?: typeof fetch }).fetch;
|
|
55
|
+
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
if (originalFetch === undefined) {
|
|
58
|
+
delete (globalThis as { fetch?: typeof fetch }).fetch;
|
|
59
|
+
} else {
|
|
60
|
+
(globalThis as { fetch?: typeof fetch }).fetch = originalFetch;
|
|
61
|
+
}
|
|
62
|
+
jest.restoreAllMocks();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Build a Response-like object whose `body.getReader()` streams the given
|
|
67
|
+
* SSE text as a single UTF-8 chunk then closes (the shape `fetch` returns for
|
|
68
|
+
* a `text/event-stream` response so `streamAssistant` hands back the reader).
|
|
69
|
+
*/
|
|
70
|
+
const sseResponse = (sseText: string, init?: { ok?: boolean; status?: number }) => {
|
|
71
|
+
const encoder = new TextEncoder();
|
|
72
|
+
let sent = false;
|
|
73
|
+
const reader = {
|
|
74
|
+
read: async () => {
|
|
75
|
+
if (sent) return { done: true, value: undefined };
|
|
76
|
+
sent = true;
|
|
77
|
+
return { done: false, value: encoder.encode(sseText) };
|
|
78
|
+
},
|
|
79
|
+
releaseLock: () => undefined,
|
|
80
|
+
cancel: async () => undefined,
|
|
81
|
+
closed: Promise.resolve(undefined),
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
ok: init?.ok ?? true,
|
|
86
|
+
status: init?.status ?? 200,
|
|
87
|
+
statusText: 'OK',
|
|
88
|
+
headers: {
|
|
89
|
+
get: (h: string) => (h.toLowerCase() === 'content-type' ? 'text/event-stream' : null),
|
|
90
|
+
},
|
|
91
|
+
body: { getReader: () => reader },
|
|
92
|
+
json: async () => ({}),
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** Serialize an SSE event frame the way the backend writes them. */
|
|
97
|
+
const frame = (event: Record<string, unknown>): string => `data: ${JSON.stringify(event)}\n\n`;
|
|
98
|
+
|
|
99
|
+
const ROUTE: AssistantRouteContext = {
|
|
100
|
+
surface: 'studio/cast',
|
|
101
|
+
formId: 'persona',
|
|
102
|
+
entityId: 'story-1',
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
describe('PlatformAssistantController.ask', () => {
|
|
106
|
+
it('streams token events into onTextDelta in order and fires onDone', async () => {
|
|
107
|
+
const sse =
|
|
108
|
+
frame({ type: 'token', content: 'A weary ' }) +
|
|
109
|
+
frame({ type: 'token', content: 'detective.' }) +
|
|
110
|
+
frame({ type: 'done', reason: 'stop' }) +
|
|
111
|
+
'data: [DONE]\n\n';
|
|
112
|
+
const fetchMock = jest.fn().mockResolvedValue(sseResponse(sse));
|
|
113
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
114
|
+
|
|
115
|
+
const deltas: string[] = [];
|
|
116
|
+
let doneCount = 0;
|
|
117
|
+
const controller = new PlatformAssistantController({
|
|
118
|
+
apiClient: makeClient(),
|
|
119
|
+
routeContext: ROUTE,
|
|
120
|
+
callbacks: {
|
|
121
|
+
onTextDelta: (text) => deltas.push(text),
|
|
122
|
+
onDone: () => {
|
|
123
|
+
doneCount += 1;
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
await controller.ask('make a brooding detective');
|
|
129
|
+
|
|
130
|
+
expect(deltas).toEqual(['A weary ', 'detective.']);
|
|
131
|
+
expect(doneCount).toBe(1);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('decodes a suggestion event into onSuggestion with targetForm + fields + rationale', async () => {
|
|
135
|
+
const sse =
|
|
136
|
+
frame({
|
|
137
|
+
type: 'suggestion',
|
|
138
|
+
targetForm: 'persona',
|
|
139
|
+
fields: { name: 'Cole Tarrant', systemPrompt: 'A weary, brooding detective.' },
|
|
140
|
+
rationale: 'Matches the noir brief.',
|
|
141
|
+
}) +
|
|
142
|
+
frame({ type: 'done' }) +
|
|
143
|
+
'data: [DONE]\n\n';
|
|
144
|
+
const fetchMock = jest.fn().mockResolvedValue(sseResponse(sse));
|
|
145
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
146
|
+
|
|
147
|
+
const suggestions: PlatformAssistantSuggestion[] = [];
|
|
148
|
+
const controller = new PlatformAssistantController({
|
|
149
|
+
apiClient: makeClient(),
|
|
150
|
+
routeContext: ROUTE,
|
|
151
|
+
callbacks: { onSuggestion: (s) => suggestions.push(s) },
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
await controller.ask('propose persona fields');
|
|
155
|
+
|
|
156
|
+
expect(suggestions).toHaveLength(1);
|
|
157
|
+
expect(suggestions[0].targetForm).toBe('persona');
|
|
158
|
+
expect(suggestions[0].fields).toEqual({
|
|
159
|
+
name: 'Cole Tarrant',
|
|
160
|
+
systemPrompt: 'A weary, brooding detective.',
|
|
161
|
+
});
|
|
162
|
+
expect(suggestions[0].rationale).toBe('Matches the noir brief.');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('surfaces an error event through onError', async () => {
|
|
166
|
+
const sse = frame({ type: 'error', message: 'model unavailable' }) + 'data: [DONE]\n\n';
|
|
167
|
+
const fetchMock = jest.fn().mockResolvedValue(sseResponse(sse));
|
|
168
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
169
|
+
|
|
170
|
+
const errors: string[] = [];
|
|
171
|
+
const controller = new PlatformAssistantController({
|
|
172
|
+
apiClient: makeClient(),
|
|
173
|
+
routeContext: ROUTE,
|
|
174
|
+
callbacks: { onError: (m) => errors.push(m) },
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
await controller.ask('anything');
|
|
178
|
+
|
|
179
|
+
expect(errors).toEqual(['model unavailable']);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('surfaces a TRANSPORT error via onError and never rejects ask()', async () => {
|
|
183
|
+
// streamAssistant rejects (network/transport) — not an SSE `error` frame.
|
|
184
|
+
const fetchMock = jest.fn().mockRejectedValue(new Error('network down'));
|
|
185
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
186
|
+
|
|
187
|
+
const errors: string[] = [];
|
|
188
|
+
const controller = new PlatformAssistantController({
|
|
189
|
+
apiClient: makeClient(),
|
|
190
|
+
routeContext: ROUTE,
|
|
191
|
+
callbacks: { onError: (m) => errors.push(m) },
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// ask() must RESOLVE (not reject) — the framework-agnostic contract.
|
|
195
|
+
await expect(controller.ask('anything')).resolves.toBeUndefined();
|
|
196
|
+
expect(errors).toEqual(['network down']);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('threads the current routeContext + accumulated message history into streamAssistant', async () => {
|
|
200
|
+
const fetchMock = jest
|
|
201
|
+
.fn()
|
|
202
|
+
.mockResolvedValueOnce(
|
|
203
|
+
sseResponse(frame({ type: 'token', content: 'hi' }) + 'data: [DONE]\n\n'),
|
|
204
|
+
)
|
|
205
|
+
.mockResolvedValueOnce(sseResponse('data: [DONE]\n\n'));
|
|
206
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
207
|
+
|
|
208
|
+
const controller = new PlatformAssistantController({
|
|
209
|
+
apiClient: makeClient(),
|
|
210
|
+
routeContext: ROUTE,
|
|
211
|
+
callbacks: {},
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
await controller.ask('first');
|
|
215
|
+
// The second ask must carry the first user prompt + the streamed assistant
|
|
216
|
+
// reply as prior history (the running message list).
|
|
217
|
+
await controller.ask('second');
|
|
218
|
+
|
|
219
|
+
const secondBody = JSON.parse(fetchMock.mock.calls[1][1].body as string);
|
|
220
|
+
expect(secondBody.prompt).toBe('second');
|
|
221
|
+
expect(secondBody.routeContext).toEqual(ROUTE);
|
|
222
|
+
expect(secondBody.messages).toEqual([
|
|
223
|
+
{ role: 'user', content: 'first' },
|
|
224
|
+
{ role: 'assistant', content: 'hi' },
|
|
225
|
+
]);
|
|
226
|
+
// No videoId — the assistant is out-of-world.
|
|
227
|
+
expect(secondBody).not.toHaveProperty('videoId');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('threads ask() documentContext into the request only when supplied (R11.2)', async () => {
|
|
231
|
+
const fetchMock = jest
|
|
232
|
+
.fn()
|
|
233
|
+
.mockResolvedValue(sseResponse('data: [DONE]\n\n'));
|
|
234
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
235
|
+
|
|
236
|
+
const controller = new PlatformAssistantController({
|
|
237
|
+
apiClient: makeClient(),
|
|
238
|
+
routeContext: ROUTE,
|
|
239
|
+
callbacks: {},
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// WITH documentContext: the large draft rides the dedicated field.
|
|
243
|
+
await controller.ask('Refine this prompt; return only the body.', {
|
|
244
|
+
documentContext: 'A 40 KB draft body the assistant should operate on…',
|
|
245
|
+
});
|
|
246
|
+
const withCtx = JSON.parse(fetchMock.mock.calls[0][1].body as string);
|
|
247
|
+
expect(withCtx.prompt).toBe('Refine this prompt; return only the body.');
|
|
248
|
+
expect(withCtx.documentContext).toBe(
|
|
249
|
+
'A 40 KB draft body the assistant should operate on…',
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
// WITHOUT documentContext (plain ask): the key is ABSENT — the wire payload
|
|
253
|
+
// is byte-identical to the pre-R11.2 one (no empty/undefined documentContext).
|
|
254
|
+
await controller.ask('just a question');
|
|
255
|
+
const withoutCtx = JSON.parse(fetchMock.mock.calls[1][1].body as string);
|
|
256
|
+
expect(withoutCtx).not.toHaveProperty('documentContext');
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('omits documentContext when an empty string is passed (no empty wire field)', async () => {
|
|
260
|
+
const fetchMock = jest.fn().mockResolvedValue(sseResponse('data: [DONE]\n\n'));
|
|
261
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
262
|
+
|
|
263
|
+
const controller = new PlatformAssistantController({
|
|
264
|
+
apiClient: makeClient(),
|
|
265
|
+
routeContext: ROUTE,
|
|
266
|
+
callbacks: {},
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
await controller.ask('hi', { documentContext: '' });
|
|
270
|
+
const body = JSON.parse(fetchMock.mock.calls[0][1].body as string);
|
|
271
|
+
expect(body).not.toHaveProperty('documentContext');
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('setRouteContext updates the context sent on the next ask', async () => {
|
|
275
|
+
const fetchMock = jest.fn().mockResolvedValue(sseResponse('data: [DONE]\n\n'));
|
|
276
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
277
|
+
|
|
278
|
+
const controller = new PlatformAssistantController({
|
|
279
|
+
apiClient: makeClient(),
|
|
280
|
+
routeContext: ROUTE,
|
|
281
|
+
callbacks: {},
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
const next: AssistantRouteContext = { surface: 'settings/defaults', formId: 'prompt' };
|
|
285
|
+
controller.setRouteContext(next);
|
|
286
|
+
await controller.ask('improve this');
|
|
287
|
+
|
|
288
|
+
const body = JSON.parse(fetchMock.mock.calls[0][1].body as string);
|
|
289
|
+
expect(body.routeContext).toEqual(next);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('abort() cancels the in-flight stream via the AbortSignal', async () => {
|
|
293
|
+
let capturedSignal: AbortSignal | undefined;
|
|
294
|
+
// A reader whose first read() blocks until the signal aborts (so the stream
|
|
295
|
+
// is genuinely IN FLIGHT when abort() is called — the real cancel path).
|
|
296
|
+
const blockingResponse = (signal?: AbortSignal) => {
|
|
297
|
+
const reader = {
|
|
298
|
+
read: () =>
|
|
299
|
+
new Promise<{ done: boolean; value: undefined }>((resolve) => {
|
|
300
|
+
const finish = () => resolve({ done: true, value: undefined });
|
|
301
|
+
// Resolve immediately if abort already fired before this read()
|
|
302
|
+
// subscribed; otherwise resolve when it does.
|
|
303
|
+
if (signal?.aborted) {
|
|
304
|
+
finish();
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
signal?.addEventListener('abort', finish, { once: true });
|
|
308
|
+
}),
|
|
309
|
+
releaseLock: () => undefined,
|
|
310
|
+
cancel: async () => undefined,
|
|
311
|
+
closed: Promise.resolve(undefined),
|
|
312
|
+
};
|
|
313
|
+
return {
|
|
314
|
+
ok: true,
|
|
315
|
+
status: 200,
|
|
316
|
+
statusText: 'OK',
|
|
317
|
+
headers: { get: () => 'text/event-stream' },
|
|
318
|
+
body: { getReader: () => reader },
|
|
319
|
+
json: async () => ({}),
|
|
320
|
+
};
|
|
321
|
+
};
|
|
322
|
+
const fetchMock = jest.fn().mockImplementation((_url: string, init: RequestInit) => {
|
|
323
|
+
capturedSignal = init.signal ?? undefined;
|
|
324
|
+
return Promise.resolve(blockingResponse(init.signal ?? undefined));
|
|
325
|
+
});
|
|
326
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
327
|
+
|
|
328
|
+
const controller = new PlatformAssistantController({
|
|
329
|
+
apiClient: makeClient(),
|
|
330
|
+
routeContext: ROUTE,
|
|
331
|
+
callbacks: {},
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
// Do NOT await — the stream blocks until we abort it.
|
|
335
|
+
const pending = controller.ask('start');
|
|
336
|
+
// Let the fetch + getReader resolve so the controller is mid-stream.
|
|
337
|
+
await Promise.resolve();
|
|
338
|
+
await Promise.resolve();
|
|
339
|
+
expect(capturedSignal).toBeDefined();
|
|
340
|
+
expect(capturedSignal!.aborted).toBe(false);
|
|
341
|
+
|
|
342
|
+
controller.abort();
|
|
343
|
+
expect(capturedSignal!.aborted).toBe(true);
|
|
344
|
+
await pending; // resolves once the blocked read() unblocks on abort
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it('ask() after dispose() surfaces onError and does NOT throw', async () => {
|
|
348
|
+
const errors: string[] = [];
|
|
349
|
+
const controller = new PlatformAssistantController({
|
|
350
|
+
apiClient: makeClient(),
|
|
351
|
+
routeContext: ROUTE,
|
|
352
|
+
callbacks: { onError: (m) => errors.push(m) },
|
|
353
|
+
});
|
|
354
|
+
controller.dispose();
|
|
355
|
+
// Honors the "never rejects" contract — disposed routes through onError.
|
|
356
|
+
await expect(controller.ask('after dispose')).resolves.toBeUndefined();
|
|
357
|
+
expect(errors.some((m) => /dispose/i.test(m))).toBe(true);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it('a turn that produces no reply commits no message (no dangling user turn)', async () => {
|
|
361
|
+
// Empty reply (just [DONE]) ⇒ the user turn is NOT committed, so a re-entrant
|
|
362
|
+
// ask() that aborts a reply-less turn never leaves a dangling user message.
|
|
363
|
+
const fetchMock = jest.fn().mockResolvedValue(sseResponse('data: [DONE]\n\n'));
|
|
364
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
365
|
+
|
|
366
|
+
const controller = new PlatformAssistantController({
|
|
367
|
+
apiClient: makeClient(),
|
|
368
|
+
routeContext: ROUTE,
|
|
369
|
+
callbacks: {},
|
|
370
|
+
});
|
|
371
|
+
await controller.ask('no reply expected');
|
|
372
|
+
expect(controller.getMessages()).toEqual([]);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it('fires onDone when the stream closes WITHOUT a [DONE] sentinel (server crash/timeout)', async () => {
|
|
376
|
+
// The reader closes mid-stream with NO done event / [DONE] sentinel — the
|
|
377
|
+
// turn is still over, so onDone MUST fire (else the overlay sticks streaming).
|
|
378
|
+
const sse = frame({ type: 'token', content: 'partial answer' }); // no [DONE]
|
|
379
|
+
const fetchMock = jest.fn().mockResolvedValue(sseResponse(sse));
|
|
380
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
381
|
+
|
|
382
|
+
const done = jest.fn();
|
|
383
|
+
const controller = new PlatformAssistantController({
|
|
384
|
+
apiClient: makeClient(),
|
|
385
|
+
routeContext: ROUTE,
|
|
386
|
+
callbacks: { onDone: done },
|
|
387
|
+
});
|
|
388
|
+
await controller.ask('x');
|
|
389
|
+
expect(done).toHaveBeenCalledTimes(1);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it('processes a final frame that arrives WITHOUT a trailing newline (server closed mid-frame)', async () => {
|
|
393
|
+
// The last frame has no terminating `\n\n`, so it sits in the line buffer when
|
|
394
|
+
// the reader closes. The close-time flush must still parse it (via the shared
|
|
395
|
+
// handleLine path) so the final token is not silently dropped.
|
|
396
|
+
const sse =
|
|
397
|
+
frame({ type: 'token', content: 'first ' }) +
|
|
398
|
+
'data: {"type":"token","content":"last"}';
|
|
399
|
+
const fetchMock = jest.fn().mockResolvedValue(sseResponse(sse));
|
|
400
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
401
|
+
|
|
402
|
+
const deltas: string[] = [];
|
|
403
|
+
const controller = new PlatformAssistantController({
|
|
404
|
+
apiClient: makeClient(),
|
|
405
|
+
routeContext: ROUTE,
|
|
406
|
+
callbacks: { onTextDelta: (t) => deltas.push(t) },
|
|
407
|
+
});
|
|
408
|
+
await controller.ask('x');
|
|
409
|
+
expect(deltas).toEqual(['first ', 'last']);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it('releases the response-body reader lock when the stream closes', async () => {
|
|
413
|
+
// Hygiene (claude[bot] round-7): the consume() finally must releaseLock() on
|
|
414
|
+
// the normal-close path so the body reader does not stay locked.
|
|
415
|
+
const releaseLock = jest.fn();
|
|
416
|
+
const encoder = new TextEncoder();
|
|
417
|
+
let sent = false;
|
|
418
|
+
const reader = {
|
|
419
|
+
read: async () => {
|
|
420
|
+
if (sent) return { done: true, value: undefined };
|
|
421
|
+
sent = true;
|
|
422
|
+
return {
|
|
423
|
+
done: false,
|
|
424
|
+
value: encoder.encode(frame({ type: 'done' }) + 'data: [DONE]\n\n'),
|
|
425
|
+
};
|
|
426
|
+
},
|
|
427
|
+
releaseLock,
|
|
428
|
+
cancel: async () => undefined,
|
|
429
|
+
closed: Promise.resolve(undefined),
|
|
430
|
+
};
|
|
431
|
+
const response = {
|
|
432
|
+
ok: true,
|
|
433
|
+
status: 200,
|
|
434
|
+
statusText: 'OK',
|
|
435
|
+
headers: { get: () => 'text/event-stream' },
|
|
436
|
+
body: { getReader: () => reader },
|
|
437
|
+
json: async () => ({}),
|
|
438
|
+
};
|
|
439
|
+
const fetchMock = jest.fn().mockResolvedValue(response);
|
|
440
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
441
|
+
|
|
442
|
+
const controller = new PlatformAssistantController({
|
|
443
|
+
apiClient: makeClient(),
|
|
444
|
+
routeContext: ROUTE,
|
|
445
|
+
callbacks: {},
|
|
446
|
+
});
|
|
447
|
+
await controller.ask('x');
|
|
448
|
+
expect(releaseLock).toHaveBeenCalledTimes(1);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it('a self-abort (AbortError) fires onDone, not onError, and resolves', async () => {
|
|
452
|
+
// The reader THROWS AbortError (vs resolving {done:true}) — the turn is over,
|
|
453
|
+
// so onDone fires (a future Cancel button must not leave streaming stuck) and
|
|
454
|
+
// it is NOT surfaced as an error.
|
|
455
|
+
const abortErr = new Error('aborted');
|
|
456
|
+
abortErr.name = 'AbortError';
|
|
457
|
+
const fetchMock = jest.fn().mockRejectedValue(abortErr);
|
|
458
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
459
|
+
|
|
460
|
+
const done = jest.fn();
|
|
461
|
+
const errors: string[] = [];
|
|
462
|
+
const controller = new PlatformAssistantController({
|
|
463
|
+
apiClient: makeClient(),
|
|
464
|
+
routeContext: ROUTE,
|
|
465
|
+
callbacks: { onDone: done, onError: (m) => errors.push(m) },
|
|
466
|
+
});
|
|
467
|
+
await expect(controller.ask('x')).resolves.toBeUndefined();
|
|
468
|
+
expect(done).toHaveBeenCalledTimes(1);
|
|
469
|
+
expect(errors).toEqual([]);
|
|
470
|
+
});
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
describe('PlatformAssistantController portability', () => {
|
|
474
|
+
it('imports no UI framework (framework-agnostic — portable to Vue/PlayCanvas)', () => {
|
|
475
|
+
const source = readFileSync(
|
|
476
|
+
path.join(__dirname, '..', 'platformAssistant.ts'),
|
|
477
|
+
'utf8',
|
|
478
|
+
);
|
|
479
|
+
// No import from react / react-dom / vue / next (or scoped variants).
|
|
480
|
+
expect(source).not.toMatch(/from\s+['"](react|react-dom|vue|next)(\/[^'"]*)?['"]/);
|
|
481
|
+
expect(source).not.toMatch(/from\s+['"]@(react|vue|next)\//);
|
|
482
|
+
expect(source).not.toMatch(/require\(\s*['"](react|react-dom|vue|next)['"]\s*\)/);
|
|
483
|
+
});
|
|
484
|
+
});
|