Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/core/src/api/page-actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from 'bun:test';
import { getPageActionTool } from '../utils/getPageActionTool';
import { pageActionTools } from './page-actions';

describe('pageActionTools', () => {
Expand Down Expand Up @@ -32,21 +33,21 @@ describe('pageActionTools', () => {
});

it('click has a single target parameter', () => {
const click = pageActionTools.find((t) => t.name === 'click')!;
const click = getPageActionTool('click');
expect(click.parameters).toHaveLength(1);
expect(click.parameters[0].name).toBe('target');
expect(click.parameters[0].type).toBe('string');
});

it('scroll_to has a single target parameter', () => {
const scrollTo = pageActionTools.find((t) => t.name === 'scroll_to')!;
const scrollTo = getPageActionTool('scroll_to');
expect(scrollTo.parameters).toHaveLength(1);
expect(scrollTo.parameters[0].name).toBe('target');
expect(scrollTo.parameters[0].type).toBe('string');
});

it('highlight has target and duration parameters', () => {
const highlight = pageActionTools.find((t) => t.name === 'highlight')!;
const highlight = getPageActionTool('highlight');
expect(highlight.parameters).toHaveLength(2);
expect(highlight.parameters[0].name).toBe('target');
expect(highlight.parameters[1].name).toBe('duration');
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/utils/getPageActionTool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from 'bun:test';
import { pageActionTools } from '../api';
import { getPageActionTool } from './getPageActionTool';

describe('getPageActionTool', () => {
it('returns the tool with the given name', () => {
expect(getPageActionTool('click').name).toBe('click');
});

it('returns the same object as pageActionTools', () => {
for (const tool of pageActionTools) {
expect(getPageActionTool(tool.name)).toBe(tool);
}
});

it('throws for an unknown tool name', () => {
expect(() => getPageActionTool('does_not_exist')).toThrow(
'Missing page action tool: does_not_exist',
);
});
});
11 changes: 11 additions & 0 deletions packages/core/src/utils/getPageActionTool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { pageActionTools } from '../api';

/**
* Look up a page action tool by name, throwing if it doesn't exist.
* Avoids non-null assertions when a test needs a specific tool.
*/
export function getPageActionTool(name: string) {
const tool = pageActionTools.find((t) => t.name === name);
if (!tool) throw new Error(`Missing page action tool: ${name}`);
return tool;
}