From f2e1619f0352663d68cb59950042183d3e237410 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 12 Apr 2026 04:35:14 +0100 Subject: [PATCH] test(matrix): share onboarding update helpers --- .../matrix/src/onboarding.test-harness.ts | 49 +++++++++++ extensions/matrix/src/onboarding.test.ts | 86 ++++--------------- 2 files changed, 64 insertions(+), 71 deletions(-) diff --git a/extensions/matrix/src/onboarding.test-harness.ts b/extensions/matrix/src/onboarding.test-harness.ts index 233620199c1..e5711459116 100644 --- a/extensions/matrix/src/onboarding.test-harness.ts +++ b/extensions/matrix/src/onboarding.test-harness.ts @@ -205,3 +205,52 @@ export function createMatrixTokenAddAccountPrompter(params?: { onConfirm: async () => false, }); } + +export function createConfiguredMatrixTopLevelConfig(params?: { + homeserver?: string; + accessToken?: string | { source: "env"; provider: "default"; id: string }; + autoJoin?: "allowlist" | "off"; + autoJoinAllowlist?: string[]; +}): CoreConfig { + return { + channels: { + matrix: { + homeserver: params?.homeserver ?? "https://matrix.example.org", + accessToken: params?.accessToken ?? "matrix-token", + ...(params?.autoJoin ? { autoJoin: params.autoJoin } : {}), + ...(params?.autoJoinAllowlist ? { autoJoinAllowlist: params.autoJoinAllowlist } : {}), + }, + }, + } as CoreConfig; +} + +export function createMatrixUpdateKeepCredentialsPrompter(params?: { + notes?: string[]; + inviteAutoJoin?: "off" | "allowlist"; + updateAutoJoin?: boolean; + homeserver?: string; + deviceName?: string; + onText?: PromptHandler>; +}) { + return createMatrixWizardPrompter({ + notes: params?.notes, + select: { + "Matrix already configured. What do you want to do?": "update", + ...(params?.inviteAutoJoin ? { "Matrix invite auto-join": params.inviteAutoJoin } : {}), + }, + text: { + "Matrix homeserver URL": params?.homeserver ?? "https://matrix.example.org", + "Matrix device name (optional)": params?.deviceName ?? "OpenClaw Gateway", + }, + confirm: { + "Matrix credentials already configured. Keep them?": true, + "Enable end-to-end encryption (E2EE)?": false, + "Configure Matrix rooms access?": false, + "Configure Matrix invite auto-join?": params?.inviteAutoJoin !== undefined, + ...(params?.inviteAutoJoin !== undefined + ? { "Update Matrix invite auto-join?": params?.updateAutoJoin ?? true } + : {}), + }, + ...(params?.onText ? { onText: params.onText } : {}), + }); +} diff --git a/extensions/matrix/src/onboarding.test.ts b/extensions/matrix/src/onboarding.test.ts index 13a7802711b..aa0adab1ae9 100644 --- a/extensions/matrix/src/onboarding.test.ts +++ b/extensions/matrix/src/onboarding.test.ts @@ -2,8 +2,10 @@ import { describe, expect, it, vi } from "vitest"; import { matrixOnboardingAdapter } from "./onboarding.js"; import { createConfiguredMatrixDefaultAccountConfig, + createConfiguredMatrixTopLevelConfig, createLegacyMatrixTopLevelConfig, createMatrixTokenAddAccountPrompter, + createMatrixUpdateKeepCredentialsPrompter, installMatrixOnboardingEnvRestoreHooks, createMatrixWizardPrompter, runMatrixAddAccountAllowlistConfigure, @@ -289,30 +291,13 @@ describe("matrix onboarding", () => { process.env.MATRIX_ACCESS_TOKEN = "env-token"; - const prompter = createMatrixWizardPrompter({ - select: { - "Matrix already configured. What do you want to do?": "update", - }, - text: { - "Matrix homeserver URL": "https://matrix.example.org", - "Matrix device name (optional)": "OpenClaw Gateway", - }, - confirm: { - "Matrix credentials already configured. Keep them?": true, - "Enable end-to-end encryption (E2EE)?": false, - "Configure Matrix rooms access?": false, - "Configure Matrix invite auto-join?": false, - }, - }); + const prompter = createMatrixUpdateKeepCredentialsPrompter(); const result = await runMatrixInteractiveConfigure({ cfg: { - channels: { - matrix: { - homeserver: "https://matrix.example.org", - accessToken: { source: "env", provider: "default", id: "MATRIX_ACCESS_TOKEN" }, - }, - }, + ...createConfiguredMatrixTopLevelConfig({ + accessToken: { source: "env", provider: "default", id: "MATRIX_ACCESS_TOKEN" }, + }), secrets: { defaults: { env: "default", @@ -418,36 +403,16 @@ describe("matrix onboarding", () => { installMatrixTestRuntime(); const notes: string[] = []; - const prompter = createMatrixWizardPrompter({ + const prompter = createMatrixUpdateKeepCredentialsPrompter({ notes, - select: { - "Matrix already configured. What do you want to do?": "update", - "Matrix invite auto-join": "off", - }, - text: { - "Matrix homeserver URL": "https://matrix.example.org", - "Matrix device name (optional)": "OpenClaw Gateway", - }, - confirm: { - "Matrix credentials already configured. Keep them?": true, - "Enable end-to-end encryption (E2EE)?": false, - "Configure Matrix rooms access?": false, - "Configure Matrix invite auto-join?": true, - "Update Matrix invite auto-join?": true, - }, + inviteAutoJoin: "off", }); const result = await runMatrixInteractiveConfigure({ - cfg: { - channels: { - matrix: { - homeserver: "https://matrix.example.org", - accessToken: "matrix-token", - autoJoin: "allowlist", - autoJoinAllowlist: ["#ops:example.org"], - }, - }, - } as CoreConfig, + cfg: createConfiguredMatrixTopLevelConfig({ + autoJoin: "allowlist", + autoJoinAllowlist: ["#ops:example.org"], + }), prompter, configured: true, }); @@ -470,23 +435,9 @@ describe("matrix onboarding", () => { const notes: string[] = []; let inviteAllowlistPrompts = 0; - const prompter = createMatrixWizardPrompter({ + const prompter = createMatrixUpdateKeepCredentialsPrompter({ notes, - select: { - "Matrix already configured. What do you want to do?": "update", - "Matrix invite auto-join": "allowlist", - }, - text: { - "Matrix homeserver URL": "https://matrix.example.org", - "Matrix device name (optional)": "OpenClaw Gateway", - }, - confirm: { - "Matrix credentials already configured. Keep them?": true, - "Enable end-to-end encryption (E2EE)?": false, - "Configure Matrix rooms access?": false, - "Configure Matrix invite auto-join?": true, - "Update Matrix invite auto-join?": true, - }, + inviteAutoJoin: "allowlist", onText: async (message) => { if (message === "Matrix invite auto-join allowlist (comma-separated)") { inviteAllowlistPrompts += 1; @@ -497,14 +448,7 @@ describe("matrix onboarding", () => { }); const result = await runMatrixInteractiveConfigure({ - cfg: { - channels: { - matrix: { - homeserver: "https://matrix.example.org", - accessToken: "matrix-token", - }, - }, - } as CoreConfig, + cfg: createConfiguredMatrixTopLevelConfig(), prompter, configured: true, });