@escapeaihq/client-core 0.6.1 → 0.7.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 +27 -2
- 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/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/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
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the three Reactor / Helios API client methods added to
|
|
3
|
+
* `ApiClient`:
|
|
4
|
+
* - getReactorToken() → POST /api/reactor/token (same-origin)
|
|
5
|
+
* - getSeedImageBlob(imageId) → GET /api/reactor/seed-image (same-origin)
|
|
6
|
+
* - upsampleReactorPrompt(req) → POST /api/v1/reactor/upsample (FastAPI,
|
|
7
|
+
* uses Authorization header via this.request())
|
|
8
|
+
*
|
|
9
|
+
* Mocking pattern mirrors `api-pollers.test.ts` — overwrite `globalThis.fetch`
|
|
10
|
+
* with a jest mock, snapshot the URL + init that the client passes through.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { ApiClient } from '../../api';
|
|
14
|
+
import type {
|
|
15
|
+
ReactorTokenResponse,
|
|
16
|
+
UpsampleReactorPromptRequest,
|
|
17
|
+
UpsampleReactorPromptResponse,
|
|
18
|
+
} from '../../api';
|
|
19
|
+
|
|
20
|
+
const TEST_BASE_URL = 'http://test.local';
|
|
21
|
+
const TEST_CHAT_BASE_URL = 'http://test.local/chat';
|
|
22
|
+
|
|
23
|
+
const makeClient = () =>
|
|
24
|
+
new ApiClient(TEST_BASE_URL, 'test-token', TEST_CHAT_BASE_URL);
|
|
25
|
+
|
|
26
|
+
// Snapshot the original fetch (likely undefined under jest-jsdom) so we
|
|
27
|
+
// can restore it after each test — same pattern api-pollers.test.ts uses.
|
|
28
|
+
const originalFetch = (globalThis as { fetch?: typeof fetch }).fetch;
|
|
29
|
+
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
if (originalFetch === undefined) {
|
|
32
|
+
delete (globalThis as { fetch?: typeof fetch }).fetch;
|
|
33
|
+
} else {
|
|
34
|
+
(globalThis as { fetch?: typeof fetch }).fetch = originalFetch;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('ApiClient.getReactorToken', () => {
|
|
39
|
+
it('POSTs to the SAME-ORIGIN /api/reactor/token with the bearer token attached', async () => {
|
|
40
|
+
const tokenBody: ReactorTokenResponse = {
|
|
41
|
+
jwt: 'jwt-abc',
|
|
42
|
+
expiresAt: 1700000000000,
|
|
43
|
+
};
|
|
44
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
45
|
+
ok: true,
|
|
46
|
+
status: 200,
|
|
47
|
+
json: async () => tokenBody,
|
|
48
|
+
});
|
|
49
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
50
|
+
|
|
51
|
+
const client = makeClient();
|
|
52
|
+
const result = await client.getReactorToken();
|
|
53
|
+
|
|
54
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
55
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
56
|
+
// Relative path — NOT prefixed with baseURL. The Next.js route handler
|
|
57
|
+
// lives on the web app origin.
|
|
58
|
+
expect(url).toBe('/api/reactor/token');
|
|
59
|
+
expect(init.method).toBe('POST');
|
|
60
|
+
// Defense-in-depth Bearer header — Descope middleware also enforces
|
|
61
|
+
// auth, but the route checks this explicitly so middleware
|
|
62
|
+
// misconfiguration can't accidentally open the broker. Regression
|
|
63
|
+
// test for PR #191 R5 BLOCKER.
|
|
64
|
+
const headers = (init.headers as Record<string, string>) ?? {};
|
|
65
|
+
expect(headers['Authorization']).toBe('Bearer test-token');
|
|
66
|
+
expect(result).toEqual(tokenBody);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('omits Authorization when the ApiClient has no session token', async () => {
|
|
70
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
71
|
+
ok: true,
|
|
72
|
+
status: 200,
|
|
73
|
+
json: async () => ({ jwt: 'x' }),
|
|
74
|
+
});
|
|
75
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
76
|
+
|
|
77
|
+
const client = new ApiClient(TEST_BASE_URL, undefined, TEST_CHAT_BASE_URL);
|
|
78
|
+
await client.getReactorToken();
|
|
79
|
+
|
|
80
|
+
const headers = (fetchMock.mock.calls[0][1].headers as Record<string, string>) ?? {};
|
|
81
|
+
expect(headers['Authorization']).toBeUndefined();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('forwards AbortSignal to fetch', async () => {
|
|
85
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
86
|
+
ok: true,
|
|
87
|
+
status: 200,
|
|
88
|
+
json: async () => ({ jwt: 'x' }),
|
|
89
|
+
});
|
|
90
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
91
|
+
const controller = new AbortController();
|
|
92
|
+
|
|
93
|
+
await makeClient().getReactorToken({ signal: controller.signal });
|
|
94
|
+
expect(fetchMock.mock.calls[0][1].signal).toBe(controller.signal);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('throws with the response detail when the broker returns a non-2xx', async () => {
|
|
98
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
99
|
+
ok: false,
|
|
100
|
+
status: 502,
|
|
101
|
+
json: async () => ({ detail: 'reactor /tokens 502' }),
|
|
102
|
+
});
|
|
103
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
104
|
+
|
|
105
|
+
await expect(makeClient().getReactorToken()).rejects.toThrow(/reactor \/tokens 502/);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe('ApiClient.getSeedImageBlob', () => {
|
|
110
|
+
it('GETs the SAME-ORIGIN seed-image proxy with imageId in the query string and forwards the bearer token', async () => {
|
|
111
|
+
const blob = new Blob(['fake-bytes'], { type: 'image/png' });
|
|
112
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
113
|
+
ok: true,
|
|
114
|
+
status: 200,
|
|
115
|
+
blob: async () => blob,
|
|
116
|
+
});
|
|
117
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
118
|
+
|
|
119
|
+
const result = await makeClient().getSeedImageBlob('img-abc/123');
|
|
120
|
+
|
|
121
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
122
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
123
|
+
// imageId URL-encoded — the `/` inside the id becomes %2F.
|
|
124
|
+
expect(url).toBe('/api/reactor/seed-image?imageId=img-abc%2F123');
|
|
125
|
+
expect(init.method).toBe('GET');
|
|
126
|
+
// The Next.js proxy gates on Authorization (it forwards the bearer to
|
|
127
|
+
// FastAPI to resolve the ImageRecord); without this header users would
|
|
128
|
+
// get a 401 on every seed pick. Regression test for PR #191 Codex P1.
|
|
129
|
+
const headers = (init.headers as Record<string, string>) ?? {};
|
|
130
|
+
expect(headers['Authorization']).toBe('Bearer test-token');
|
|
131
|
+
expect(result).toBe(blob);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('omits Authorization when the ApiClient has no session token', async () => {
|
|
135
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
136
|
+
ok: true,
|
|
137
|
+
status: 200,
|
|
138
|
+
blob: async () => new Blob(),
|
|
139
|
+
});
|
|
140
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
141
|
+
|
|
142
|
+
// Pass undefined for the session token — proxy will then 401, which is
|
|
143
|
+
// the right behavior, but the apiClient itself shouldn't send an empty
|
|
144
|
+
// "Bearer undefined" header.
|
|
145
|
+
const client = new ApiClient(TEST_BASE_URL, undefined, TEST_CHAT_BASE_URL);
|
|
146
|
+
await client.getSeedImageBlob('img-1');
|
|
147
|
+
|
|
148
|
+
const headers = (fetchMock.mock.calls[0][1].headers as Record<string, string>) ?? {};
|
|
149
|
+
expect(headers['Authorization']).toBeUndefined();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('forwards AbortSignal to fetch', async () => {
|
|
153
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
154
|
+
ok: true,
|
|
155
|
+
status: 200,
|
|
156
|
+
blob: async () => new Blob(),
|
|
157
|
+
});
|
|
158
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
159
|
+
const controller = new AbortController();
|
|
160
|
+
|
|
161
|
+
await makeClient().getSeedImageBlob('img-1', { signal: controller.signal });
|
|
162
|
+
expect(fetchMock.mock.calls[0][1].signal).toBe(controller.signal);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('throws with the response detail when the proxy returns a non-2xx', async () => {
|
|
166
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
167
|
+
ok: false,
|
|
168
|
+
status: 404,
|
|
169
|
+
json: async () => ({ detail: 'image not found' }),
|
|
170
|
+
});
|
|
171
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
172
|
+
|
|
173
|
+
await expect(makeClient().getSeedImageBlob('img-x')).rejects.toThrow(
|
|
174
|
+
/image not found/,
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe('ApiClient.upsampleReactorPrompt', () => {
|
|
180
|
+
it('POSTs to ${chatBaseURL}/api/v1/reactor/upsample with Authorization header and JSON body', async () => {
|
|
181
|
+
const response: UpsampleReactorPromptResponse = {
|
|
182
|
+
traceId: 'trace-1',
|
|
183
|
+
upsampledPrompt: 'A majestic lion stands regally...',
|
|
184
|
+
};
|
|
185
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
186
|
+
ok: true,
|
|
187
|
+
status: 200,
|
|
188
|
+
json: async () => response,
|
|
189
|
+
});
|
|
190
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
191
|
+
|
|
192
|
+
const request: UpsampleReactorPromptRequest = {
|
|
193
|
+
prompt: 'a lion in a jungle',
|
|
194
|
+
previousPrompt: undefined,
|
|
195
|
+
imageId: 'img-1',
|
|
196
|
+
entityContext: {
|
|
197
|
+
name: 'Leo',
|
|
198
|
+
sceneContext: 'on a rocky outcrop',
|
|
199
|
+
sourceVideoId: 'vid-1',
|
|
200
|
+
sourceTimestamp: 12.5,
|
|
201
|
+
},
|
|
202
|
+
personaContext: {
|
|
203
|
+
name: 'Leo',
|
|
204
|
+
backstory: 'A noble jungle king.',
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const result = await makeClient().upsampleReactorPrompt(request);
|
|
209
|
+
|
|
210
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
211
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
212
|
+
// Goes through this.chatRequest() — the reactor router is mounted on
|
|
213
|
+
// the chat app (api/chat/app.py), NOT the tools app. In split local
|
|
214
|
+
// dev (tools on 8080, chat on 8000) routing through baseURL would
|
|
215
|
+
// 404. Regression test for PR #191 Codex P2.
|
|
216
|
+
expect(url).toBe(`${TEST_CHAT_BASE_URL}/api/v1/reactor/upsample`);
|
|
217
|
+
expect(init.method).toBe('POST');
|
|
218
|
+
expect(JSON.parse(init.body as string)).toEqual(request);
|
|
219
|
+
const headers = init.headers as Record<string, string>;
|
|
220
|
+
expect(headers['Authorization']).toBe('Bearer test-token');
|
|
221
|
+
expect(headers['Content-Type']).toBe('application/json');
|
|
222
|
+
expect(result).toEqual(response);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('forwards AbortSignal to fetch', async () => {
|
|
226
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
227
|
+
ok: true,
|
|
228
|
+
status: 200,
|
|
229
|
+
json: async () => ({ traceId: 't', upsampledPrompt: 'x' }),
|
|
230
|
+
});
|
|
231
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
232
|
+
const controller = new AbortController();
|
|
233
|
+
|
|
234
|
+
await makeClient().upsampleReactorPrompt(
|
|
235
|
+
{ prompt: 'a lion' },
|
|
236
|
+
{ signal: controller.signal },
|
|
237
|
+
);
|
|
238
|
+
expect(fetchMock.mock.calls[0][1].signal).toBe(controller.signal);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('propagates 5xx with the backend detail', async () => {
|
|
242
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
243
|
+
ok: false,
|
|
244
|
+
status: 502,
|
|
245
|
+
json: async () => ({ detail: 'bedrock unavailable' }),
|
|
246
|
+
});
|
|
247
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
248
|
+
|
|
249
|
+
await expect(
|
|
250
|
+
makeClient().upsampleReactorPrompt({ prompt: 'a lion' }),
|
|
251
|
+
).rejects.toThrow(/bedrock unavailable/);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for `aggregateEntityContext` and `aggregatePersonaContext` —
|
|
3
|
+
* the wire-shape → LLM-context aggregators.
|
|
4
|
+
*
|
|
5
|
+
* Coverage:
|
|
6
|
+
* - ImageRecord with `source_entity` → name preserved verbatim
|
|
7
|
+
* - ImageRecord without `source_entity` → fallback to s3_key stem
|
|
8
|
+
* - ImageRecord with no s3_key/source_entity → "unknown entity" fallback
|
|
9
|
+
* - Enrichment with matching {scenes:[{start,end,description}]} → scene description
|
|
10
|
+
* - Enrichment with no matching timestamp → undefined sceneContext
|
|
11
|
+
* - Enrichment with `{summary: ...}` flat shape → summary as fallback
|
|
12
|
+
* - Persona context — truncation at 400 chars
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
aggregateEntityContext,
|
|
17
|
+
aggregatePersonaContext,
|
|
18
|
+
} from '../../reactor/entityContext';
|
|
19
|
+
import type { EnrichmentRecord, ImageRecord, PersonaConfig } from '../../api';
|
|
20
|
+
|
|
21
|
+
const buildImage = (overrides: Partial<ImageRecord> = {}): ImageRecord => ({
|
|
22
|
+
image_id: 'img-1',
|
|
23
|
+
user_id: 'user-1',
|
|
24
|
+
s3_key: 'extracted/leo.png',
|
|
25
|
+
s3_bucket: 'test-bucket',
|
|
26
|
+
category: 'extracted',
|
|
27
|
+
content_type: 'image/png',
|
|
28
|
+
status: 'complete',
|
|
29
|
+
created_at: '2026-05-09T00:00:00Z',
|
|
30
|
+
updated_at: '2026-05-09T00:00:00Z',
|
|
31
|
+
...overrides,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const buildEnrichment = (
|
|
35
|
+
overrides: Partial<EnrichmentRecord> = {},
|
|
36
|
+
): EnrichmentRecord => ({
|
|
37
|
+
enrichmentId: 'enr-1',
|
|
38
|
+
videoId: 'vid-1',
|
|
39
|
+
jobId: 'job-1',
|
|
40
|
+
status: 'SUCCEEDED',
|
|
41
|
+
createdAt: '2026-05-09T00:00:00Z',
|
|
42
|
+
...overrides,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const buildPersona = (overrides: Partial<PersonaConfig> = {}): PersonaConfig => ({
|
|
46
|
+
persona_id: 'persona-1',
|
|
47
|
+
user_id: 'user-1',
|
|
48
|
+
name: 'Leo',
|
|
49
|
+
voice_id: 'v1',
|
|
50
|
+
tts_model_id: 'eleven_v3',
|
|
51
|
+
temperature: 0.7,
|
|
52
|
+
speaking_rate: 1.0,
|
|
53
|
+
audio_format: 'PCM16',
|
|
54
|
+
enable_markups: false,
|
|
55
|
+
chat_model_id: 'claude-sonnet-4-5',
|
|
56
|
+
system_prompt_override: false,
|
|
57
|
+
heartbeat_interval: 30,
|
|
58
|
+
idle_timeout: 300,
|
|
59
|
+
is_default: false,
|
|
60
|
+
status: 'approved',
|
|
61
|
+
created_at: '2026-05-09T00:00:00Z',
|
|
62
|
+
updated_at: '2026-05-09T00:00:00Z',
|
|
63
|
+
...overrides,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('aggregateEntityContext — name resolution', () => {
|
|
67
|
+
it('uses source_entity verbatim when present', () => {
|
|
68
|
+
const ctx = aggregateEntityContext({
|
|
69
|
+
imageRecord: buildImage({ source_entity: 'Leo the Lion' }),
|
|
70
|
+
});
|
|
71
|
+
expect(ctx.name).toBe('Leo the Lion');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('falls back to s3_key stem when source_entity is missing', () => {
|
|
75
|
+
const ctx = aggregateEntityContext({
|
|
76
|
+
imageRecord: buildImage({
|
|
77
|
+
source_entity: null,
|
|
78
|
+
s3_key: 'extracted/2026/wonton-hero.png',
|
|
79
|
+
}),
|
|
80
|
+
});
|
|
81
|
+
expect(ctx.name).toBe('wonton-hero');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('falls back to "unknown entity" when source_entity and s3_key are both empty', () => {
|
|
85
|
+
const ctx = aggregateEntityContext({
|
|
86
|
+
imageRecord: buildImage({ source_entity: null, s3_key: '' }),
|
|
87
|
+
});
|
|
88
|
+
expect(ctx.name).toBe('unknown entity');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('trims whitespace-only source_entity', () => {
|
|
92
|
+
const ctx = aggregateEntityContext({
|
|
93
|
+
imageRecord: buildImage({ source_entity: ' ', s3_key: 'foo/bar.png' }),
|
|
94
|
+
});
|
|
95
|
+
expect(ctx.name).toBe('bar');
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe('aggregateEntityContext — source video metadata', () => {
|
|
100
|
+
it('forwards sourceVideoId and sourceTimestamp when present', () => {
|
|
101
|
+
const ctx = aggregateEntityContext({
|
|
102
|
+
imageRecord: buildImage({
|
|
103
|
+
source_video_id: 'vid-42',
|
|
104
|
+
source_timestamp: 12.5,
|
|
105
|
+
}),
|
|
106
|
+
});
|
|
107
|
+
expect(ctx.sourceVideoId).toBe('vid-42');
|
|
108
|
+
expect(ctx.sourceTimestamp).toBe(12.5);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('omits sourceVideoId / sourceTimestamp when absent', () => {
|
|
112
|
+
const ctx = aggregateEntityContext({
|
|
113
|
+
imageRecord: buildImage({
|
|
114
|
+
source_video_id: null,
|
|
115
|
+
source_timestamp: null,
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
expect(ctx.sourceVideoId).toBeUndefined();
|
|
119
|
+
expect(ctx.sourceTimestamp).toBeUndefined();
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('aggregateEntityContext — scene context from enrichment', () => {
|
|
124
|
+
const imageWithTs = buildImage({
|
|
125
|
+
source_video_id: 'vid-1',
|
|
126
|
+
source_timestamp: 12.5,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('picks up the scene description when timestamp falls inside a scene window', () => {
|
|
130
|
+
const ctx = aggregateEntityContext({
|
|
131
|
+
imageRecord: imageWithTs,
|
|
132
|
+
enrichment: buildEnrichment({
|
|
133
|
+
result: {
|
|
134
|
+
scenes: [
|
|
135
|
+
{ start: 0, end: 10, description: 'opening titles' },
|
|
136
|
+
{ start: 10, end: 20, description: 'Leo on the rocky outcrop' },
|
|
137
|
+
{ start: 20, end: 30, description: 'butterfly appears' },
|
|
138
|
+
],
|
|
139
|
+
},
|
|
140
|
+
}),
|
|
141
|
+
});
|
|
142
|
+
expect(ctx.sceneContext).toBe('Leo on the rocky outcrop');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('returns undefined when no scene window matches the timestamp', () => {
|
|
146
|
+
const ctx = aggregateEntityContext({
|
|
147
|
+
imageRecord: imageWithTs,
|
|
148
|
+
enrichment: buildEnrichment({
|
|
149
|
+
result: {
|
|
150
|
+
scenes: [
|
|
151
|
+
{ start: 0, end: 10, description: 'opening titles' },
|
|
152
|
+
{ start: 20, end: 30, description: 'butterfly appears' },
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
}),
|
|
156
|
+
});
|
|
157
|
+
expect(ctx.sceneContext).toBeUndefined();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('falls back to `summary` when scenes array has no match', () => {
|
|
161
|
+
const ctx = aggregateEntityContext({
|
|
162
|
+
imageRecord: imageWithTs,
|
|
163
|
+
enrichment: buildEnrichment({
|
|
164
|
+
result: {
|
|
165
|
+
summary: 'A noble jungle king on his rocky throne.',
|
|
166
|
+
scenes: [{ start: 100, end: 200, description: 'unrelated' }],
|
|
167
|
+
},
|
|
168
|
+
}),
|
|
169
|
+
});
|
|
170
|
+
expect(ctx.sceneContext).toBe('A noble jungle king on his rocky throne.');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('falls back to `summary` when the enrichment result has no scenes array at all', () => {
|
|
174
|
+
const ctx = aggregateEntityContext({
|
|
175
|
+
imageRecord: imageWithTs,
|
|
176
|
+
enrichment: buildEnrichment({
|
|
177
|
+
result: { summary: 'A noble jungle king on his rocky throne.' },
|
|
178
|
+
}),
|
|
179
|
+
});
|
|
180
|
+
expect(ctx.sceneContext).toBe('A noble jungle king on his rocky throne.');
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('returns undefined when no enrichment is provided', () => {
|
|
184
|
+
const ctx = aggregateEntityContext({ imageRecord: imageWithTs });
|
|
185
|
+
expect(ctx.sceneContext).toBeUndefined();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('never throws on garbage `result` shape', () => {
|
|
189
|
+
expect(() =>
|
|
190
|
+
aggregateEntityContext({
|
|
191
|
+
imageRecord: imageWithTs,
|
|
192
|
+
enrichment: buildEnrichment({
|
|
193
|
+
result: { scenes: [null, 'not a scene', { description: 42 }, {}] } as Record<string, unknown>,
|
|
194
|
+
}),
|
|
195
|
+
}),
|
|
196
|
+
).not.toThrow();
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe('aggregatePersonaContext', () => {
|
|
201
|
+
it('returns null when custom_system_prompt is absent', () => {
|
|
202
|
+
expect(aggregatePersonaContext(buildPersona({ custom_system_prompt: null }))).toBeNull();
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('returns null when custom_system_prompt is whitespace-only', () => {
|
|
206
|
+
expect(aggregatePersonaContext(buildPersona({ custom_system_prompt: ' ' }))).toBeNull();
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('passes through short backstories untouched', () => {
|
|
210
|
+
const ctx = aggregatePersonaContext(
|
|
211
|
+
buildPersona({ custom_system_prompt: 'A noble jungle king.' }),
|
|
212
|
+
);
|
|
213
|
+
expect(ctx).not.toBeNull();
|
|
214
|
+
expect(ctx!.name).toBe('Leo');
|
|
215
|
+
expect(ctx!.backstory).toBe('A noble jungle king.');
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('truncates backstories longer than 400 chars and adds an ellipsis', () => {
|
|
219
|
+
const long = 'a'.repeat(800);
|
|
220
|
+
const ctx = aggregatePersonaContext(
|
|
221
|
+
buildPersona({ custom_system_prompt: long }),
|
|
222
|
+
);
|
|
223
|
+
expect(ctx).not.toBeNull();
|
|
224
|
+
// 400 chars + 1 ellipsis char
|
|
225
|
+
expect(ctx!.backstory.length).toBeLessThanOrEqual(401);
|
|
226
|
+
expect(ctx!.backstory.endsWith('…')).toBe(true);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('does not truncate exactly-400-char backstories', () => {
|
|
230
|
+
const exact = 'b'.repeat(400);
|
|
231
|
+
const ctx = aggregatePersonaContext(
|
|
232
|
+
buildPersona({ custom_system_prompt: exact }),
|
|
233
|
+
);
|
|
234
|
+
expect(ctx!.backstory).toBe(exact);
|
|
235
|
+
expect(ctx!.backstory.endsWith('…')).toBe(false);
|
|
236
|
+
});
|
|
237
|
+
});
|