Skip to content

Commit 95f142b

Browse files
steipetevincentkoc
andauthored
fix(ui): isolate retained test modules (#105711)
* fix(ui): isolate retained test elements * test(ui): share terminal mock across retained modules * refactor(ui): trim private search exports * test(ui): type retained terminal mock * test(ui): narrow terminal mock contract * test(ui): isolate approval page modules --------- Co-authored-by: Vincent Koc <[email protected]>
1 parent 2c06dfd commit 95f142b

6 files changed

Lines changed: 37 additions & 33 deletions

File tree

‎ui/src/components/config-form.node.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import {
2222
type JsonSchema,
2323
} from "./config-form.shared.ts";
2424

25-
export { matchesNodeSearch, parseConfigSearchQuery } from "./config-form.search.ts";
26-
2725
const META_KEYS = new Set(["title", "description", "default", "nullable", "tags", "x-tags"]);
2826

2927
function isAnySchema(schema: JsonSchema): boolean {

‎ui/src/components/config-form.search.node.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @vitest-environment node
22
import { describe, expect, it } from "vitest";
3-
import { matchesNodeSearch, parseConfigSearchQuery } from "./config-form.node.ts";
3+
import { matchesNodeSearch, parseConfigSearchQuery } from "./config-form.search.ts";
44

55
const schema = {
66
type: "object",

‎ui/src/components/config-form.search.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ export type ConfigSearchCriteria = {
77
tags: string[];
88
};
99

10-
export type ConfigFieldMeta = {
10+
type ConfigFieldMeta = {
1111
label: string;
1212
help?: string;
1313
tags: string[];
1414
};
1515

16-
export type ConfigSearchTextMatcher = (value: string, query: string) => boolean;
16+
type ConfigSearchTextMatcher = (value: string, query: string) => boolean;
1717

1818
export function hasConfigSearchCriteria(criteria: ConfigSearchCriteria | undefined): boolean {
1919
return Boolean(criteria && (criteria.text.length > 0 || criteria.tags.length > 0));

‎ui/src/components/native-link-menu.test.ts‎

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
/* @vitest-environment jsdom */
22

3-
import { html, render } from "lit";
43
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
54
import { i18n } from "../i18n/index.ts";
65
import { NativeLinkMenu, type NativeLinkMenuAction } from "./native-link-menu.ts";
76

8-
const NATIVE_LINK_MENU_ELEMENT_NAME = "test-openclaw-native-link-menu";
7+
const NATIVE_LINK_MENU_ELEMENT_NAME = `test-openclaw-native-link-menu-${crypto.randomUUID()}`;
98
const containers: HTMLElement[] = [];
109

1110
// The non-isolated UI runner resets modules but not customElements. Register
1211
// the current class graph so instanceof and locale updates share one module.
1312
class TestNativeLinkMenu extends NativeLinkMenu {}
1413

15-
if (!customElements.get(NATIVE_LINK_MENU_ELEMENT_NAME)) {
16-
customElements.define(NATIVE_LINK_MENU_ELEMENT_NAME, TestNativeLinkMenu);
17-
}
14+
customElements.define(NATIVE_LINK_MENU_ELEMENT_NAME, TestNativeLinkMenu);
1815

1916
beforeEach(async () => {
2017
await i18n.setLocale("en");
@@ -35,17 +32,13 @@ async function mountMenu(options: {
3532
const container = document.createElement("div");
3633
containers.push(container);
3734
document.body.append(container);
38-
render(
39-
html`<test-openclaw-native-link-menu
40-
.x=${100}
41-
.y=${100}
42-
.trigger=${options.trigger ?? null}
43-
.onAction=${options.onAction ?? (() => {})}
44-
.onClose=${options.onClose ?? (() => {})}
45-
></test-openclaw-native-link-menu>`,
46-
container,
47-
);
48-
const menu = container.querySelector(NATIVE_LINK_MENU_ELEMENT_NAME);
35+
const menu = document.createElement(NATIVE_LINK_MENU_ELEMENT_NAME) as NativeLinkMenu;
36+
menu.x = 100;
37+
menu.y = 100;
38+
menu.trigger = options.trigger ?? null;
39+
menu.onAction = options.onAction ?? (() => {});
40+
menu.onClose = options.onClose ?? (() => {});
41+
container.append(menu);
4942
if (!(menu instanceof NativeLinkMenu)) {
5043
throw new Error("Expected native link menu");
5144
}

‎ui/src/components/terminal/terminal-panel.test.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @vitest-environment jsdom */
22

3-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3+
import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from "vitest";
44
import { i18n } from "../../i18n/index.ts";
55
import type { TerminalGatewayClient } from "./terminal-connection.ts";
66

@@ -11,7 +11,19 @@ type CreateOptions = {
1111
onResize?: (size: { columns: number; rows: number }) => void;
1212
};
1313

14-
const createGhosttyTerminalMock = vi.hoisted(() => vi.fn());
14+
type CreateGhosttyTerminalMock = Mock<
15+
(options: CreateOptions) => Promise<ReturnType<typeof createTerminalController>>
16+
>;
17+
18+
// Vitest resets this test module but can retain terminal-panel.ts. Reuse one
19+
// mock so retained imports and the current test graph share the same identity.
20+
const createGhosttyTerminalMock = vi.hoisted(() => {
21+
const scope = globalThis as typeof globalThis & {
22+
openclawTestCreateGhosttyTerminalMock?: CreateGhosttyTerminalMock;
23+
};
24+
return (scope.openclawTestCreateGhosttyTerminalMock ??=
25+
vi.fn<(options: CreateOptions) => Promise<ReturnType<typeof createTerminalController>>>());
26+
});
1527

1628
function createTerminalController(dispose: () => void = vi.fn()) {
1729
return {
@@ -52,15 +64,13 @@ vi.mock("./terminal-runtime.ts", () => {
5264

5365
import { OpenClawTerminalPanel } from "./terminal-panel.ts";
5466

55-
const TERMINAL_PANEL_ELEMENT_NAME = "test-openclaw-terminal-panel";
67+
const TERMINAL_PANEL_ELEMENT_NAME = `test-openclaw-terminal-panel-${crypto.randomUUID()}`;
5668

5769
// Keep the mounted panel and i18n manager in the current module graph when
5870
// the non-isolated runner has retained an earlier production registration.
5971
class TestTerminalPanel extends OpenClawTerminalPanel {}
6072

61-
if (!customElements.get(TERMINAL_PANEL_ELEMENT_NAME)) {
62-
customElements.define(TERMINAL_PANEL_ELEMENT_NAME, TestTerminalPanel);
63-
}
73+
customElements.define(TERMINAL_PANEL_ELEMENT_NAME, TestTerminalPanel);
6474

6575
describe("OpenClawTerminalPanel", () => {
6676
beforeEach(async () => {

‎ui/src/pages/approval/approval-page.test.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import type { GatewayBrowserClient } from "../../api/gateway.ts";
1414
import type { ApplicationContext, ApplicationGatewaySnapshot } from "../../app/context.ts";
1515
import { applicationContext } from "../../app/context.ts";
1616
import { i18n } from "../../i18n/index.ts";
17-
import "./approval-page.ts";
17+
import { ApprovalPage } from "./approval-page.ts";
1818

19-
const PROVIDER_ELEMENT_NAME = "test-approval-page-context-provider";
19+
const TEST_ELEMENT_SUFFIX = crypto.randomUUID();
20+
const PROVIDER_ELEMENT_NAME = `test-approval-page-context-provider-${TEST_ELEMENT_SUFFIX}`;
21+
const APPROVAL_PAGE_ELEMENT_NAME = `test-openclaw-approval-page-${TEST_ELEMENT_SUFFIX}`;
2022

2123
class ApprovalPageContextProvider extends LitElement {
2224
private readonly contextProvider = new ContextProvider(this, {
@@ -28,9 +30,10 @@ class ApprovalPageContextProvider extends LitElement {
2830
}
2931
}
3032

31-
if (!customElements.get(PROVIDER_ELEMENT_NAME)) {
32-
customElements.define(PROVIDER_ELEMENT_NAME, ApprovalPageContextProvider);
33-
}
33+
// The non-isolated UI runner resets modules but not customElements. Register
34+
// the current provider and page graphs so context and locale state stay paired.
35+
customElements.define(PROVIDER_ELEMENT_NAME, ApprovalPageContextProvider);
36+
customElements.define(APPROVAL_PAGE_ELEMENT_NAME, class extends ApprovalPage {});
3437

3538
type TestApprovalPage = HTMLElement & {
3639
approvalId: string;
@@ -122,7 +125,7 @@ function createPage(params: {
122125
}) {
123126
const source = createGateway(params.client, params.connected);
124127
const provider = document.createElement(PROVIDER_ELEMENT_NAME) as ApprovalPageContextProvider;
125-
const page = document.createElement("openclaw-approval-page") as TestApprovalPage;
128+
const page = document.createElement(APPROVAL_PAGE_ELEMENT_NAME) as TestApprovalPage;
126129
provider.setContext({
127130
basePath: "",
128131
gateway: source.gateway,

0 commit comments

Comments
 (0)