@escapeaihq/client-core 0.6.1 → 0.8.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 +28 -3
- package/dist/api.d.ts +102 -2
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +92 -0
- package/dist/api.js.map +1 -1
- package/dist/audio.d.ts +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/reactor/entityContext.d.ts +34 -0
- package/dist/reactor/entityContext.d.ts.map +1 -0
- package/dist/reactor/entityContext.js +119 -0
- package/dist/reactor/entityContext.js.map +1 -0
- package/dist/reactor/entitySearch.d.ts +71 -0
- package/dist/reactor/entitySearch.d.ts.map +1 -0
- package/dist/reactor/entitySearch.js +93 -0
- package/dist/reactor/entitySearch.js.map +1 -0
- package/dist/reactor/heliosCommandDispatcher.d.ts +98 -0
- package/dist/reactor/heliosCommandDispatcher.d.ts.map +1 -0
- package/dist/reactor/heliosCommandDispatcher.js +181 -0
- package/dist/reactor/heliosCommandDispatcher.js.map +1 -0
- package/dist/reactor/heliosTools.d.ts +156 -0
- package/dist/reactor/heliosTools.d.ts.map +1 -0
- package/dist/reactor/heliosTools.js +241 -0
- package/dist/reactor/heliosTools.js.map +1 -0
- package/dist/reactor/index.d.ts +17 -0
- package/dist/reactor/index.d.ts.map +1 -0
- package/dist/reactor/index.js +17 -0
- package/dist/reactor/index.js.map +1 -0
- package/dist/reactor/promptAddonRegistry.d.ts +37 -0
- package/dist/reactor/promptAddonRegistry.d.ts.map +1 -0
- package/dist/reactor/promptAddonRegistry.js +38 -0
- package/dist/reactor/promptAddonRegistry.js.map +1 -0
- package/dist/reactor/regenTemplate.d.ts +23 -0
- package/dist/reactor/regenTemplate.d.ts.map +1 -0
- package/dist/reactor/regenTemplate.js +103 -0
- package/dist/reactor/regenTemplate.js.map +1 -0
- package/dist/reactor/sceneEntry.d.ts +79 -0
- package/dist/reactor/sceneEntry.d.ts.map +1 -0
- package/dist/reactor/sceneEntry.js +177 -0
- package/dist/reactor/sceneEntry.js.map +1 -0
- package/dist/reactor/scenesFromEnrichment.d.ts +67 -0
- package/dist/reactor/scenesFromEnrichment.d.ts.map +1 -0
- package/dist/reactor/scenesFromEnrichment.js +344 -0
- package/dist/reactor/scenesFromEnrichment.js.map +1 -0
- package/dist/reactor/seedImageOrchestrator.d.ts +81 -0
- package/dist/reactor/seedImageOrchestrator.d.ts.map +1 -0
- package/dist/reactor/seedImageOrchestrator.js +79 -0
- package/dist/reactor/seedImageOrchestrator.js.map +1 -0
- package/dist/reactor/toolPromptAddon.d.ts +44 -0
- package/dist/reactor/toolPromptAddon.d.ts.map +1 -0
- package/dist/reactor/toolPromptAddon.js +129 -0
- package/dist/reactor/toolPromptAddon.js.map +1 -0
- package/dist/reactor/toolPromptAddonId.d.ts +10 -0
- package/dist/reactor/toolPromptAddonId.d.ts.map +1 -0
- package/dist/reactor/toolPromptAddonId.js +10 -0
- package/dist/reactor/toolPromptAddonId.js.map +1 -0
- package/dist/reactor/types.d.ts +149 -0
- package/dist/reactor/types.d.ts.map +1 -0
- package/dist/reactor/types.js +2 -0
- package/dist/reactor/types.js.map +1 -0
- package/dist/reactor/upsamplePrompt.d.ts +30 -0
- package/dist/reactor/upsamplePrompt.d.ts.map +1 -0
- package/dist/reactor/upsamplePrompt.js +118 -0
- package/dist/reactor/upsamplePrompt.js.map +1 -0
- package/dist/timeFormat.d.ts +28 -0
- package/dist/timeFormat.d.ts.map +1 -0
- package/dist/timeFormat.js +48 -0
- package/dist/timeFormat.js.map +1 -0
- package/dist/websocket.d.ts +31 -0
- package/dist/websocket.d.ts.map +1 -1
- package/dist/websocket.js +20 -0
- package/dist/websocket.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/reactor/__snapshots__/regenTemplate.test.ts.snap +5 -0
- package/src/__tests__/reactor/__snapshots__/scenesFromEnrichment.test.ts.snap +34 -0
- package/src/__tests__/reactor/api-reactor.test.ts +253 -0
- package/src/__tests__/reactor/entityContext.test.ts +237 -0
- package/src/__tests__/reactor/entitySearch.test.ts +223 -0
- package/src/__tests__/reactor/heliosCommandDispatcher.test.ts +438 -0
- package/src/__tests__/reactor/heliosTools.test.ts +164 -0
- package/src/__tests__/reactor/promptAddonRegistry.test.ts +40 -0
- package/src/__tests__/reactor/regenTemplate.test.ts +156 -0
- package/src/__tests__/reactor/sceneEntry.test.ts +342 -0
- package/src/__tests__/reactor/scenesFromEnrichment.test.ts +760 -0
- package/src/__tests__/reactor/seedImageOrchestrator.test.ts +139 -0
- package/src/__tests__/reactor/toolPromptAddon.test.ts +104 -0
- package/src/__tests__/reactor/upsamplePrompt.test.ts +245 -0
- package/src/__tests__/timeFormat.test.ts +80 -0
- package/src/__tests__/websocket.test.ts +75 -0
- package/src/api.ts +170 -2
- package/src/index.ts +2 -0
- package/src/reactor/entityContext.ts +147 -0
- package/src/reactor/entitySearch.ts +139 -0
- package/src/reactor/heliosCommandDispatcher.ts +293 -0
- package/src/reactor/heliosTools.ts +260 -0
- package/src/reactor/index.ts +17 -0
- package/src/reactor/promptAddonRegistry.ts +52 -0
- package/src/reactor/regenTemplate.ts +142 -0
- package/src/reactor/sceneEntry.ts +267 -0
- package/src/reactor/scenesFromEnrichment.ts +393 -0
- package/src/reactor/seedImageOrchestrator.ts +132 -0
- package/src/reactor/toolPromptAddon.ts +135 -0
- package/src/reactor/toolPromptAddonId.ts +9 -0
- package/src/reactor/types.ts +158 -0
- package/src/reactor/upsamplePrompt.ts +154 -0
- package/src/timeFormat.ts +57 -0
- package/src/websocket.ts +42 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for findEntityImageBySeedName — the SWAP_SEED entity lookup
|
|
3
|
+
* lifted out of the Phase 3 chat handler so it can be reused from any
|
|
4
|
+
* client-core consumer.
|
|
5
|
+
*
|
|
6
|
+
* Coverage:
|
|
7
|
+
* - returns undefined for empty / whitespace-only name without
|
|
8
|
+
* calling the API
|
|
9
|
+
* - case-insensitive match on `source_entity`
|
|
10
|
+
* - searches `extracted` first, then `character`, and stops on the
|
|
11
|
+
* first match
|
|
12
|
+
* - returns undefined when no category contains the entity
|
|
13
|
+
* - swallows per-category listImages failures and falls through to
|
|
14
|
+
* the next category
|
|
15
|
+
* - honours a custom `categories` ordering
|
|
16
|
+
* - caches catalogues per (videoId, category) — a hit avoids listImages
|
|
17
|
+
* - `forceRefresh: true` bypasses the cache
|
|
18
|
+
* - per-category failures are NOT cached (so a retry can succeed)
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { ApiClient, ImageRecord } from '../../api';
|
|
22
|
+
import {
|
|
23
|
+
clearEntitySearchCache,
|
|
24
|
+
findEntityImageBySeedName,
|
|
25
|
+
} from '../../reactor/entitySearch';
|
|
26
|
+
|
|
27
|
+
type AnyMock = jest.Mock;
|
|
28
|
+
|
|
29
|
+
function makeImage(overrides: Partial<ImageRecord> = {}): ImageRecord {
|
|
30
|
+
return {
|
|
31
|
+
image_id: 'img-leo',
|
|
32
|
+
user_id: 'u1',
|
|
33
|
+
s3_key: 'extracted/leo.jpg',
|
|
34
|
+
s3_bucket: 'b',
|
|
35
|
+
category: 'extracted',
|
|
36
|
+
content_type: 'image/jpeg',
|
|
37
|
+
image_url: 'https://example.com/leo.jpg',
|
|
38
|
+
source_entity: 'Leo',
|
|
39
|
+
created_at: '2026-01-01',
|
|
40
|
+
updated_at: '2026-01-01',
|
|
41
|
+
...overrides,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function makeApiClient(listImages: AnyMock): ApiClient {
|
|
46
|
+
// Structural subset — findEntityImageBySeedName only touches listImages.
|
|
47
|
+
return { listImages } as unknown as ApiClient;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe('findEntityImageBySeedName', () => {
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
clearEntitySearchCache();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('returns undefined for an empty entity name without calling the API', async () => {
|
|
56
|
+
const listImages = jest.fn();
|
|
57
|
+
const out = await findEntityImageBySeedName(
|
|
58
|
+
makeApiClient(listImages),
|
|
59
|
+
'v1',
|
|
60
|
+
' ',
|
|
61
|
+
);
|
|
62
|
+
expect(out).toBeUndefined();
|
|
63
|
+
expect(listImages).not.toHaveBeenCalled();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('matches entity names case-insensitively after trim', async () => {
|
|
67
|
+
const leo = makeImage({ source_entity: 'Leo' });
|
|
68
|
+
const listImages = jest.fn().mockResolvedValue({ images: [leo] });
|
|
69
|
+
const out = await findEntityImageBySeedName(
|
|
70
|
+
makeApiClient(listImages),
|
|
71
|
+
'v1',
|
|
72
|
+
' leo ',
|
|
73
|
+
);
|
|
74
|
+
expect(out).toBe(leo);
|
|
75
|
+
expect(listImages).toHaveBeenCalledTimes(1);
|
|
76
|
+
expect(listImages).toHaveBeenCalledWith(
|
|
77
|
+
expect.objectContaining({ videoId: 'v1', category: 'extracted' }),
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('searches extracted first, then character; stops on first match', async () => {
|
|
82
|
+
const sarah = makeImage({
|
|
83
|
+
image_id: 'img-sarah',
|
|
84
|
+
source_entity: 'Sarah',
|
|
85
|
+
category: 'character',
|
|
86
|
+
});
|
|
87
|
+
const listImages = jest
|
|
88
|
+
.fn()
|
|
89
|
+
.mockResolvedValueOnce({ images: [] }) // extracted miss
|
|
90
|
+
.mockResolvedValueOnce({ images: [sarah] }); // character hit
|
|
91
|
+
|
|
92
|
+
const out = await findEntityImageBySeedName(
|
|
93
|
+
makeApiClient(listImages),
|
|
94
|
+
'v1',
|
|
95
|
+
'Sarah',
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
expect(out).toBe(sarah);
|
|
99
|
+
const categoriesProbed = listImages.mock.calls.map(c => c[0].category);
|
|
100
|
+
expect(categoriesProbed).toEqual(['extracted', 'character']);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('returns undefined when no category contains the entity', async () => {
|
|
104
|
+
const listImages = jest.fn().mockResolvedValue({ images: [] });
|
|
105
|
+
const out = await findEntityImageBySeedName(
|
|
106
|
+
makeApiClient(listImages),
|
|
107
|
+
'v1',
|
|
108
|
+
'Ghost',
|
|
109
|
+
);
|
|
110
|
+
expect(out).toBeUndefined();
|
|
111
|
+
expect(listImages).toHaveBeenCalledTimes(2); // both default categories probed
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('swallows per-category failures and continues searching', async () => {
|
|
115
|
+
const leo = makeImage({ source_entity: 'Leo', category: 'character' });
|
|
116
|
+
const listImages = jest
|
|
117
|
+
.fn()
|
|
118
|
+
.mockRejectedValueOnce(new Error('extracted endpoint down'))
|
|
119
|
+
.mockResolvedValueOnce({ images: [leo] });
|
|
120
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
121
|
+
|
|
122
|
+
const out = await findEntityImageBySeedName(
|
|
123
|
+
makeApiClient(listImages),
|
|
124
|
+
'v1',
|
|
125
|
+
'Leo',
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
expect(out).toBe(leo);
|
|
129
|
+
expect(listImages).toHaveBeenCalledTimes(2);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('honours a custom categories ordering', async () => {
|
|
133
|
+
const sarah = makeImage({
|
|
134
|
+
image_id: 'img-sarah',
|
|
135
|
+
source_entity: 'Sarah',
|
|
136
|
+
category: 'character',
|
|
137
|
+
});
|
|
138
|
+
const listImages = jest.fn().mockResolvedValue({ images: [sarah] });
|
|
139
|
+
|
|
140
|
+
const out = await findEntityImageBySeedName(
|
|
141
|
+
makeApiClient(listImages),
|
|
142
|
+
'v1',
|
|
143
|
+
'Sarah',
|
|
144
|
+
{ categories: ['character'] },
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
expect(out).toBe(sarah);
|
|
148
|
+
expect(listImages).toHaveBeenCalledTimes(1);
|
|
149
|
+
expect(listImages.mock.calls[0][0].category).toBe('character');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe('caching', () => {
|
|
153
|
+
it('caches the catalogue per (videoId, category) — a hit avoids listImages', async () => {
|
|
154
|
+
const leo = makeImage({ source_entity: 'Leo' });
|
|
155
|
+
const listImages = jest.fn().mockResolvedValue({ images: [leo] });
|
|
156
|
+
const apiClient = makeApiClient(listImages);
|
|
157
|
+
|
|
158
|
+
// First call populates the cache.
|
|
159
|
+
const first = await findEntityImageBySeedName(apiClient, 'v1', 'Leo');
|
|
160
|
+
// Second call against the same (videoId, category) should hit cache.
|
|
161
|
+
const second = await findEntityImageBySeedName(apiClient, 'v1', 'Leo');
|
|
162
|
+
// Even a DIFFERENT name in the same video should hit the cached
|
|
163
|
+
// extracted catalogue (then fall through to character on miss).
|
|
164
|
+
const third = await findEntityImageBySeedName(apiClient, 'v1', 'Sarah');
|
|
165
|
+
|
|
166
|
+
expect(first).toBe(leo);
|
|
167
|
+
expect(second).toBe(leo);
|
|
168
|
+
expect(third).toBeUndefined();
|
|
169
|
+
// 1 call for extracted (cached), 1 for character (probed because
|
|
170
|
+
// Sarah missed in extracted). NOT 4 calls (would be the unknown
|
|
171
|
+
// pattern without caching).
|
|
172
|
+
expect(listImages).toHaveBeenCalledTimes(2);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('isolates cache entries per videoId', async () => {
|
|
176
|
+
const leo = makeImage({ source_entity: 'Leo' });
|
|
177
|
+
const listImages = jest.fn().mockResolvedValue({ images: [leo] });
|
|
178
|
+
const apiClient = makeApiClient(listImages);
|
|
179
|
+
|
|
180
|
+
await findEntityImageBySeedName(apiClient, 'v1', 'Leo');
|
|
181
|
+
await findEntityImageBySeedName(apiClient, 'v2', 'Leo');
|
|
182
|
+
|
|
183
|
+
// Each video gets its own catalogue fetched.
|
|
184
|
+
expect(listImages).toHaveBeenCalledTimes(2);
|
|
185
|
+
const videoIds = listImages.mock.calls.map(c => c[0].videoId);
|
|
186
|
+
expect(videoIds.sort()).toEqual(['v1', 'v2']);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('forceRefresh: true bypasses the cache and re-fetches', async () => {
|
|
190
|
+
const leo = makeImage({ source_entity: 'Leo' });
|
|
191
|
+
const listImages = jest.fn().mockResolvedValue({ images: [leo] });
|
|
192
|
+
const apiClient = makeApiClient(listImages);
|
|
193
|
+
|
|
194
|
+
await findEntityImageBySeedName(apiClient, 'v1', 'Leo'); // populate
|
|
195
|
+
await findEntityImageBySeedName(apiClient, 'v1', 'Leo', {
|
|
196
|
+
forceRefresh: true,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// 1 for the initial fetch, 1 for the forced refresh.
|
|
200
|
+
expect(listImages).toHaveBeenCalledTimes(2);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('does NOT cache per-category failures so a retry can succeed', async () => {
|
|
204
|
+
const leo = makeImage({ source_entity: 'Leo' });
|
|
205
|
+
const listImages = jest
|
|
206
|
+
.fn()
|
|
207
|
+
.mockRejectedValueOnce(new Error('extracted endpoint down'))
|
|
208
|
+
.mockResolvedValueOnce({ images: [] }) // character — empty, miss
|
|
209
|
+
.mockResolvedValueOnce({ images: [leo] }); // retry extracted — hit
|
|
210
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
211
|
+
const apiClient = makeApiClient(listImages);
|
|
212
|
+
|
|
213
|
+
const first = await findEntityImageBySeedName(apiClient, 'v1', 'Leo');
|
|
214
|
+
const second = await findEntityImageBySeedName(apiClient, 'v1', 'Leo');
|
|
215
|
+
|
|
216
|
+
expect(first).toBeUndefined();
|
|
217
|
+
expect(second).toBe(leo);
|
|
218
|
+
// First call: 1 failure + 1 success (character empty)
|
|
219
|
+
// Second call: 1 retry on extracted (failure NOT cached) — character cached.
|
|
220
|
+
expect(listImages).toHaveBeenCalledTimes(3);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
});
|
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for dispatchHeliosCommand — the bridge between parsed marker
|
|
3
|
+
* commands and `reactor.sendCommand`. Lifted from
|
|
4
|
+
* `web/__tests__/app/interactive/chat/heliosToolHandler.test.tsx`.
|
|
5
|
+
*
|
|
6
|
+
* Coverage:
|
|
7
|
+
* - SET_SCENE happy path: upsamples then set_prompt + start at chunk 0.
|
|
8
|
+
* - SET_SCENE mid-stream: schedule_prompt at currentChunk + 2.
|
|
9
|
+
* - SET_SCENE upsample failure: falls back to raw prompt.
|
|
10
|
+
* - SET_SCENE chunk 0 + hasStarted=true: suppresses re-emitting start.
|
|
11
|
+
* - SWAP_SEED happy path: lookup → upload → set_image with blend +
|
|
12
|
+
* onSeedSwapped fires with the resolved ImageRecord.
|
|
13
|
+
* - SWAP_SEED case-insensitive match.
|
|
14
|
+
* - SWAP_SEED no match: warn + onUserWarning + no sendCommand.
|
|
15
|
+
* - SWAP_SEED no videoId: warn + onUserWarning + no listImages.
|
|
16
|
+
* - PAUSE / RESUME / RESET → correct sendCommand names.
|
|
17
|
+
* - sendCommand failures are swallowed so the chat keeps working.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { ApiClient, ImageRecord } from '../../api';
|
|
21
|
+
import { clearEntitySearchCache } from '../../reactor/entitySearch';
|
|
22
|
+
import { dispatchHeliosCommand } from '../../reactor/heliosCommandDispatcher';
|
|
23
|
+
import type { HeliosCommand } from '../../reactor/heliosTools';
|
|
24
|
+
|
|
25
|
+
type AnyMock = jest.Mock;
|
|
26
|
+
|
|
27
|
+
function makeReactor() {
|
|
28
|
+
return {
|
|
29
|
+
sendCommand: jest.fn().mockResolvedValue(undefined) as AnyMock,
|
|
30
|
+
uploadFile: jest.fn().mockResolvedValue({
|
|
31
|
+
uploadId: 'u1',
|
|
32
|
+
name: 'seed.jpg',
|
|
33
|
+
mimeType: 'image/jpeg',
|
|
34
|
+
size: 1,
|
|
35
|
+
}) as AnyMock,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function makeApiClient(overrides: Partial<Record<string, AnyMock>> = {}) {
|
|
40
|
+
return {
|
|
41
|
+
upsampleReactorPrompt: jest.fn().mockResolvedValue({
|
|
42
|
+
traceId: 't',
|
|
43
|
+
upsampledPrompt: 'UPSAMPLED',
|
|
44
|
+
}),
|
|
45
|
+
listImages: jest.fn().mockResolvedValue({ images: [] }),
|
|
46
|
+
getSeedImageBlob: jest
|
|
47
|
+
.fn()
|
|
48
|
+
.mockResolvedValue(new Blob(['x'], { type: 'image/jpeg' })),
|
|
49
|
+
...overrides,
|
|
50
|
+
} as unknown as ApiClient & Record<string, AnyMock>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function makeImage(overrides: Partial<ImageRecord> = {}): ImageRecord {
|
|
54
|
+
return {
|
|
55
|
+
image_id: 'img-leo',
|
|
56
|
+
user_id: 'u1',
|
|
57
|
+
s3_key: 'extracted/leo.jpg',
|
|
58
|
+
s3_bucket: 'b',
|
|
59
|
+
category: 'extracted',
|
|
60
|
+
content_type: 'image/jpeg',
|
|
61
|
+
image_url: 'https://example.com/leo.jpg',
|
|
62
|
+
source_entity: 'Leo',
|
|
63
|
+
created_at: '2026-01-01',
|
|
64
|
+
updated_at: '2026-01-01',
|
|
65
|
+
...overrides,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
describe('dispatchHeliosCommand', () => {
|
|
70
|
+
beforeEach(() => {
|
|
71
|
+
// SWAP_SEED's entity lookup uses a process-local cache (#119).
|
|
72
|
+
// Without this, listImages call-count assertions are order-dependent
|
|
73
|
+
// because earlier tests warm the cache for `(videoId, category)`.
|
|
74
|
+
clearEntitySearchCache();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('set_scene', () => {
|
|
78
|
+
it('upsamples then set_prompt + start at chunk 0', async () => {
|
|
79
|
+
const reactor = makeReactor();
|
|
80
|
+
const apiClient = makeApiClient();
|
|
81
|
+
const cmd: HeliosCommand = { type: 'set_scene', prompt: 'rainy night' };
|
|
82
|
+
|
|
83
|
+
await dispatchHeliosCommand(cmd, {
|
|
84
|
+
apiClient,
|
|
85
|
+
reactor,
|
|
86
|
+
videoId: 'v1',
|
|
87
|
+
currentChunk: 0,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
expect(
|
|
91
|
+
(apiClient as unknown as Record<string, AnyMock>).upsampleReactorPrompt,
|
|
92
|
+
).toHaveBeenCalledWith(
|
|
93
|
+
expect.objectContaining({ prompt: 'rainy night' }),
|
|
94
|
+
expect.anything(),
|
|
95
|
+
);
|
|
96
|
+
const cmdNames = reactor.sendCommand.mock.calls.map(c => c[0]);
|
|
97
|
+
expect(cmdNames).toEqual(['set_prompt', 'start']);
|
|
98
|
+
const setPrompt = reactor.sendCommand.mock.calls.find(
|
|
99
|
+
c => c[0] === 'set_prompt',
|
|
100
|
+
);
|
|
101
|
+
expect(setPrompt?.[1]).toEqual({ prompt: 'UPSAMPLED' });
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('uses schedule_prompt at chunk + 2 mid-stream', async () => {
|
|
105
|
+
const reactor = makeReactor();
|
|
106
|
+
const apiClient = makeApiClient();
|
|
107
|
+
|
|
108
|
+
await dispatchHeliosCommand(
|
|
109
|
+
{ type: 'set_scene', prompt: 'beach' },
|
|
110
|
+
{
|
|
111
|
+
apiClient,
|
|
112
|
+
reactor,
|
|
113
|
+
videoId: 'v1',
|
|
114
|
+
currentChunk: 14,
|
|
115
|
+
},
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const cmdNames = reactor.sendCommand.mock.calls.map(c => c[0]);
|
|
119
|
+
expect(cmdNames).toEqual(['schedule_prompt']);
|
|
120
|
+
expect(reactor.sendCommand.mock.calls[0][1]).toEqual({
|
|
121
|
+
prompt: 'UPSAMPLED',
|
|
122
|
+
chunk: 16,
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('falls back to the raw prompt when upsample fails', async () => {
|
|
127
|
+
const reactor = makeReactor();
|
|
128
|
+
const apiClient = makeApiClient({
|
|
129
|
+
upsampleReactorPrompt: jest
|
|
130
|
+
.fn()
|
|
131
|
+
.mockRejectedValue(new Error('LLM down')),
|
|
132
|
+
});
|
|
133
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
134
|
+
|
|
135
|
+
await dispatchHeliosCommand(
|
|
136
|
+
{ type: 'set_scene', prompt: 'raw fallback' },
|
|
137
|
+
{
|
|
138
|
+
apiClient,
|
|
139
|
+
reactor,
|
|
140
|
+
videoId: 'v1',
|
|
141
|
+
currentChunk: 0,
|
|
142
|
+
},
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const setPrompt = reactor.sendCommand.mock.calls.find(
|
|
146
|
+
c => c[0] === 'set_prompt',
|
|
147
|
+
);
|
|
148
|
+
expect(setPrompt?.[1]).toEqual({ prompt: 'raw fallback' });
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// Round 11 regression — the `hasStarted` guard prevents re-emitting
|
|
152
|
+
// `start` while the model is already running.
|
|
153
|
+
it('suppresses `start` at chunk 0 when hasStarted is already true', async () => {
|
|
154
|
+
const reactor = makeReactor();
|
|
155
|
+
const apiClient = makeApiClient();
|
|
156
|
+
|
|
157
|
+
await dispatchHeliosCommand(
|
|
158
|
+
{ type: 'set_scene', prompt: 'refreshed scene' },
|
|
159
|
+
{
|
|
160
|
+
apiClient,
|
|
161
|
+
reactor,
|
|
162
|
+
videoId: 'v1',
|
|
163
|
+
currentChunk: 0,
|
|
164
|
+
hasStarted: true,
|
|
165
|
+
},
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const cmdNames = reactor.sendCommand.mock.calls.map(c => c[0]);
|
|
169
|
+
expect(cmdNames).toContain('set_prompt');
|
|
170
|
+
expect(cmdNames).not.toContain('start');
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('swap_seed', () => {
|
|
175
|
+
it('looks up the entity, uploads, set_image with blend transition, fires onSeedSwapped', async () => {
|
|
176
|
+
const reactor = makeReactor();
|
|
177
|
+
const leo = makeImage({ source_entity: 'Leo' });
|
|
178
|
+
const apiClient = makeApiClient({
|
|
179
|
+
listImages: jest.fn().mockResolvedValue({ images: [leo] }),
|
|
180
|
+
});
|
|
181
|
+
const onSeedSwapped = jest.fn();
|
|
182
|
+
|
|
183
|
+
await dispatchHeliosCommand(
|
|
184
|
+
{ type: 'swap_seed', entityName: 'Leo' },
|
|
185
|
+
{
|
|
186
|
+
apiClient,
|
|
187
|
+
reactor,
|
|
188
|
+
videoId: 'v1',
|
|
189
|
+
currentChunk: 5,
|
|
190
|
+
onSeedSwapped,
|
|
191
|
+
},
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
expect(
|
|
195
|
+
(apiClient as unknown as Record<string, AnyMock>).listImages,
|
|
196
|
+
).toHaveBeenCalledWith(
|
|
197
|
+
expect.objectContaining({ videoId: 'v1' }),
|
|
198
|
+
);
|
|
199
|
+
expect(
|
|
200
|
+
(apiClient as unknown as Record<string, AnyMock>).getSeedImageBlob,
|
|
201
|
+
).toHaveBeenCalledWith('img-leo', expect.anything());
|
|
202
|
+
expect(reactor.uploadFile).toHaveBeenCalled();
|
|
203
|
+
const setImage = reactor.sendCommand.mock.calls.find(
|
|
204
|
+
c => c[0] === 'set_image',
|
|
205
|
+
);
|
|
206
|
+
expect(setImage?.[1]).toEqual(
|
|
207
|
+
expect.objectContaining({ transition: 'blend' }),
|
|
208
|
+
);
|
|
209
|
+
expect(onSeedSwapped).toHaveBeenCalledTimes(1);
|
|
210
|
+
expect(onSeedSwapped).toHaveBeenCalledWith(leo);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('derives the upload filename extension from the blob MIME (PNG)', async () => {
|
|
214
|
+
const reactor = makeReactor();
|
|
215
|
+
const leo = makeImage({
|
|
216
|
+
source_entity: 'Leo',
|
|
217
|
+
category: 'character',
|
|
218
|
+
content_type: 'image/png',
|
|
219
|
+
});
|
|
220
|
+
const apiClient = makeApiClient({
|
|
221
|
+
listImages: jest.fn().mockResolvedValue({ images: [leo] }),
|
|
222
|
+
getSeedImageBlob: jest
|
|
223
|
+
.fn()
|
|
224
|
+
.mockResolvedValue(new Blob(['x'], { type: 'image/png' })),
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
await dispatchHeliosCommand(
|
|
228
|
+
{ type: 'swap_seed', entityName: 'Leo' },
|
|
229
|
+
{ apiClient, reactor, videoId: 'v1' },
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const uploaded = reactor.uploadFile.mock.calls[0][0] as File;
|
|
233
|
+
expect(uploaded.name).toBe('swap-seed.png');
|
|
234
|
+
expect(uploaded.type).toBe('image/png');
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('matches entity names case-insensitively', async () => {
|
|
238
|
+
const reactor = makeReactor();
|
|
239
|
+
const apiClient = makeApiClient({
|
|
240
|
+
listImages: jest
|
|
241
|
+
.fn()
|
|
242
|
+
.mockResolvedValue({ images: [makeImage({ source_entity: 'Leo' })] }),
|
|
243
|
+
});
|
|
244
|
+
await dispatchHeliosCommand(
|
|
245
|
+
{ type: 'swap_seed', entityName: 'leo' },
|
|
246
|
+
{ apiClient, reactor, videoId: 'v1' },
|
|
247
|
+
);
|
|
248
|
+
expect(
|
|
249
|
+
(apiClient as unknown as Record<string, AnyMock>).getSeedImageBlob,
|
|
250
|
+
).toHaveBeenCalled();
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('warns + onUserWarning + skips sendCommand when no entity matches', async () => {
|
|
254
|
+
const reactor = makeReactor();
|
|
255
|
+
const apiClient = makeApiClient({
|
|
256
|
+
listImages: jest.fn().mockResolvedValue({ images: [] }),
|
|
257
|
+
});
|
|
258
|
+
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
259
|
+
const onUserWarning = jest.fn();
|
|
260
|
+
const onSeedSwapped = jest.fn();
|
|
261
|
+
|
|
262
|
+
await dispatchHeliosCommand(
|
|
263
|
+
{ type: 'swap_seed', entityName: 'NotInVideo' },
|
|
264
|
+
{
|
|
265
|
+
apiClient,
|
|
266
|
+
reactor,
|
|
267
|
+
videoId: 'v1',
|
|
268
|
+
onUserWarning,
|
|
269
|
+
onSeedSwapped,
|
|
270
|
+
},
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
expect(reactor.sendCommand).not.toHaveBeenCalled();
|
|
274
|
+
expect(warn).toHaveBeenCalled();
|
|
275
|
+
expect(onUserWarning).toHaveBeenCalledTimes(1);
|
|
276
|
+
expect(onUserWarning.mock.calls[0][0]).toContain('NotInVideo');
|
|
277
|
+
expect(onSeedSwapped).not.toHaveBeenCalled();
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('warns + onUserWarning + skips when videoId is null', async () => {
|
|
281
|
+
const reactor = makeReactor();
|
|
282
|
+
const apiClient = makeApiClient();
|
|
283
|
+
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
284
|
+
const onUserWarning = jest.fn();
|
|
285
|
+
|
|
286
|
+
await dispatchHeliosCommand(
|
|
287
|
+
{ type: 'swap_seed', entityName: 'Leo' },
|
|
288
|
+
{
|
|
289
|
+
apiClient,
|
|
290
|
+
reactor,
|
|
291
|
+
videoId: null,
|
|
292
|
+
onUserWarning,
|
|
293
|
+
},
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
expect(reactor.sendCommand).not.toHaveBeenCalled();
|
|
297
|
+
expect(
|
|
298
|
+
(apiClient as unknown as Record<string, AnyMock>).listImages,
|
|
299
|
+
).not.toHaveBeenCalled();
|
|
300
|
+
expect(warn).toHaveBeenCalled();
|
|
301
|
+
expect(onUserWarning).toHaveBeenCalledTimes(1);
|
|
302
|
+
expect(onUserWarning.mock.calls[0][0].toLowerCase()).toContain(
|
|
303
|
+
'no video',
|
|
304
|
+
);
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
describe('pause/resume/reset', () => {
|
|
309
|
+
it.each([
|
|
310
|
+
['pause_world', 'pause'],
|
|
311
|
+
['resume_world', 'resume'],
|
|
312
|
+
['reset_world', 'reset'],
|
|
313
|
+
])('%s → reactor.sendCommand("%s")', async (cmdType, expectedCmd) => {
|
|
314
|
+
const reactor = makeReactor();
|
|
315
|
+
const apiClient = makeApiClient();
|
|
316
|
+
await dispatchHeliosCommand(
|
|
317
|
+
{ type: cmdType } as HeliosCommand,
|
|
318
|
+
{ apiClient, reactor, videoId: null },
|
|
319
|
+
);
|
|
320
|
+
const names = reactor.sendCommand.mock.calls.map(c => c[0]);
|
|
321
|
+
expect(names).toEqual([expectedCmd]);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
describe('abort signal', () => {
|
|
326
|
+
it('returns immediately without calling apiClient when the signal is already aborted', async () => {
|
|
327
|
+
const reactor = makeReactor();
|
|
328
|
+
const apiClient = makeApiClient();
|
|
329
|
+
const controller = new AbortController();
|
|
330
|
+
controller.abort();
|
|
331
|
+
|
|
332
|
+
await dispatchHeliosCommand(
|
|
333
|
+
{ type: 'set_scene', prompt: 'rainy night' },
|
|
334
|
+
{
|
|
335
|
+
apiClient,
|
|
336
|
+
reactor,
|
|
337
|
+
videoId: 'v1',
|
|
338
|
+
signal: controller.signal,
|
|
339
|
+
},
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
expect(
|
|
343
|
+
(apiClient as unknown as Record<string, AnyMock>).upsampleReactorPrompt,
|
|
344
|
+
).not.toHaveBeenCalled();
|
|
345
|
+
expect(reactor.sendCommand).not.toHaveBeenCalled();
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it('threads the signal into upsampleReactorPrompt for SET_SCENE', async () => {
|
|
349
|
+
const reactor = makeReactor();
|
|
350
|
+
const apiClient = makeApiClient();
|
|
351
|
+
const controller = new AbortController();
|
|
352
|
+
|
|
353
|
+
await dispatchHeliosCommand(
|
|
354
|
+
{ type: 'set_scene', prompt: 'rainy night' },
|
|
355
|
+
{
|
|
356
|
+
apiClient,
|
|
357
|
+
reactor,
|
|
358
|
+
videoId: 'v1',
|
|
359
|
+
currentChunk: 0,
|
|
360
|
+
signal: controller.signal,
|
|
361
|
+
},
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
const call = (apiClient as unknown as Record<string, AnyMock>)
|
|
365
|
+
.upsampleReactorPrompt.mock.calls[0];
|
|
366
|
+
expect(call?.[1]).toEqual(
|
|
367
|
+
expect.objectContaining({ signal: controller.signal }),
|
|
368
|
+
);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it('threads the signal into getSeedImageBlob for SWAP_SEED', async () => {
|
|
372
|
+
const reactor = makeReactor();
|
|
373
|
+
const leo = makeImage({ source_entity: 'Leo' });
|
|
374
|
+
const apiClient = makeApiClient({
|
|
375
|
+
listImages: jest.fn().mockResolvedValue({ images: [leo] }),
|
|
376
|
+
});
|
|
377
|
+
const controller = new AbortController();
|
|
378
|
+
|
|
379
|
+
await dispatchHeliosCommand(
|
|
380
|
+
{ type: 'swap_seed', entityName: 'Leo' },
|
|
381
|
+
{
|
|
382
|
+
apiClient,
|
|
383
|
+
reactor,
|
|
384
|
+
videoId: 'v1',
|
|
385
|
+
signal: controller.signal,
|
|
386
|
+
},
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
const call = (apiClient as unknown as Record<string, AnyMock>)
|
|
390
|
+
.getSeedImageBlob.mock.calls[0];
|
|
391
|
+
expect(call?.[1]).toEqual(
|
|
392
|
+
expect.objectContaining({ signal: controller.signal }),
|
|
393
|
+
);
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
it('stops cleanly when the upsample rejects with AbortError', async () => {
|
|
397
|
+
const reactor = makeReactor();
|
|
398
|
+
const abortErr = new Error('aborted');
|
|
399
|
+
abortErr.name = 'AbortError';
|
|
400
|
+
const apiClient = makeApiClient({
|
|
401
|
+
upsampleReactorPrompt: jest.fn().mockRejectedValue(abortErr),
|
|
402
|
+
});
|
|
403
|
+
const controller = new AbortController();
|
|
404
|
+
controller.abort();
|
|
405
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
406
|
+
|
|
407
|
+
await dispatchHeliosCommand(
|
|
408
|
+
{ type: 'set_scene', prompt: 'rainy night' },
|
|
409
|
+
{
|
|
410
|
+
apiClient,
|
|
411
|
+
reactor,
|
|
412
|
+
videoId: 'v1',
|
|
413
|
+
currentChunk: 0,
|
|
414
|
+
signal: controller.signal,
|
|
415
|
+
},
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
// No sendCommand (no fallback to raw prompt) — abort means stop.
|
|
419
|
+
expect(reactor.sendCommand).not.toHaveBeenCalled();
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
describe('error handling', () => {
|
|
424
|
+
it('swallows sendCommand failures so the chat keeps working', async () => {
|
|
425
|
+
const reactor = makeReactor();
|
|
426
|
+
reactor.sendCommand.mockRejectedValue(new Error('reactor down'));
|
|
427
|
+
const apiClient = makeApiClient();
|
|
428
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
429
|
+
|
|
430
|
+
await expect(
|
|
431
|
+
dispatchHeliosCommand(
|
|
432
|
+
{ type: 'pause_world' },
|
|
433
|
+
{ apiClient, reactor, videoId: null },
|
|
434
|
+
),
|
|
435
|
+
).resolves.toBeUndefined();
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
});
|