-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotion-essay-pr.smoke.test.ts
More file actions
278 lines (262 loc) · 8.84 KB
/
Copy pathnotion-essay-pr.smoke.test.ts
File metadata and controls
278 lines (262 loc) · 8.84 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import assert from 'node:assert/strict';
import { mkdtemp, mkdir, readdir, readFile, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import test from 'node:test';
import notionEssayPr from './agent.js';
import { envelopeToAgentEvent, type WorkforceCtx, type WorkforceEvent } from '@agentworkforce/runtime';
const FAKE_RECEIPT_URL = 'https://github.com/AgentWorkforce/proactive-agents/pull/17';
test('notion-essay-pr smoke skeleton runs Notion page to essay PR with mocks', async () => {
const runtime = await MockNotionEssayRuntime.create();
try {
runtime.startFakeWriteback();
await runtime.spawnAndDispatch(notionEssayPr);
runtime.stopFakeWriteback();
assert.equal(runtime.sandboxSpawned, true);
assert.equal(runtime.files.get('/workspace/AGENTS.md'), '# Agent: notion-essay-pr\n');
assert.deepEqual(runtime.fileReads, ['/notion/pages/page-123.md']);
assert.equal(runtime.files.get('/workspace/output/page-123.md'), '# A useful essay\n\nDraft body.\n');
const writebacks = await runtime.collectPullRequestWritebacks();
assert.equal(writebacks.length, 1);
assert.equal(writebacks[0]?.title, 'Essay: Launch notes');
assert.equal(writebacks[0]?.head, 'essay/page-123');
assert.equal(writebacks[0]?.base, 'main');
assert.equal(writebacks[0]?.url, FAKE_RECEIPT_URL);
assert.equal(runtime.memorySaves.length, 1);
const save = runtime.memorySaves[0]!;
assert.equal(
save.content,
`Notion essay PR opened for Launch notes: ${FAKE_RECEIPT_URL}`
);
assert.equal(save.scope, 'workspace');
assert.deepEqual(save.tags, ['notion-essay-pr', 'page:page-123']);
} finally {
runtime.stopFakeWriteback();
delete process.env.RELAYFILE_MOUNT_ROOT;
}
});
class MockNotionEssayRuntime {
readonly files = new Map<string, string>([
['/notion/pages/page-123.md', '# Launch notes\n\nWe shipped the first customer-facing deploy path.']
]);
readonly fileReads: string[] = [];
readonly memorySaves: Array<{ content: string; scope?: string; tags?: string[] }> = [];
sandboxSpawned = false;
readonly relayfileRoot: string;
private writebackTimer: NodeJS.Timeout | undefined;
static async create(): Promise<MockNotionEssayRuntime> {
const root = await mkdtemp(path.join(tmpdir(), 'notion-essay-pr-'));
process.env.RELAYFILE_MOUNT_ROOT = root;
return new MockNotionEssayRuntime(root);
}
private constructor(relayfileRoot: string) {
this.relayfileRoot = relayfileRoot;
}
/**
* Stand-in for the Relayfile writeback worker: every 50ms, look for
* freshly written draft JSON in `github/repos/.../pulls/` and rewrite
* it with a receipt envelope carrying a real-looking URL. The agent's
* `writeJsonFile` polls the same file for top-level `created` / `url`
* / `id` keys and resolves once they appear.
*/
startFakeWriteback(): void {
const pullsDir = path.join(
this.relayfileRoot,
'github',
'repos',
'AgentWorkforce',
'proactive-agents',
'pulls'
);
this.writebackTimer = setInterval(async () => {
const entries = await readdir(pullsDir).catch(() => [] as string[]);
for (const entry of entries) {
if (!entry.endsWith('.json')) continue;
const target = path.join(pullsDir, entry);
const body = await readFile(target, 'utf8').catch(() => null);
if (!body) continue;
let parsed: Record<string, unknown>;
try {
parsed = JSON.parse(body) as Record<string, unknown>;
} catch {
continue;
}
if (typeof parsed.created === 'string' || typeof parsed.url === 'string') continue;
const receipt = {
...parsed,
created: new Date().toISOString(),
id: 'pr-17',
url: FAKE_RECEIPT_URL
};
await writeFile(target, `${JSON.stringify(receipt, null, 2)}\n`, 'utf8');
}
}, 50);
}
stopFakeWriteback(): void {
if (this.writebackTimer) {
clearInterval(this.writebackTimer);
this.writebackTimer = undefined;
}
}
async spawnAndDispatch(agent: { handler: (ctx: WorkforceCtx, event: WorkforceEvent) => Promise<void> | void }): Promise<void> {
this.sandboxSpawned = true;
this.files.set('/workspace/AGENTS.md', '# Agent: notion-essay-pr\n');
const event = envelopeToAgentEvent({
id: 'evt-page-123',
workspace: 'ws-proactive',
type: 'notion.page.created',
occurredAt: '2026-05-13T12:00:00.000Z',
attempt: 1,
resource: {
pageId: 'page-123',
title: 'Launch notes'
},
summary: {
title: 'Launch notes'
}
});
if (!event) throw new Error('failed to build test event');
await agent.handler(this.ctx(), event);
}
async collectPullRequestWritebacks(): Promise<Array<Record<string, unknown>>> {
const dir = path.join(
this.relayfileRoot,
'github',
'repos',
'AgentWorkforce',
'proactive-agents',
'pulls'
);
const entries = await readdir(dir).catch(() => []);
const out: Array<Record<string, unknown>> = [];
for (const entry of entries) {
if (!entry.endsWith('.json')) continue;
const body = JSON.parse(await readFile(path.join(dir, entry), 'utf8')) as Record<string, unknown>;
out.push(body);
}
return out;
}
private ctx(): WorkforceCtx {
return {
persona: {
id: 'notion-essay-pr',
intent: 'documentation',
tags: ['documentation'],
description: 'fixture',
skills: [],
harness: 'claude',
model: 'claude-sonnet-4-6',
systemPrompt: '',
harnessSettings: { reasoning: 'medium', timeoutSeconds: 600 },
inputs: { GITHUB_TARGET_REPO: 'AgentWorkforce/proactive-agents' },
inputSpecs: {}
},
agent: { id: 'agent-1', deployedName: 'notion-essay-pr', spawnedByAgentId: null },
deployment: { id: 'deployment-1', triggerKind: 'radio', parentDeploymentId: null },
workspaceId: 'ws-proactive',
agentName: 'notion-essay-pr',
llm: {
async complete() {
return '';
}
},
harness: {
async run() {
return { output: '# A useful essay\n\nDraft body.', exitCode: 0, durationMs: 25 };
}
},
sandbox: {
cwd: '/workspace',
async exec() {
return { output: '', exitCode: 0 };
},
readFile: (filePath) => this.read(filePath),
writeFile: (filePath, contents) => this.write(filePath, contents)
},
files: {
read: (filePath) => this.read(filePath),
write: (filePath, contents) => this.write(filePath, contents)
},
credentials: {
relayfile: {
url: 'https://relayfile.example.test',
token: 'relayfile-token',
workspaceId: 'rw_proactive'
},
cloudApi: {
url: 'https://cloud.example.test',
token: 'cloud-api-token'
},
tryRequire() {
return {
relayfile: this.relayfile,
cloudApi: this.cloudApi
};
},
require() {
return {
relayfile: this.relayfile,
cloudApi: this.cloudApi
};
}
},
memory: {
async recall() {
return [{
id: 'mem-1',
content: 'Previous essays should be concise.',
tags: ['notion-essay-pr'],
scope: 'workspace',
createdAt: '2026-05-13T10:00:00.000Z'
}];
},
save: async (content, opts) => {
this.memorySaves.push({ content, scope: opts?.scope, tags: opts?.tags });
return { id: 'mem-2' };
}
},
workflow: {
async run() {
throw new Error('not configured');
},
async status() {
throw new Error('not configured');
}
},
schedule: {
async at() {
/* unused */
},
async cancel() {
/* unused */
}
},
trajectory: {
async chapter() {},
async note() {},
async decide() {},
async error() {},
async done() {}
},
relay: {
async dm() { return { ok: false }; },
async post() { return { ok: false }; }
},
log: () => undefined
};
}
private async read(filePath: string): Promise<string> {
this.fileReads.push(filePath);
const value = this.files.get(filePath);
if (value === undefined) throw new Error(`missing file: ${filePath}`);
return value;
}
private async write(filePath: string, contents: string): Promise<void> {
this.files.set(filePath, contents);
if (filePath.startsWith('/workspace/')) {
const target = path.join(this.relayfileRoot, filePath.replace(/^\//, ''));
await mkdir(path.dirname(target), { recursive: true });
await writeFile(target, contents, 'utf8');
}
}
}