From 3f911c120c9492e6f74ce27afa504f8f40c7e9fb Mon Sep 17 00:00:00 2001 From: "arrz.dev" <55147716+arrzdev@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:45:33 +0000 Subject: [PATCH 1/3] test(routes): every route dsl overload returns the node upstream's would --- src/routes/adaptv-routes.test.ts | 154 +++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 src/routes/adaptv-routes.test.ts diff --git a/src/routes/adaptv-routes.test.ts b/src/routes/adaptv-routes.test.ts new file mode 100644 index 00000000..fce4768a --- /dev/null +++ b/src/routes/adaptv-routes.test.ts @@ -0,0 +1,154 @@ +// @vitest-environment node +// The route DSL is a façade over `@tanstack/virtual-file-routes`: the generator +// reads the nodes it returns, so every overload must hand back exactly the node +// upstream's would. The one place adaptv has an opinion is `rootRoute`, which +// supplies the root file itself when the app passes only children. +import * as upstream from "@tanstack/virtual-file-routes" +import { afterEach, beforeEach, describe, expect, it } from "vitest" +import type { RouteNode } from "#adaptv/routes/adaptv-routes.ts" +import { + index, + layout, + physical, + rootRoute, + route, +} from "#adaptv/routes/adaptv-routes.ts" + +const ROOT_FILE = "../../node_modules/adaptv/src/routes/root-route.tsx" + +let saved: string | undefined +beforeEach(() => { + saved = process.env.ADAPTV_ROOT_ROUTE_FILE + delete process.env.ADAPTV_ROOT_ROUTE_FILE +}) +afterEach(() => { + if (saved === undefined) delete process.env.ADAPTV_ROOT_ROUTE_FILE + else process.env.ADAPTV_ROOT_ROUTE_FILE = saved +}) + +describe("rootRoute", () => { + it("wires in adaptv's root file when given only children", () => { + process.env.ADAPTV_ROOT_ROUTE_FILE = ROOT_FILE + const children = [index("pages/home.tsx")] + expect(rootRoute(children)).toEqual({ + type: "root", + file: ROOT_FILE, + children, + }) + }) + + it("wires in adaptv's root file with no arguments at all", () => { + process.env.ADAPTV_ROOT_ROUTE_FILE = ROOT_FILE + expect(rootRoute()).toEqual(upstream.rootRoute(ROOT_FILE)) + }) + + it("throws a named error when evaluated outside the Vite plugin", () => { + expect(() => rootRoute([])).toThrow(/ADAPTV_ROOT_ROUTE_FILE is unset/) + }) + + it("treats an empty variable as unset rather than resolving to ''", () => { + process.env.ADAPTV_ROOT_ROUTE_FILE = "" + expect(() => rootRoute([])).toThrow(/ADAPTV_ROOT_ROUTE_FILE is unset/) + }) + + it("uses an ejected root file as given and never reads the variable", () => { + const children = [index("pages/home.tsx")] + expect(rootRoute("layouts/_root.tsx", children)).toEqual( + upstream.rootRoute("layouts/_root.tsx", children), + ) + expect(rootRoute("layouts/_root.tsx")).toEqual({ + type: "root", + file: "layouts/_root.tsx", + }) + }) +}) + +describe("the non-root nodes match upstream's", () => { + const kids: RouteNode[] = [index("pages/a.tsx")] + + it("index", () => { + expect(index("pages/home.tsx")).toEqual({ + type: "index", + file: "pages/home.tsx", + }) + }) + + it("layout(file, children)", () => { + expect(layout("layouts/app.tsx", kids)).toEqual( + upstream.layout("layouts/app.tsx", kids), + ) + expect(layout("layouts/app.tsx", kids)).not.toHaveProperty("id") + }) + + it("layout(id, file, children)", () => { + expect(layout("app", "layouts/app.tsx", kids)).toEqual({ + type: "layout", + id: "app", + file: "layouts/app.tsx", + children: kids, + }) + }) + + it("layout(id, file) from untyped code gets an empty children list", () => { + //the overloads require children; a .js route config can still omit them + const untyped = layout as unknown as ( + id: string, + file: string, + ) => RouteNode + expect(untyped("app", "layouts/app.tsx")).toEqual({ + type: "layout", + id: "app", + file: "layouts/app.tsx", + children: [], + }) + }) + + it("route(path, children) has no file", () => { + expect(route("/settings", kids)).toEqual( + upstream.route("/settings", kids), + ) + expect(route("/settings", kids)).not.toHaveProperty("file") + }) + + it("route(path, file) has no children", () => { + expect(route("/settings", "pages/settings.tsx")).toEqual({ + type: "route", + path: "/settings", + file: "pages/settings.tsx", + }) + }) + + it("route(path, file, children) keeps both, even an empty list", () => { + expect(route("/s", "pages/s.tsx", kids)).toEqual( + upstream.route("/s", "pages/s.tsx", kids), + ) + expect(route("/s", "pages/s.tsx", [])).toEqual({ + type: "route", + path: "/s", + file: "pages/s.tsx", + children: [], + }) + }) + + it("physical(directory) mounts at this level", () => { + expect(physical("blog")).toEqual({ + type: "physical", + directory: "blog", + pathPrefix: "", + }) + }) + + it("physical(prefix, directory) mounts at the prefix", () => { + expect(physical("/blog", "blog")).toEqual( + upstream.physical("/blog", "blog"), + ) + expect(physical("/blog", "blog")).toMatchObject({ + pathPrefix: "/blog", + }) + }) + + it("physical with an empty directory still takes the two-argument form", () => { + //`maybeDirectory === undefined`, not falsiness, picks the overload + expect(physical("/blog", "")).toEqual(upstream.physical("/blog", "")) + }) +}) From 71ec677902652fb2bda98faef722bfa273a5d5b5 Mon Sep 17 00:00:00 2001 From: "arrz.dev" <55147716+arrzdev@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:45:33 +0000 Subject: [PATCH 2/3] test(native): pin the installed plugin enumeration and the native code check --- src/native/installed-plugins.test.ts | 236 +++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 src/native/installed-plugins.test.ts diff --git a/src/native/installed-plugins.test.ts b/src/native/installed-plugins.test.ts new file mode 100644 index 00000000..443ffe12 --- /dev/null +++ b/src/native/installed-plugins.test.ts @@ -0,0 +1,236 @@ +// @vitest-environment node +// One enumeration feeds two consumers with opposite tolerances (see the table in +// installed-plugins.ts), so these tests pin both halves separately: the list is +// wide (adaptv's own dependencies first, then everything the app names), and +// `carriesNativeCode` is narrow (a `capacitor` field AND a platform tree). +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import path from "node:path" +import { afterEach, describe, expect, it, vi } from "vitest" +import { + adaptvRoots, + carriesNativeCode, + installedPlugins, + pluginDir, + readPackageJson, +} from "#adaptv/native/installed-plugins.ts" + +const dirs: string[] = [] +afterEach(() => { + for (const dir of dirs.splice(0)) + rmSync(dir, { recursive: true, force: true }) + vi.doUnmock("#adaptv/vite/package-files.ts") + vi.doUnmock("node:url") + vi.resetModules() + vi.restoreAllMocks() +}) + +function tempDir(): string { + const dir = mkdtempSync(path.join(tmpdir(), "adaptv-plugins-")) + dirs.push(dir) + return dir +} + +function writePackage(dir: string, pkg: object): string { + mkdirSync(dir, { recursive: true }) + writeFileSync(path.join(dir, "package.json"), JSON.stringify(pkg)) + return dir +} + +describe("readPackageJson", () => { + it("is null for no directory, a missing file and broken JSON", () => { + const dir = tempDir() + expect(readPackageJson(null)).toBeNull() + expect(readPackageJson(dir)).toBeNull() + writeFileSync(path.join(dir, "package.json"), "{ not json") + expect(readPackageJson(dir)).toBeNull() + }) + + it("parses a real manifest", () => { + const dir = writePackage(tempDir(), { version: "1.2.3" }) + expect(readPackageJson(dir)).toEqual({ version: "1.2.3" }) + }) +}) + +describe("adaptvRoots", () => { + it("tries the given root first, then the package's own, then the cwd", () => { + const roots = adaptvRoots("/given") + expect(roots[0]).toBe("/given") + expect(roots.at(-1)).toBe(process.cwd()) + expect(roots).toHaveLength(3) + }) + + it("leaves out an absent given root", () => { + expect(adaptvRoots(undefined)).toHaveLength(2) + }) + + it("falls back to the cwd when the package cannot locate itself", async () => { + vi.doMock("#adaptv/vite/package-files.ts", () => ({ + adaptvPackageRoot: () => { + throw new Error("no file: URL under this loader") + }, + })) + const fresh = await import("#adaptv/native/installed-plugins.ts") + expect(fresh.adaptvRoots(undefined)).toEqual([process.cwd()]) + expect(fresh.adaptvRoots("/given")).toEqual(["/given", process.cwd()]) + }) +}) + +describe("installedPlugins", () => { + it("starts with adaptv's own dependencies, whatever their vendor", () => { + const adaptvRoot = writePackage(tempDir(), { + dependencies: { + "@capacitor/device": "1", + "@capawesome/capacitor-live-update": "1", + }, + }) + const app = writePackage(tempDir(), {}) + expect(installedPlugins(app, { adaptvRoot })).toEqual([ + "@capacitor/device", + "@capawesome/capacitor-live-update", + ]) + }) + + it("reads only the first adaptv root that has dependencies", () => { + //the given root wins; the real package's long list must not be merged in + const adaptvRoot = writePackage(tempDir(), { + dependencies: { "@capacitor/device": "1" }, + }) + const app = writePackage(tempDir(), {}) + expect(installedPlugins(app, { adaptvRoot })).toEqual([ + "@capacitor/device", + ]) + }) + + it("skips a given root with no dependencies and reads the next one", () => { + const adaptvRoot = writePackage(tempDir(), { name: "empty" }) + const app = writePackage(tempDir(), {}) + //the next root is this checkout, which bundles Capacitor + expect(installedPlugins(app, { adaptvRoot })).toContain( + "@capacitor/preferences", + ) + }) + + it("adds the app's dependencies, dev dependencies and config plugins, once each", () => { + const adaptvRoot = writePackage(tempDir(), { + dependencies: { "@capacitor/device": "1" }, + }) + const app = writePackage(tempDir(), { + dependencies: { "@capacitor/camera": "1", "@capacitor/device": "1" }, + devDependencies: { vitest: "1" }, + }) + expect( + installedPlugins(app, { + adaptvRoot, + plugins: ["@vendor/scanner", "@capacitor/camera"], + }), + ).toEqual([ + "@capacitor/device", + "@capacitor/camera", + "vitest", + "@vendor/scanner", + ]) + }) + + it("still lists adaptv's plugins for an app with no package.json and no input", () => { + const list = installedPlugins(tempDir(), undefined) + expect(list).toContain("@capacitor/device") + expect(list).toContain("@capawesome/capacitor-live-update") + }) +}) + +describe("pluginDir", () => { + it("finds a plugin the app installed", () => { + const app = writePackage(tempDir(), {}) + const dir = writePackage( + path.join(app, "node_modules/@vendor/scanner"), + { + name: "@vendor/scanner", + }, + ) + expect(pluginDir("@vendor/scanner", app, undefined)).toBe(dir) + }) + + it("prefers the app's copy over adaptv's", () => { + const app = writePackage(tempDir(), {}) + const adaptvRoot = writePackage(tempDir(), {}) + const appCopy = writePackage(path.join(app, "node_modules/shared"), { + name: "shared", + }) + writePackage(path.join(adaptvRoot, "node_modules/shared"), { + name: "shared", + }) + expect(pluginDir("shared", app, adaptvRoot)).toBe(appCopy) + }) + + it("falls back to a plugin only adaptv can see", () => { + const app = writePackage(tempDir(), {}) + const adaptvRoot = writePackage(tempDir(), {}) + const dir = writePackage( + path.join(adaptvRoot, "node_modules/@capacitor/x"), + { + name: "@capacitor/x", + }, + ) + expect(pluginDir("@capacitor/x", app, adaptvRoot)).toBe(dir) + }) + + it("resolves from the module's own tree when no adaptv root is given", () => { + const app = writePackage(tempDir(), {}) + expect(pluginDir("@capacitor/device", app, undefined)).toMatch( + /@capacitor[/\\]device$/, + ) + }) + + it("resolves from the cwd when the module URL is not a file path", async () => { + //what `bin/lib/load-ts.mjs` hands out: a `data:` URL + vi.doMock("node:url", async (original) => ({ + ...(await original()), + fileURLToPath: () => { + throw new TypeError("The URL must be of scheme file") + }, + })) + const fresh = await import("#adaptv/native/installed-plugins.ts") + const app = writePackage(tempDir(), {}) + //found through `process.cwd()`, which is this checkout + expect(fresh.pluginDir("@capacitor/device", app, undefined)).toMatch( + /@capacitor[/\\]device$/, + ) + }) + + it("is null when neither root can see the package", () => { + const app = writePackage(tempDir(), {}) + const adaptvRoot = writePackage(tempDir(), {}) + expect(pluginDir("@vendor/absent", app, adaptvRoot)).toBeNull() + }) +}) + +describe("carriesNativeCode", () => { + function plugin(pkg: object, platforms: string[]): string { + const dir = writePackage(tempDir(), pkg) + for (const p of platforms) mkdirSync(path.join(dir, p)) + return dir + } + + it("needs a capacitor field and a platform tree", () => { + expect(carriesNativeCode(plugin({ capacitor: {} }, ["ios"]))).toBe( + true, + ) + expect(carriesNativeCode(plugin({ capacitor: {} }, ["android"]))).toBe( + true, + ) + }) + + it("is false for the field alone, like @capacitor/cli", () => { + expect(carriesNativeCode(plugin({ capacitor: {} }, []))).toBe(false) + }) + + it("is false for a platform folder without the field", () => { + expect(carriesNativeCode(plugin({}, ["ios", "android"]))).toBe(false) + }) + + it("is false for no directory or an unreadable manifest", () => { + expect(carriesNativeCode(null)).toBe(false) + expect(carriesNativeCode(tempDir())).toBe(false) + }) +}) From 28eea4d1526ae3f95e560eed9e488f815a85400b Mon Sep 17 00:00:00 2001 From: "arrz.dev" <55147716+arrzdev@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:45:33 +0000 Subject: [PATCH 3/3] test(config): pin resolveThemeColors, defineApp and the router build keys --- src/config/app-config.test.ts | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/config/app-config.test.ts diff --git a/src/config/app-config.test.ts b/src/config/app-config.test.ts new file mode 100644 index 00000000..9c168354 --- /dev/null +++ b/src/config/app-config.test.ts @@ -0,0 +1,70 @@ +// @vitest-environment node +// app-config.ts is the config's types plus three runtime values: `defineApp`, +// `ROUTER_BUILD_KEYS` and `resolveThemeColors`. The loader and error tests +// reach them through a whole config; these pin each one directly. +import { describe, expect, it } from "vitest" +import type { AdaptvThemeColor } from "#adaptv/config/app-config.ts" +import { + defineApp, + ROUTER_BUILD_KEYS, + resolveThemeColors, +} from "#adaptv/config/app-config.ts" + +describe("defineApp", () => { + it("returns the very object it was given", () => { + const config = { + name: "Todos", + description: "A list", + themeColor: { light: "#fff" }, + styles: "./src/styles/main.css", + router: {}, + } + expect(defineApp(config)).toBe(config) + }) +}) + +describe("ROUTER_BUILD_KEYS", () => { + it("names exactly the router keys adaptv keeps out of createRouter", () => { + //`render` is a top-level key now; it must not come back here + expect(ROUTER_BUILD_KEYS).toEqual([ + "serverEntry", + "routesDirectory", + "routerConfig", + ]) + }) +}) + +describe("resolveThemeColors", () => { + it("keeps both sides when both are given", () => { + expect(resolveThemeColors({ light: "#fff", dark: "#000" })).toEqual({ + light: "#fff", + dark: "#000", + }) + }) + + it("uses the light colour for both when only light is given", () => { + expect(resolveThemeColors({ light: "#fff" })).toEqual({ + light: "#fff", + dark: "#fff", + }) + }) + + it("uses the dark colour for both when only dark is given", () => { + expect(resolveThemeColors({ dark: "#000" })).toEqual({ + light: "#000", + dark: "#000", + }) + }) + + it("throws when neither side is set", () => { + //a JS config (or `as any`) can get past the type + for (const themeColor of [ + {}, + { light: undefined, dark: undefined }, + { light: "", dark: "" }, + ]) + expect(() => + resolveThemeColors(themeColor as unknown as AdaptvThemeColor), + ).toThrow("`themeColor` needs at least one of `light` / `dark`") + }) +})