-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.test.ts
More file actions
230 lines (208 loc) · 8.42 KB
/
Copy pathbundle.test.ts
File metadata and controls
230 lines (208 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import path from 'node:path';
import os from 'node:os';
import { pathToFileURL } from 'node:url';
import { bundleStager } from './bundle.js';
import { runtimeContextEnv } from './runtime-context.js';
import type { PersonaSpec } from '@agentworkforce/persona-kit';
const require = createRequire(import.meta.url);
function persona(overrides: Partial<PersonaSpec> = {}): PersonaSpec {
return {
id: 'bundle-fixture',
intent: 'documentation',
tags: ['documentation'],
description: 'fixture for bundle tests',
skills: [],
harness: 'claude',
model: 'anthropic/claude-3-5-sonnet',
systemPrompt: 'be helpful',
harnessSettings: { reasoning: 'medium', timeoutSeconds: 300 },
cloud: true,
onEvent: './agent.ts',
...overrides
};
}
test('bundleStager produces an executable, importable bundle from a real onEvent file', async () => {
const dir = await mkdtemp(path.join(os.tmpdir(), 'wf-bundle-'));
try {
const personaPath = path.join(dir, 'persona.json');
const personaSpec = persona();
await writeFile(personaPath, JSON.stringify(personaSpec, null, 2), 'utf8');
await writeFile(
path.join(dir, 'agent.ts'),
[
"import { defineAgent } from '@agentworkforce/runtime';",
'',
'export default defineAgent({',
" schedules: [{ name: 'weekly', cron: '0 9 * * 6' }],",
' handler: async (ctx, event) => {',
" ctx.log('info', 'fixture.handler.fired', { eventId: event.id });",
' }',
'});',
''
].join('\n'),
'utf8'
);
const outDir = path.join(dir, 'build');
const result = await bundleStager.stage({
personaPath,
persona: personaSpec,
outDir
});
assert.equal(result.personaCopyPath, path.join(outDir, 'persona.json'));
assert.equal(result.runnerPath, path.join(outDir, 'runner.mjs'));
assert.equal(result.bundlePath, path.join(outDir, 'agent.bundle.mjs'));
assert.equal(result.packageJsonPath, path.join(outDir, 'package.json'));
assert.ok(result.sizeBytes > 0);
// persona.json round-trips verbatim
const personaCopy = JSON.parse(await readFile(result.personaCopyPath, 'utf8'));
assert.equal(personaCopy.id, personaSpec.id);
assert.equal(personaCopy.onEvent, './agent.ts');
// runner imports the expected entry points
const runnerSource = await readFile(result.runnerPath, 'utf8');
assert.match(runnerSource, /from '@agentworkforce\/runtime\/runner'/);
assert.match(runnerSource, /from '@agentworkforce\/runtime'/);
assert.match(runnerSource, /import \* as userModule from '\.\/agent\.bundle\.mjs'/);
assert.match(runnerSource, /WORKFORCE_AGENT_CONTEXT/);
assert.match(runnerSource, /WORKFORCE_DEPLOYMENT_CONTEXT/);
assert.match(runnerSource, /agentSpec = projectAgentSpec\(exported\)/);
assert.match(runnerSource, /delete spec\.handler/);
assert.match(runnerSource, /Object\.keys\(persona\)/);
assert.match(runnerSource, /await startRunner\({ persona, agent, deployment, handler/);
// bundle output is ES module shape and references the runtime as external
const bundleSource = await readFile(result.bundlePath, 'utf8');
assert.match(bundleSource, /^import /m);
assert.match(bundleSource, /from\s+['"]@agentworkforce\/runtime['"]/);
// package.json pins the exact installed runtime version — never a
// wildcard a sandbox's npm install could silently satisfy with a
// stale pre-baked/cached copy.
const generatedPackageJson = JSON.parse(await readFile(result.packageJsonPath, 'utf8'));
const runtimeDep = generatedPackageJson.dependencies['@agentworkforce/runtime'];
const installedRuntimePackageJsonPath = require.resolve('@agentworkforce/runtime/package.json');
const installedRuntimeVersion = JSON.parse(
await readFile(installedRuntimePackageJsonPath, 'utf8')
).version;
assert.equal(runtimeDep, installedRuntimeVersion);
assert.notEqual(runtimeDep, '*');
} finally {
await rm(dir, { recursive: true, force: true });
}
});
test('bundleStager leaves bare Node builtins external for transitive CommonJS deps', async () => {
const dir = await mkdtemp(path.join(os.tmpdir(), 'wf-bundle-'));
try {
const personaPath = path.join(dir, 'persona.json');
const personaSpec = persona();
await writeFile(personaPath, JSON.stringify(personaSpec, null, 2), 'utf8');
const packageDir = path.join(dir, 'node_modules', 'cjs-process-user');
await mkdir(packageDir, { recursive: true });
await writeFile(
path.join(packageDir, 'package.json'),
JSON.stringify({ name: 'cjs-process-user', version: '1.0.0', main: 'index.cjs' }, null, 2),
'utf8'
);
await writeFile(
path.join(packageDir, 'index.cjs'),
[
"const process = require('process');",
'module.exports = process.release.name;',
''
].join('\n'),
'utf8'
);
await writeFile(
path.join(dir, 'agent.ts'),
[
"import runtimeName from 'cjs-process-user';",
'',
'export default async function handler() {',
' return runtimeName;',
'}',
''
].join('\n'),
'utf8'
);
const result = await bundleStager.stage({
personaPath,
persona: personaSpec,
outDir: path.join(dir, 'build')
});
const mod = await import(pathToFileURL(result.bundlePath).href);
assert.equal(typeof mod.default, 'function');
assert.equal(await mod.default(), 'node');
} finally {
await rm(dir, { recursive: true, force: true });
}
});
test('bundleStager throws when onEvent file is missing', async () => {
const dir = await mkdtemp(path.join(os.tmpdir(), 'wf-bundle-'));
try {
const personaPath = path.join(dir, 'persona.json');
const personaSpec = persona({ onEvent: './missing.ts' });
await writeFile(personaPath, JSON.stringify(personaSpec, null, 2), 'utf8');
await assert.rejects(
() => bundleStager.stage({ personaPath, persona: personaSpec, outDir: path.join(dir, 'build') }),
/file not found/
);
} finally {
await rm(dir, { recursive: true, force: true });
}
});
test('bundleStager throws when persona has no onEvent', async () => {
const dir = await mkdtemp(path.join(os.tmpdir(), 'wf-bundle-'));
try {
const personaPath = path.join(dir, 'persona.json');
const personaSpec = persona();
delete (personaSpec as { onEvent?: string }).onEvent;
await writeFile(personaPath, JSON.stringify(personaSpec, null, 2), 'utf8');
await assert.rejects(
() => bundleStager.stage({ personaPath, persona: personaSpec, outDir: path.join(dir, 'build') }),
/missing onEvent/
);
} finally {
await rm(dir, { recursive: true, force: true });
}
});
test('runtimeContextEnv injects explicit runner row context', () => {
const env = runtimeContextEnv(persona(), {
WORKFORCE_AGENT_ID: 'agent_123',
WORKFORCE_AGENT_DEPLOYED_NAME: 'docs-demo',
WORKFORCE_DEPLOYMENT_ID: 'deployment_456',
WORKFORCE_DEPLOYMENT_TRIGGER_KIND: 'inbox'
});
assert.deepEqual(JSON.parse(env.WORKFORCE_AGENT_CONTEXT), {
id: 'agent_123',
deployedName: 'docs-demo',
spawnedByAgentId: null
});
assert.deepEqual(JSON.parse(env.WORKFORCE_DEPLOYMENT_CONTEXT), {
id: 'deployment_456',
triggerKind: 'inbox',
parentDeploymentId: null
});
});
test('runtimeContextEnv preserves precomputed row context JSON', () => {
const env = runtimeContextEnv(persona(), {
WORKFORCE_AGENT_CONTEXT: '{"id":"agent_real"}',
WORKFORCE_DEPLOYMENT_CONTEXT: '{"id":"deployment_real"}'
});
assert.equal(env.WORKFORCE_AGENT_CONTEXT, '{"id":"agent_real"}');
assert.equal(env.WORKFORCE_DEPLOYMENT_CONTEXT, '{"id":"deployment_real"}');
});
test('runtimeContextEnv infers radio for integration-triggered agents', () => {
const env = runtimeContextEnv(
persona({ integrations: { github: {} } }),
undefined,
{ triggers: { github: [{ on: 'pull_request.opened' }] } }
);
assert.equal(JSON.parse(env.WORKFORCE_DEPLOYMENT_CONTEXT).triggerKind, 'radio');
});
test('runtimeContextEnv defaults to clock when the agent has no integration triggers', () => {
const env = runtimeContextEnv(persona(), undefined, {
schedules: [{ name: 'weekly', cron: '0 9 * * 6' }]
});
assert.equal(JSON.parse(env.WORKFORCE_DEPLOYMENT_CONTEXT).triggerKind, 'clock');
});