|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { HELPER_TOOLS } from '../../src/const.js'; |
| 4 | +import { getActorRunLog } from '../../src/tools/runs/get_actor_run_log.js'; |
| 5 | +import { getActorRunLogToolOutputSchema } from '../../src/tools/structured_output_schemas.js'; |
| 6 | +import type { HelperTool, InternalToolArgs } from '../../src/types.js'; |
| 7 | +import { |
| 8 | + expectSchemaConformingStructuredContent, |
| 9 | + stubToolCallContext, |
| 10 | + type TextToolResult, |
| 11 | +} from './helpers/tool_context.js'; |
| 12 | + |
| 13 | +const getMock = vi.fn(); |
| 14 | +const logMock = vi.fn(() => ({ get: getMock })); |
| 15 | +const runMock = vi.fn(() => ({ log: logMock })); |
| 16 | + |
| 17 | +const stubClient = { run: runMock } as unknown as InternalToolArgs['apifyClient']; |
| 18 | + |
| 19 | +const LOG_LINES = Array.from({ length: 20 }, (_, i) => `line ${i + 1}`); |
| 20 | +const LOG_TEXT = LOG_LINES.join('\n'); |
| 21 | + |
| 22 | +describe('get-actor-run-log', () => { |
| 23 | + it('has the expected tool name', () => { |
| 24 | + expect(getActorRunLog.name).toBe(HELPER_TOOLS.ACTOR_RUNS_LOG); |
| 25 | + }); |
| 26 | + |
| 27 | + it('declares an outputSchema', () => { |
| 28 | + expect((getActorRunLog as HelperTool).outputSchema).toMatchObject({ type: 'object' }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns the last N lines matching the tool slicing', async () => { |
| 32 | + getMock.mockResolvedValue(LOG_TEXT); |
| 33 | + |
| 34 | + const result = await (getActorRunLog as HelperTool).call( |
| 35 | + stubToolCallContext({ runId: 'run-1', lines: 5 }, stubClient), |
| 36 | + ); |
| 37 | + const { content } = result as TextToolResult; |
| 38 | + |
| 39 | + // Expected derived from the same slice expression the tool uses (current behavior; the -1 |
| 40 | + // off-by-one is intentional and out of scope here). |
| 41 | + const expected = LOG_LINES.slice(LOG_LINES.length - 5 - 1, LOG_LINES.length).join('\n'); |
| 42 | + expect(content[0].text).toBe(expected); |
| 43 | + expect(runMock).toHaveBeenCalledWith('run-1'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('mirrors the text content in structuredContent.log', async () => { |
| 47 | + getMock.mockResolvedValue(LOG_TEXT); |
| 48 | + |
| 49 | + const result = await (getActorRunLog as HelperTool).call( |
| 50 | + stubToolCallContext({ runId: 'run-1', lines: 5 }, stubClient), |
| 51 | + ); |
| 52 | + const { content, structuredContent } = result as TextToolResult; |
| 53 | + |
| 54 | + expect((structuredContent as { log: string }).log).toBe(content[0].text); |
| 55 | + }); |
| 56 | + |
| 57 | + it('emits structuredContent conforming to the outputSchema', async () => { |
| 58 | + getMock.mockResolvedValue(LOG_TEXT); |
| 59 | + |
| 60 | + const result = await (getActorRunLog as HelperTool).call( |
| 61 | + stubToolCallContext({ runId: 'run-1', lines: 5 }, stubClient), |
| 62 | + ); |
| 63 | + |
| 64 | + expectSchemaConformingStructuredContent(result, getActorRunLogToolOutputSchema); |
| 65 | + }); |
| 66 | +}); |
0 commit comments