From c30441c6c63938db48b503e7816ccb81a0449af3 Mon Sep 17 00:00:00 2001 From: Eli Payano <48443245+eli7pm@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:51:31 -0400 Subject: [PATCH] test: replace non-null assertions in page action tests with a lookup helper Add getPageActionTool, which finds a page action tool by name and throws a descriptive error if it's missing, and use it in page-actions.test.ts instead of `find(...)!`. Clears the lint/style/noNonNullAssertion warnings in that file. Co-Authored-By: Claude Opus 5.5 --- packages/core/src/api/page-actions.test.ts | 7 ++++--- .../core/src/utils/getPageActionTool.test.ts | 21 +++++++++++++++++++ packages/core/src/utils/getPageActionTool.ts | 11 ++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 packages/core/src/utils/getPageActionTool.test.ts create mode 100644 packages/core/src/utils/getPageActionTool.ts diff --git a/packages/core/src/api/page-actions.test.ts b/packages/core/src/api/page-actions.test.ts index 4fed4e5..7276963 100644 --- a/packages/core/src/api/page-actions.test.ts +++ b/packages/core/src/api/page-actions.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from 'bun:test'; +import { getPageActionTool } from '../utils/getPageActionTool'; import { pageActionTools } from './page-actions'; describe('pageActionTools', () => { @@ -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'); diff --git a/packages/core/src/utils/getPageActionTool.test.ts b/packages/core/src/utils/getPageActionTool.test.ts new file mode 100644 index 0000000..e21c787 --- /dev/null +++ b/packages/core/src/utils/getPageActionTool.test.ts @@ -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', + ); + }); +}); diff --git a/packages/core/src/utils/getPageActionTool.ts b/packages/core/src/utils/getPageActionTool.ts new file mode 100644 index 0000000..7e4f013 --- /dev/null +++ b/packages/core/src/utils/getPageActionTool.ts @@ -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; +}