|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { WIDGET_URIS } from '../../src/resources/widgets.js'; |
| 4 | +import { getActorRunWidgetTool } from '../../src/tools/apps/get_actor_run_widget.js'; |
| 5 | +import type { HelperTool, InternalToolArgs } from '../../src/types.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Apps / UI mode: get-actor-run-widget renders an interactive UI element (widget) |
| 9 | + * showing live Actor run progress. Carries widget `_meta` on both the tool definition |
| 10 | + * and the response. Input is strict: only `runId` is accepted. |
| 11 | + */ |
| 12 | + |
| 13 | +const MOCK_RUN = { |
| 14 | + id: 'run-widget-1', |
| 15 | + actId: 'actor-id-rag', |
| 16 | + status: 'RUNNING', |
| 17 | + startedAt: new Date('2026-04-20T12:00:00.000Z'), |
| 18 | + stats: {}, |
| 19 | +}; |
| 20 | + |
| 21 | +const MOCK_ACTOR = { |
| 22 | + username: 'apify', |
| 23 | + name: 'rag-web-browser', |
| 24 | +}; |
| 25 | + |
| 26 | +function stubApifyClient(): InternalToolArgs['apifyClient'] { |
| 27 | + return { |
| 28 | + run: (_id: string) => ({ |
| 29 | + get: async () => MOCK_RUN, |
| 30 | + }), |
| 31 | + actor: (_id: string) => ({ |
| 32 | + get: async () => MOCK_ACTOR, |
| 33 | + }), |
| 34 | + } as unknown as InternalToolArgs['apifyClient']; |
| 35 | +} |
| 36 | + |
| 37 | +function stubArgs(args: Record<string, unknown>): InternalToolArgs { |
| 38 | + return { |
| 39 | + args, |
| 40 | + apifyToken: 'test-token', |
| 41 | + apifyClient: stubApifyClient(), |
| 42 | + extra: {} as InternalToolArgs['extra'], |
| 43 | + mcpServer: {} as InternalToolArgs['mcpServer'], |
| 44 | + apifyMcpServer: { options: { paymentProvider: undefined } } as InternalToolArgs['apifyMcpServer'], |
| 45 | + } as InternalToolArgs; |
| 46 | +} |
| 47 | + |
| 48 | +describe('get-actor-run-widget response', () => { |
| 49 | + it('returns structured run status and widget _meta on the response', async () => { |
| 50 | + const result = await (getActorRunWidgetTool as HelperTool).call( |
| 51 | + stubArgs({ runId: 'run-widget-1' }), |
| 52 | + ); |
| 53 | + |
| 54 | + const { structuredContent, content, _meta } = result as { |
| 55 | + structuredContent: { |
| 56 | + runId: string; |
| 57 | + actorName?: string; |
| 58 | + status: string; |
| 59 | + startedAt: string; |
| 60 | + }; |
| 61 | + content: { type: string; text: string }[]; |
| 62 | + _meta?: { ui?: { resourceUri?: string; visibility?: readonly string[]; csp?: unknown }; 'openai/widgetDescription'?: string }; |
| 63 | + }; |
| 64 | + |
| 65 | + expect(structuredContent.runId).toBe('run-widget-1'); |
| 66 | + expect(structuredContent.actorName).toBe('apify/rag-web-browser'); |
| 67 | + expect(structuredContent.status).toBe('RUNNING'); |
| 68 | + expect(structuredContent.startedAt).toBe('2026-04-20T12:00:00.000Z'); |
| 69 | + |
| 70 | + // Short pointer text, not a JSON dump. |
| 71 | + expect(content).toHaveLength(1); |
| 72 | + expect(content[0].text).toContain('A progress widget has been rendered'); |
| 73 | + expect(content[0].text).toContain('run-widget-1'); |
| 74 | + |
| 75 | + // Response-level widget _meta. |
| 76 | + expect(_meta?.ui?.resourceUri).toBe(WIDGET_URIS.ACTOR_RUN); |
| 77 | + expect(_meta?.ui?.visibility).toEqual(['model', 'app']); |
| 78 | + expect(_meta?.ui?.csp).toBeDefined(); |
| 79 | + expect(_meta?.['openai/widgetDescription']).toContain('apify/rag-web-browser'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('carries widget _meta on the tool definition', () => { |
| 83 | + const tool = getActorRunWidgetTool as HelperTool; |
| 84 | + const meta = tool._meta as { ui?: { resourceUri?: string; visibility?: readonly string[]; csp?: unknown } }; |
| 85 | + expect(meta.ui?.resourceUri).toBe(WIDGET_URIS.ACTOR_RUN); |
| 86 | + expect(meta.ui?.visibility).toEqual(['model', 'app']); |
| 87 | + expect(meta.ui?.csp).toBeDefined(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('declares a strict input schema accepting only runId', () => { |
| 91 | + const tool = getActorRunWidgetTool as HelperTool; |
| 92 | + |
| 93 | + const schema = tool.inputSchema as { additionalProperties?: boolean; properties?: Record<string, unknown>; required?: string[] }; |
| 94 | + expect(schema.additionalProperties).toBe(false); |
| 95 | + expect(Object.keys(schema.properties ?? {})).toEqual(['runId']); |
| 96 | + expect(schema.required).toEqual(['runId']); |
| 97 | + |
| 98 | + // Runtime: AJV is configured with `removeAdditional: true`, so stray keys are silently |
| 99 | + // stripped from the input object in place. |
| 100 | + const input: Record<string, unknown> = { runId: 'run-widget-1', output: true }; |
| 101 | + const ok = tool.ajvValidate(input); |
| 102 | + expect(ok).toBe(true); |
| 103 | + expect('output' in input).toBe(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it('accepts a minimal runId payload', () => { |
| 107 | + const tool = getActorRunWidgetTool as HelperTool; |
| 108 | + const ok = tool.ajvValidate({ runId: 'run-widget-1' }); |
| 109 | + expect(ok).toBe(true); |
| 110 | + }); |
| 111 | +}); |
0 commit comments