Open History to see the last commit
1import "vite-plus/test/config";
2import { defineConfig, mergeConfig } from "vite-plus";
3
4import baseConfig from "../../vite.config.ts";
5import { loadRepoEnv } from "../../scripts/lib/public-config.ts";
6import rootPackageJson from "../../package.json" with { type: "json" };
7import packageJson from "./package.json" with { type: "json" };
8import { WeightedShardSequencer } from "./src/testUtils/weightedShardSequencer.ts";
9
10// NSIS install time tracks file count, not bytes.
11import {
12 isExternalCliDependency,
13 shouldBundleCliDependency,
14} from "../../scripts/lib/cli-external-packages.ts";
15
16export { shouldBundleCliDependency };
17
18const repoEnv = loadRepoEnv();
19const cliBuildChannel = /^[^-+]+-(?:nightly|preview)\./.test(packageJson.version)
20 ? "nightly"
21 : "latest";
22// Official release workflows set the telemetry channel; local builds and forks use "dev".
23const TELEMETRY_CHANNELS = ["stable", "nightly", "preview", "dev"];
24const telemetryChannel = process.env.T3CODE_TELEMETRY_CHANNEL?.trim() || "dev";
25if (!TELEMETRY_CHANNELS.includes(telemetryChannel)) {
26 throw new Error(
27 `T3CODE_TELEMETRY_CHANNEL must be one of ${TELEMETRY_CHANNELS.join(", ")}, got "${telemetryChannel}".`,
28 );
29}
30
31// tsdown SEA needs one chunk (sourcemaps count) and Node >=25.7, so package separately.
32// TEMPORARY one-time Node fix. Remove when fixed: https://github.com/nodejs/node/issues/67048
33// "1" builds dist-exe/t3 here; "bundle" stops at bin.mjs for scripts/cli.ts to inject (linux-arm64).
34const packMode = process.env.T3CODE_PACK_EXE;
35const packExecutable = packMode === "1" || packMode === "bundle";
36// TEMPORARY one-time Node fix. Remove when fixed: https://github.com/nodejs/node/issues/67048
37// No linux-arm64: --build-sea output cannot load on 16/64 KiB page kernels, so it builds natively.
38const SEA_TARGETS = {
39 "darwin-arm64": { platform: "darwin", arch: "arm64" },
40 "darwin-x64": { platform: "darwin", arch: "x64" },
41 "linux-x64": { platform: "linux", arch: "x64" },
42 "win-arm64": { platform: "win", arch: "arm64" },
43 "win-x64": { platform: "win", arch: "x64" },
44} as const;
45const packExecutableTarget = process.env.T3CODE_PACK_EXE_TARGET?.trim();
46const cliNodeVersion = rootPackageJson.engines.node;
47if (packExecutable && !packExecutableTarget && process.versions.node !== cliNodeVersion) {
48 throw new Error(
49 `Native CLI executables must be built with Node ${cliNodeVersion}; running ${process.versions.node}.`,
50 );
51}
52if (packExecutableTarget && !Object.hasOwn(SEA_TARGETS, packExecutableTarget)) {
53 throw new Error(
54 `T3CODE_PACK_EXE_TARGET must be one of ${Object.keys(SEA_TARGETS).join(", ")}, got "${packExecutableTarget}".`,
55 );
56}
57const packExecutableTargets = packExecutableTarget
58 ? [
59 {
60 ...SEA_TARGETS[packExecutableTarget as keyof typeof SEA_TARGETS],
61 nodeVersion: cliNodeVersion,
62 },
63 ]
64 : undefined;
65
66// The root config's `projects` route repo-root runs to package configs,
67// including this one. Inheriting them would make this config a container.
68const { projects: _rootTestProjects, ...baseTestConfig } = baseConfig.test ?? {};
69// Workspace cache configuration is invalid on a package config.
70const { run: _rootRunConfig, ...basePackageConfig } = baseConfig;
71
72export default mergeConfig(
73 { ...basePackageConfig, test: baseTestConfig },
74 defineConfig({
75 run: {
76 tasks: {
77 build: {
78 command: "node scripts/cli.ts build",
79 dependsOn: ["@t3tools/web#build"],
80 cache: false,
81 },
82 },
83 },
84 pack: {
85 // SEA embeds one entry; expose the history worker as a hidden subcommand.
86 entry: packExecutable ? ["src/bin.ts"] : ["src/bin.ts", "src/claude-history-worker.ts"],
87 outDir: packExecutable ? "dist-exe" : "dist",
88 sourcemap: !packExecutable,
89 clean: true,
90 // TEMPORARY one-time Node fix. Remove when fixed: https://github.com/nodejs/node/issues/67048
91 // SEA can only resolve built-ins; keep dynamic imports inside its one module.
92 ...(packExecutable ? { outputOptions: { codeSplitting: false } } : {}),
93 ...(packMode === "1"
94 ? {
95 exe: {
96 fileName: "t3",
97 outDir: "dist-exe",
98 ...(packExecutableTargets ? { targets: packExecutableTargets } : {}),
99 // Node SEA useCodeCache breaks import(), which the server needs.
100 seaConfig: { useCodeCache: false },
101 },
102 }
103 : {}),
104 deps: {
105 // alwaysBundle=false has no opinion; neverBundle must retain transitive native acceleration.
106 alwaysBundle: shouldBundleCliDependency,
107 neverBundle: (id: string) => isExternalCliDependency(id),
108 onlyBundle: false,
109 },
110 banner: {
111 js: "#!/usr/bin/env node\n",
112 },
113 define: {
114 __T3CODE_BUILD_CHANNEL__: JSON.stringify(cliBuildChannel),
115 __T3CODE_TELEMETRY_CHANNEL__: JSON.stringify(telemetryChannel),
116 __T3CODE_BUILD_RELAY_URL__: JSON.stringify(repoEnv.T3CODE_RELAY_URL?.trim() ?? ""),
117 __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: JSON.stringify(
118 repoEnv.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() ?? "",
119 ),
120 __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: JSON.stringify(
121 repoEnv.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() ?? "",
122 ),
123 __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: JSON.stringify(
124 repoEnv.T3CODE_RELAY_CLIENT_OTLP_TRACES_URL?.trim() ?? "",
125 ),
126 __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__: JSON.stringify(
127 repoEnv.T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET?.trim() ?? "",
128 ),
129 __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__: JSON.stringify(
130 repoEnv.T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN?.trim() ?? "",
131 ),
132 },
133 },
134 test: {
135 // Parallel sqlite/git/worktree/orchestration tests introduce load-sensitive flakes.
136 fileParallelism: false,
137 sequence: { sequencer: WeightedShardSequencer },
138 setupFiles: [
139 "./src/testUtils/gitConfig.setup.ts",
140 "./src/testUtils/managedServiceEnv.setup.ts",
141 ],
142 hookTimeout: 120_000,
143 testTimeout: 120_000,
144 },
145 }),
146);