From e70168212d9f76cf7db6edeaa095448caaca407d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 6 Apr 2026 17:21:43 +0100 Subject: [PATCH] refactor: dedupe script and matrix send helpers --- extensions/matrix/src/matrix/send/client.ts | 30 +++++++++++------ scripts/lib/vitest-batch-runner.mjs | 37 +++++++++++++++++++++ scripts/test-extension-batch.mjs | 37 ++------------------- scripts/test-extension.mjs | 37 ++------------------- 4 files changed, 61 insertions(+), 80 deletions(-) create mode 100644 scripts/lib/vitest-batch-runner.mjs diff --git a/extensions/matrix/src/matrix/send/client.ts b/extensions/matrix/src/matrix/send/client.ts index a8d568c4431..29789a22483 100644 --- a/extensions/matrix/src/matrix/send/client.ts +++ b/extensions/matrix/src/matrix/send/client.ts @@ -39,11 +39,7 @@ export async function withResolvedMatrixSendClient( }, run: (client: MatrixClient) => Promise, ): Promise { - if (opts.client) { - return await run(opts.client); - } - const { withResolvedRuntimeMatrixClient } = await loadMatrixSendClientRuntime(); - return await withResolvedRuntimeMatrixClient( + return await withResolvedMatrixClient( { ...opts, // One-off outbound sends still need a started client so room encryption @@ -66,11 +62,7 @@ export async function withResolvedMatrixControlClient( }, run: (client: MatrixClient) => Promise, ): Promise { - if (opts.client) { - return await run(opts.client); - } - const { withResolvedRuntimeMatrixClient } = await loadMatrixSendClientRuntime(); - return await withResolvedRuntimeMatrixClient( + return await withResolvedMatrixClient( { ...opts, readiness: "none", @@ -78,3 +70,21 @@ export async function withResolvedMatrixControlClient( run, ); } + +async function withResolvedMatrixClient( + opts: { + client?: MatrixClient; + cfg?: CoreConfig; + timeoutMs?: number; + accountId?: string | null; + readiness: "started" | "none"; + }, + run: (client: MatrixClient) => Promise, + shutdownBehavior?: "persist", +): Promise { + if (opts.client) { + return await run(opts.client); + } + const { withResolvedRuntimeMatrixClient } = await loadMatrixSendClientRuntime(); + return await withResolvedRuntimeMatrixClient(opts, run, shutdownBehavior); +} diff --git a/scripts/lib/vitest-batch-runner.mjs b/scripts/lib/vitest-batch-runner.mjs new file mode 100644 index 00000000000..16e4b934718 --- /dev/null +++ b/scripts/lib/vitest-batch-runner.mjs @@ -0,0 +1,37 @@ +import { spawn } from "node:child_process"; +import path from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const repoRoot = path.resolve(__dirname, "../.."); +const pnpm = "pnpm"; + +export async function runVitestBatch(params) { + return await new Promise((resolve, reject) => { + const child = spawn( + pnpm, + ["exec", "vitest", "run", "--config", params.config, ...params.targets, ...params.args], + { + cwd: repoRoot, + stdio: "inherit", + shell: process.platform === "win32", + env: params.env, + }, + ); + + child.on("error", reject); + child.on("exit", (code, signal) => { + if (signal) { + process.kill(process.pid, signal); + return; + } + resolve(code ?? 1); + }); + }); +} + +export function isDirectScriptRun(metaUrl) { + const entryHref = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : ""; + return metaUrl === entryHref; +} diff --git a/scripts/test-extension-batch.mjs b/scripts/test-extension-batch.mjs index 1122adae3d0..52010f15311 100644 --- a/scripts/test-extension-batch.mjs +++ b/scripts/test-extension-batch.mjs @@ -1,38 +1,7 @@ #!/usr/bin/env node -import { spawn } from "node:child_process"; -import path from "node:path"; -import { fileURLToPath, pathToFileURL } from "node:url"; import { resolveExtensionBatchPlan } from "./lib/extension-test-plan.mjs"; - -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); -const repoRoot = path.resolve(__dirname, ".."); -const pnpm = "pnpm"; - -async function runVitestBatch(params) { - return await new Promise((resolve, reject) => { - const child = spawn( - pnpm, - ["exec", "vitest", "run", "--config", params.config, ...params.targets, ...params.args], - { - cwd: repoRoot, - stdio: "inherit", - shell: process.platform === "win32", - env: params.env, - }, - ); - - child.on("error", reject); - child.on("exit", (code, signal) => { - if (signal) { - process.kill(process.pid, signal); - return; - } - resolve(code ?? 1); - }); - }); -} +import { isDirectScriptRun, runVitestBatch } from "./lib/vitest-batch-runner.mjs"; function printUsage() { console.error("Usage: pnpm test:extensions:batch [vitest args...]"); @@ -98,8 +67,6 @@ async function run() { } } -const entryHref = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : ""; - -if (import.meta.url === entryHref) { +if (isDirectScriptRun(import.meta.url)) { await run(); } diff --git a/scripts/test-extension.mjs b/scripts/test-extension.mjs index 9fbc8900ce4..d1f44c95996 100644 --- a/scripts/test-extension.mjs +++ b/scripts/test-extension.mjs @@ -1,38 +1,7 @@ #!/usr/bin/env node -import { spawn } from "node:child_process"; -import path from "node:path"; -import { fileURLToPath, pathToFileURL } from "node:url"; import { resolveExtensionTestPlan } from "./lib/extension-test-plan.mjs"; - -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); -const repoRoot = path.resolve(__dirname, ".."); -const pnpm = "pnpm"; - -async function runVitestBatch(params) { - return await new Promise((resolve, reject) => { - const child = spawn( - pnpm, - ["exec", "vitest", "run", "--config", params.config, ...params.targets, ...params.args], - { - cwd: repoRoot, - stdio: "inherit", - shell: process.platform === "win32", - env: params.env, - }, - ); - - child.on("error", reject); - child.on("exit", (code, signal) => { - if (signal) { - process.kill(process.pid, signal); - return; - } - resolve(code ?? 1); - }); - }); -} +import { isDirectScriptRun, runVitestBatch } from "./lib/vitest-batch-runner.mjs"; function printUsage() { console.error("Usage: pnpm test:extension [vitest args...]"); @@ -81,8 +50,6 @@ async function run() { process.exit(exitCode); } -const entryHref = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : ""; - -if (import.meta.url === entryHref) { +if (isDirectScriptRun(import.meta.url)) { await run(); }