Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Stop a new import in a module under test breaking the tests that load it (#10186)

Daniel Hane84ef62ef3a6
diff --git a/studio/frontend/tests/edit-reply-keeps-tool-order.test.ts b/studio/frontend/tests/edit-reply-keeps-tool-order.test.ts
index 6fb39effcb1..2bcbdc0148b 100644
--- a/studio/frontend/tests/edit-reply-keeps-tool-order.test.ts
+++ b/studio/frontend/tests/edit-reply-keeps-tool-order.test.ts
@@ -51,8 +51,23 @@ function harness() {
saved.push(record);
return record;
},
+ // delete-thread-message loads for real under the passthrough below, and it
+ // reads the thread back; nothing here drives that path.
+ listChatMessages: async () => [],
+ },
+ "@assistant-ui/core/internal": { MessageRepository: class {} },
+ // The persistence boundary. Cut here so the passthrough stops before db.ts and
+ // its IndexedDB driver, which no node test can load: this suite is about the
+ // record's shape and part order, not about storing it.
+ "./chat-history-storage": {
+ ensureStoredChatThread: async () => undefined,
+ syncStoredChatMessages: async () => undefined,
},
},
+ // The record this test asserts on is built by the real exportedItemToRecord, and
+ // the metadata it strips comes from the real RESEARCH_METADATA_KEYS, so those
+ // siblings load rather than being faked.
+ { relativePassthrough: true },
);
return { module, saved };
}
diff --git a/studio/frontend/tests/helpers/module-stubs.ts b/studio/frontend/tests/helpers/module-stubs.ts
index a09dee7ebce..b7548aacc8b 100644
--- a/studio/frontend/tests/helpers/module-stubs.ts
+++ b/studio/frontend/tests/helpers/module-stubs.ts
@@ -4,8 +4,8 @@
// Kept out of kit.ts on purpose, like tsx-ast.ts: only the few tests that drive a
// component or a hook directly should pay for loading the TypeScript compiler.
-import { readFileSync } from "node:fs";
-import { fileURLToPath } from "node:url";
+import { existsSync, readFileSync } from "node:fs";
+import { fileURLToPath, pathToFileURL } from "node:url";
import ts from "typescript";
@@ -15,6 +15,16 @@ export type StubElement = {
props: Record<string, unknown>;
};
+/** The sibling a relative specifier names, trying the extensions src actually uses. */
+function resolveSibling(fromPath: string, specifier: string): URL {
+ const base = new URL(specifier, pathToFileURL(fromPath));
+ for (const suffix of [".ts", ".tsx", "/index.ts", "/index.tsx", ""]) {
+ const candidate = new URL(base.href + suffix);
+ if (existsSync(fileURLToPath(candidate))) return candidate;
+ }
+ throw new Error(`cannot resolve import "${specifier}" from ${fromPath}`);
+}
+
/**
* Runs one shipped src module with every import replaced by a stub, so a test can
* call the real source with dependencies it controls. Bare node cannot import a
@@ -23,6 +33,7 @@ export type StubElement = {
export function loadWithStubs<T>(
moduleUrl: URL,
stubs: Record<string, unknown>,
+ options: { relativePassthrough?: boolean } = {},
): T {
const path = fileURLToPath(moduleUrl);
const { outputText } = ts.transpileModule(readFileSync(path, "utf8"), {
@@ -36,6 +47,14 @@ export function loadWithStubs<T>(
const loaded = { exports: {} as T };
const requireStub = (specifier: string): unknown => {
if (specifier in stubs) return stubs[specifier];
+ // Opt-in: load a sibling source for real, with the same stubs, instead of failing.
+ // A stub map names the module's imports, so without this every import ADDED to the
+ // module under test breaks each test that loads it, even when the new import is
+ // irrelevant to what that test drives. Off by default: a test that means to cut a
+ // dependency off still wants the throw.
+ if (options.relativePassthrough && specifier.startsWith(".")) {
+ return loadWithStubs(resolveSibling(path, specifier), stubs, options);
+ }
throw new Error(`no stub for import "${specifier}" in ${path}`);
};
new Function("require", "module", "exports", outputText)(