-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathtools.get_actor_run_log.test.ts
More file actions
116 lines (83 loc) · 4.26 KB
/
Copy pathtools.get_actor_run_log.test.ts
File metadata and controls
116 lines (83 loc) · 4.26 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
import { describe, expect, it, vi } from 'vitest';
import { HELPER_TOOLS } from '../../src/const.js';
import { getActorRunLog } from '../../src/tools/runs/get_actor_run_log.js';
import { getActorRunLogToolOutputSchema } from '../../src/tools/structured_output_schemas.js';
import type { HelperTool, InternalToolArgs } from '../../src/types.js';
import {
expectSchemaConformingStructuredContent,
stubToolCallContext,
type TextToolResult,
} from './helpers/tool_context.js';
const getMock = vi.fn();
const stubClient = { run: () => ({ log: () => ({ get: getMock }) }) } as unknown as InternalToolArgs['apifyClient'];
const numberedLog = (count: number) => Array.from({ length: count }, (_, i) => `line ${i + 1}`).join('\n');
const callTool = async (args: Record<string, unknown>) =>
(await (getActorRunLog as HelperTool).call(stubToolCallContext(args, stubClient))) as TextToolResult;
describe('get-actor-log', () => {
it('has the expected tool name', () => {
expect(getActorRunLog.name).toBe(HELPER_TOOLS.ACTOR_RUNS_LOG);
});
it('returns exactly the requested number of trailing lines', async () => {
getMock.mockResolvedValue(numberedLog(20));
const { content } = await callTool({ runId: 'run-1', lines: 10 });
const returned = content[0].text.split('\n');
expect(returned).toHaveLength(10);
expect(returned).toEqual([
'line 11',
'line 12',
'line 13',
'line 14',
'line 15',
'line 16',
'line 17',
'line 18',
'line 19',
'line 20',
]);
});
it('returns a single line when one line is requested', async () => {
getMock.mockResolvedValue(numberedLog(20));
const { content } = await callTool({ runId: 'run-1', lines: 1 });
expect(content[0].text).toBe('line 20');
});
it('returns exactly the default 10 lines when lines is omitted', async () => {
getMock.mockResolvedValue(numberedLog(50));
const { content } = await callTool({ runId: 'run-1' });
expect(content[0].text.split('\n')).toHaveLength(10);
});
it('returns only content lines when the log ends with a newline', async () => {
getMock.mockResolvedValue(`${numberedLog(20)}\n`);
const { content } = await callTool({ runId: 'run-1', lines: 3 });
const returned = content[0].text.split('\n');
expect(returned).toEqual(['line 18', 'line 19', 'line 20']);
});
it('returns the last content line when one line is requested and the log ends with a newline', async () => {
getMock.mockResolvedValue(`${numberedLog(20)}\n`);
const { content } = await callTool({ runId: 'run-1', lines: 1 });
expect(content[0].text).toBe('line 20');
});
it('keeps a trailing blank line that is followed by nothing but one newline', async () => {
getMock.mockResolvedValue(`${numberedLog(3)}\n\n`);
const { content } = await callTool({ runId: 'run-1', lines: 2 });
expect(content[0].text.split('\n')).toEqual(['line 3', '']);
});
it('returns the whole log when it is shorter than the requested number of lines', async () => {
getMock.mockResolvedValue(numberedLog(3));
const { content } = await callTool({ runId: 'run-1', lines: 10 });
expect(content[0].text).toBe('line 1\nline 2\nline 3');
});
it('mirrors the log text in structuredContent and declares an outputSchema', async () => {
getMock.mockResolvedValue(numberedLog(20));
const result = await callTool({ runId: 'run-1', lines: 10 });
expect(result.structuredContent).toEqual({ log: result.content[0].text });
expect((getActorRunLog as HelperTool).outputSchema).toBe(getActorRunLogToolOutputSchema);
expectSchemaConformingStructuredContent(result, getActorRunLogToolOutputSchema);
});
it('returns conforming structuredContent for an empty log', async () => {
getMock.mockResolvedValue(undefined);
const result = await callTool({ runId: 'run-1', lines: 10 });
expect(result.content[0].text).toBe('');
expect(result.structuredContent).toEqual({ log: '' });
expectSchemaConformingStructuredContent(result, getActorRunLogToolOutputSchema);
});
});