Files
windmill/cli/test/test_backend.ts
Pyra 9643006f1e feat(cli): better stale scripts detection #3 (#8480)
* fix

Signed-off-by: pyranota <pyra@duck.com>

* reduce tests

Signed-off-by: pyranota <pyra@duck.com>

* update

Signed-off-by: pyranota <pyra@duck.com>

* fix

Signed-off-by: pyranota <pyra@duck.com>

* update

Signed-off-by: pyranota <pyra@duck.com>

* WIP: stash changes after merge with origin/main

* Delete backend/parsers/windmill-parser-wasm/Cargo.lock

* reset cargo.toml

* feat(cli): integrate dependency tree into generate-metadata command

- Add isDirectlyStale field to DependencyNode for staleness tracking
- Update addScript to accept itemType, folder, isRawApp, isDirectlyStale
- Update propagateStaleness to use isDirectlyStale field instead of parameter
- Handlers now determine staleness and pass it to tree.addScript
- generate-metadata calls propagateStaleness() and populates staleItems from tree
- Pass legacyBehaviour=false and tree to handlers during generation phase

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix(cli): store originalPath in tree for correct handler invocation

Scripts need the path with extension to be passed to the handler.
Added originalPath field to DependencyNode to track this.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix parsers

Signed-off-by: pyranota <pyra@duck.com>

* rever sqlx removal

* update sqlx

* feat: make py-imports parser WASM-compatible and add as separate WASM package

Gate heavy deps (sqlx, windmill-common, async-recursion, toml, pep440_rs,
tracing) behind cfg(not(wasm32)). Make parse_code_for_imports,
parse_relative_imports, NImport, and ImportPin public. Remove duplicate
import_parser from parser-py (reset to origin/main). Add py-imports-parser
feature to windmill-parser-wasm and py-imports target to build.nu.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* safer return

* update

* fix: CLI metadata fixes - folder filter, staleness detection, WASM py-imports setup

- Fix lazy_static cfg gating for WASM compatibility (split into separate blocks)
- Fix folder argument filter to match specific file paths (not just directories)
- Fix staleness detection to use checkHash with conf (includes module hashes)
- Convert relative_imports_skip tests from Deno to bun APIs
- Add windmill-parser-wasm-py-imports to CLI and build-npm dependencies
- Relax module stale test to not require per-module change detail in output

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: restore temp_script_refs parameter in parse_python_imports

Re-adds the temp_script_refs parameter that was lost when resetting
py-imports crate to origin/main. This enables resolving relative imports
from not-yet-deployed scripts during CLI lock generation.

* fixes

* extend testsuit

* update ee repo ref

* fix: diff endpoint bytea cast, upload only mismatched scripts

- Add POST /scripts/raw_temp/diff endpoint to batch-compare local content
  hashes against deployed versions using Postgres sha256()
- Use convert_to(content, 'UTF8') instead of content::bytea to avoid
  failure on scripts containing backslash sequences (e.g. \n)
- CLI now diffs all scripts against deployed, uploads only mismatched ones
- propagateStaleness no longer deletes non-stale nodes (needed for diff)
- Suppress verbose log.info messages during metadata generation
- Add E2E tests for locally modified and unpushed helper scripts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* rework

* sqlx

* fixes

* add index

* expand tests

* fix flows

* archive script before executing

* disable tests for ci

* skip Python-dependent E2E tests on CI

Tests requiring the python backend feature are skipped when
CI_MINIMAL_FEATURES=true since CI builds with zip-only features.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: make flow fixture lock optional and reset nonDottedPaths after tests

Flow fixtures no longer emit an empty lock file by default. The lockContent
parameter controls whether a lock: "!inline ..." line appears in flow.yaml.
This prevents flows from appearing "up-to-date" when they should be processed
by generate-metadata.

Also adds afterAll to reset setNonDottedPaths(false) so global state doesn't
leak between test files when run together.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* debug: add error logging in withTestBackend to diagnose CI failures

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* debug: add --bail 1 to CI test runner to show full error on first failure

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* debug: include CLI stdout/stderr in assertion message for workspace deps test

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: set WMDEBUG_FORCE_V0_WORKSPACE_DEPENDENCIES in test backend

The workspace deps feature requires workers to report their version, but
in test/CI there are no separate workers (standalone mode). The version
check fails because workers haven't had time to ping yet. Setting this
env var bypasses the version check.

Also reverts --bail 1 from CI workflow now that the root cause is fixed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* debug: add --bail 1 to Windows CI and assertion messages for Windows failure diagnosis

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: replace TEMP_SCRIPT_REFS_PLACEHOLDER in bun builder tests

The loader.bun.js now includes a TEMP_SCRIPT_REFS_PLACEHOLDER that must
be replaced before execution. The builder tests were missing this
replacement, causing all 6 bun_builder_tests to fail.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: use cdirFwd in Windows loader filterLoad regex

Raw cdir (with backslashes) interpolated into RegExp causes \r to
become carriage return and \w to become word-char, so filterLoad
never matches main.ts. This prevents replaceRelativeImports from
running, leaving bare relative imports like "./script_b" in the
bundled output, which scanImports then misparses as package ".".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: Windows filterLoad regex + graceful fallback for old backends

- Fix filterLoad in loader.bun.windows.js to match both native backslash
  and forward-slash paths from Bun's resolver by escaping cdir for regex
- Wrap uploadScripts in try/catch so generate-metadata degrades gracefully
  when the backend lacks /raw_temp endpoints (locks use deployed versions)
- Add TODO for missing TEMP_SCRIPT_REFS support in Windows loader

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* debug: add loader/builder debug logging for Windows CI diagnosis

Temporary console.log statements to understand:
- What path Bun passes to onLoad for main.ts
- Whether filterLoad regex matches
- Whether replaceRelativeImports fires
- What the bundled output contains
- What imports scanImports extracts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: trigger CI for cli path

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: trigger CI via workflow file change

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Add TEMP_SCRIPT_REFS to Windows loader, use .ts extensions in test imports

- Add TEMP_SCRIPT_REFS_PLACEHOLDER support to loader.bun.windows.js
  (mirrors loader.bun.js) so CLI lock generation can resolve imports
  from locally-modified scripts on Windows
- Use .ts extensions in all test relative imports to work around the
  Windows filterLoad regex bug (replaceRelativeImports doesn't fire
  on Windows, so extensionless imports fail)
- Remove unused uploadSucceeded variable

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Remove debug logging from loader_builder.bun.js

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Remove windmill-parser-wasm-py-imports from frontend package.json

This dependency is only needed by the CLI, not the frontend.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* debug: add temp_script_refs logging for Windows CI investigation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* ci: remove --bail 1 from Windows CLI tests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: normalize backslashes in folder filter treePath lookup (Windows)

On Windows, item.path (originalPath) uses backslashes but tree keys
use forward slashes. The isRelevant filter's touchesFolder call
passed the unnormalized path to traverseTransitive, which couldn't
find the node. This caused cross-folder importers to be excluded
from generate-metadata when a folder argument was specified.

Also removes debug logging from previous commit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Update cli-tests.yml

* fix: normalize backslashes in strict-folder-boundaries warning message (Windows)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* chore: update ee-repo-ref to fe8f0d1d7448464c98474d994e6492c0a45e8e38

This commit updates the EE repository reference after PR #467 was merged in windmill-ee-private.

Previous ee-repo-ref: 03e6eaf950776c96b9581848a583af9ad735be60

New ee-repo-ref: fe8f0d1d7448464c98474d994e6492c0a45e8e38

Automated by sync-ee-ref workflow.

* revert cli-tests.yml

---------

Signed-off-by: pyranota <pyra@duck.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
2026-03-23 18:20:19 +00:00

705 lines
20 KiB
TypeScript

/**
* Unified Test Backend Interface
*
* Provides a common interface for both Docker-based and Cargo-based backends.
* Use environment variable TEST_BACKEND to switch:
* - TEST_BACKEND=cargo (default) - Uses pre-built binary + local postgres
* - TEST_BACKEND=docker - Uses docker-compose (legacy)
*
* Prerequisites for cargo backend:
* - PostgreSQL running locally (default: postgres://postgres:changeme@localhost:5432)
* - Backend built: cd backend && cargo build --release (or debug)
*
* Usage:
* import { withTestBackend, cleanupTestBackend } from "./test_backend.ts";
*
* test("my test", async () => {
* await withTestBackend(async (backend, tempDir) => {
* const result = await backend.runCLICommand(["sync", "pull"], tempDir);
* // ...
* });
* });
*
* CROSS-LINKS - Related test helper locations (keep in sync when adding new helpers):
* @see test_fixtures.ts - Local file fixtures (createLocalScript, createLocalFlow, etc.)
* @see sync_pull_push.test.ts - Local fixtures + createRemoteScript (API-based)
*
* This file contains: API-based creation helpers (createTestApp, createTestResource,
* createAppWithInlineScript, createFlowWithInlineScript, etc.)
* If you add new helpers, update cross-links in the files above.
*/
import { CargoBackend, CargoBackendConfig } from "./cargo_backend.ts";
import { ContainerizedBackend, ContainerConfig } from "./containerized_backend.ts";
import { mkdtemp, rm } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
/**
* Common interface for test backends
*/
export interface TestBackend {
readonly baseUrl: string;
readonly workspace: string;
readonly testConfigDir: string;
readonly token?: string;
start(): Promise<void>;
stop(): Promise<void>;
reset(): Promise<void>;
createCLICommand(args: string[], workingDir: string, opts?: { workspace?: string; token?: string }): any;
runCLICommand(args: string[], workingDir: string, opts?: { workspace?: string; token?: string }): Promise<{
stdout: string;
stderr: string;
code: number;
}>;
// Optional methods that may not be on all backends
apiRequest?(path: string, options?: RequestInit): Promise<Response>;
seedTestData?(): Promise<void>;
getWorkspaceSettings?(): Promise<any>;
updateGitSyncConfig?(config: any): Promise<void>;
createAdditionalGitRepo?(repoPath: string, description: string): Promise<void>;
listAllScripts?(): Promise<any[]>;
listAllApps?(): Promise<any[]>;
listAllResources?(): Promise<any[]>;
listAllVariables?(): Promise<any[]>;
// Methods for creating apps and flows with custom inline scripts
createAppWithInlineScript?(path: string, inlineScriptContent: string, language?: string): Promise<void>;
createFlowWithInlineScript?(path: string, inlineScriptContent: string, language?: string): Promise<void>;
}
/**
* Adapter to make CargoBackend implement TestBackend
*/
class CargoBackendAdapter implements TestBackend {
private backend: CargoBackend;
constructor(config?: Partial<CargoBackendConfig>) {
this.backend = new CargoBackend(config);
}
get baseUrl(): string {
return this.backend.baseUrl;
}
get workspace(): string {
return this.backend.workspace;
}
get testConfigDir(): string {
return this.backend.testConfigDir;
}
get token(): string {
return this.backend.authToken;
}
async start(): Promise<void> {
await this.backend.start();
}
async stop(): Promise<void> {
await this.backend.stop();
}
async reset(): Promise<void> {
await this.backend.reset();
}
createCLICommand(args: string[], workingDir: string, opts?: { workspace?: string; token?: string }): any {
return this.backend.createCLICommand(args, workingDir, opts);
}
async runCLICommand(args: string[], workingDir: string, opts?: { workspace?: string; token?: string }) {
return this.backend.runCLICommand(args, workingDir, opts);
}
async apiRequest(path: string, options?: RequestInit): Promise<Response> {
return this.backend.apiRequest(path, options);
}
/** Seeds test data via API calls. See file header for cross-links to related helpers. */
async seedTestData(): Promise<void> {
// Create test folder first
await this.createTestFolder("test");
// Create test resources and variables
await this.createTestResource("f/test/my_resource", "Test resource description");
await this.createTestVariable("f/test/my_variable", "Test variable value");
// Create test group
await this.createTestGroup("test_group");
// Create a test app
await this.createTestApp("f/test/test_dashboard");
}
/** See file header for cross-links to related helpers. */
private async createTestApp(path: string): Promise<void> {
const response = await this.backend.apiRequest(`/api/w/${this.workspace}/apps/create`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
path,
value: {
type: "app",
grid: [],
hiddenInlineScripts: [],
css: {},
norefreshbar: false,
},
summary: "Test app",
policy: {
on_behalf_of: null,
on_behalf_of_email: null,
triggerables: {},
execution_mode: "viewer",
},
}),
});
if (!response.ok) {
const error = await response.text();
if (!error.includes("already exists")) {
console.warn(`Warning: Failed to create app ${path}: ${error}`);
}
} else {
await response.text();
}
}
/** See file header for cross-links to related helpers. */
private async createTestFolder(name: string): Promise<void> {
const response = await this.backend.apiRequest(`/api/w/${this.workspace}/folders/create`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name }),
});
if (!response.ok) {
const error = await response.text();
if (!error.includes("already exists")) {
console.warn(`Warning: Failed to create folder ${name}: ${error}`);
}
} else {
await response.text();
}
}
/** See file header for cross-links to related helpers. */
private async createTestGroup(name: string): Promise<void> {
const response = await this.backend.apiRequest(`/api/w/${this.workspace}/groups/create`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, summary: `Test group ${name}` }),
});
if (!response.ok) {
const error = await response.text();
if (!error.includes("already exists")) {
console.warn(`Warning: Failed to create group ${name}: ${error}`);
}
} else {
await response.text();
}
}
/** See file header for cross-links to related helpers. */
private async createTestResource(path: string, description: string): Promise<void> {
// First ensure the folder exists
const folderPath = path.split("/").slice(0, 2).join("/"); // e.g., "f/test"
const folderName = folderPath.replace("f/", "");
try {
const folderResponse = await this.backend.apiRequest(`/api/w/${this.workspace}/folders/create`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: folderName }),
});
await folderResponse.text(); // Consume response body
} catch {
// Folder may already exist
}
const response = await this.backend.apiRequest(
`/api/w/${this.workspace}/resources/create`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
path,
description,
resource_type: "any",
value: { test: "value" },
}),
}
);
if (!response.ok) {
const error = await response.text();
if (!error.includes("already exists")) {
console.warn(`Warning: Failed to create resource ${path}: ${error}`);
}
} else {
await response.text();
}
}
/** See file header for cross-links to related helpers. */
private async createTestVariable(path: string, value: string): Promise<void> {
const response = await this.backend.apiRequest(
`/api/w/${this.workspace}/variables/create`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
path,
value,
is_secret: false,
description: "Test variable",
}),
}
);
if (!response.ok) {
const error = await response.text();
if (!error.includes("already exists")) {
console.warn(`Warning: Failed to create variable ${path}: ${error}`);
}
} else {
await response.text();
}
}
async getWorkspaceSettings(): Promise<any> {
const response = await this.backend.apiRequest(`/api/w/${this.workspace}/workspaces/get_settings`);
if (!response.ok) {
throw new Error(`Failed to get workspace settings: ${response.status}`);
}
return response.json();
}
async updateGitSyncConfig(config: any): Promise<void> {
const response = await this.backend.apiRequest(
`/api/w/${this.workspace}/workspaces/edit_git_sync_config`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(config),
}
);
if (!response.ok) {
throw new Error(`Failed to update git sync config: ${response.status}`);
}
await response.text();
}
async createAdditionalGitRepo(repoPath: string, description: string): Promise<void> {
const gitRepo = {
path: repoPath,
description,
resource_type: "git_repository",
value: {
url: "https://github.com/windmill-labs/windmill-test.git",
branch: "main",
token: "",
},
};
const response = await this.backend.apiRequest(
`/api/w/${this.workspace}/resources/create`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(gitRepo),
}
);
if (!response.ok) {
const error = await response.text();
if (!error.includes("already exists")) {
console.warn(`Failed to create git repo ${repoPath}: ${error}`);
}
} else {
await response.text();
}
}
async listAllScripts(): Promise<any[]> {
const response = await this.backend.apiRequest(`/api/w/${this.workspace}/scripts/list`);
if (!response.ok) return [];
return response.json();
}
async listAllApps(): Promise<any[]> {
const response = await this.backend.apiRequest(`/api/w/${this.workspace}/apps/list`);
if (!response.ok) return [];
return response.json();
}
async listAllResources(): Promise<any[]> {
const response = await this.backend.apiRequest(`/api/w/${this.workspace}/resources/list`);
if (!response.ok) return [];
return response.json();
}
async listAllVariables(): Promise<any[]> {
const response = await this.backend.apiRequest(`/api/w/${this.workspace}/variables/list`);
if (!response.ok) return [];
return response.json();
}
async createAppWithInlineScript(path: string, inlineScriptContent: string, language: string = "bun"): Promise<void> {
const response = await this.backend.apiRequest(`/api/w/${this.workspace}/apps/create`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
path,
value: {
type: "app",
grid: [
{
id: "button1",
data: {
type: "buttoncomponent",
componentInput: {
type: "runnable",
runnable: {
type: "runnableByName",
inlineScript: {
content: inlineScriptContent,
language,
},
},
},
},
},
],
hiddenInlineScripts: [],
css: {},
norefreshbar: false,
},
summary: "Test app with inline script",
policy: {
on_behalf_of: null,
on_behalf_of_email: null,
triggerables: {},
execution_mode: "viewer",
},
}),
});
if (!response.ok) {
const error = await response.text();
throw new Error(`Failed to create app ${path}: ${error}`);
}
await response.text();
}
async createFlowWithInlineScript(path: string, inlineScriptContent: string, language: string = "bun"): Promise<void> {
const response = await this.backend.apiRequest(`/api/w/${this.workspace}/flows/create`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
path,
summary: "Test flow with inline script",
description: `Flow at ${path}`,
value: {
modules: [
{
id: "a",
value: {
type: "rawscript",
content: inlineScriptContent,
language,
input_transforms: {},
},
},
],
},
schema: {
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: {},
required: [],
},
}),
});
if (!response.ok) {
const error = await response.text();
throw new Error(`Failed to create flow ${path}: ${error}`);
}
await response.text();
}
}
/**
* Adapter to make ContainerizedBackend implement TestBackend
*/
class ContainerizedBackendAdapter implements TestBackend {
private backend: ContainerizedBackend;
constructor(config?: Partial<ContainerConfig>) {
this.backend = new ContainerizedBackend(config);
}
get baseUrl(): string {
return this.backend.baseUrl;
}
get workspace(): string {
return this.backend.workspace;
}
get testConfigDir(): string {
return this.backend.testConfigDir;
}
get token(): string {
return this.backend.token;
}
async start(): Promise<void> {
await this.backend.start();
}
async stop(): Promise<void> {
await this.backend.stop();
}
async reset(): Promise<void> {
await this.backend.reset();
}
createCLICommand(args: string[], workingDir: string, opts?: { workspace?: string; token?: string }): any {
return this.backend.createCLICommand(args, workingDir, opts);
}
async runCLICommand(args: string[], workingDir: string, opts?: { workspace?: string; token?: string }) {
return this.backend.runCLICommand(args, workingDir, opts);
}
async seedTestData(): Promise<void> {
await this.backend.seedTestData();
}
async getWorkspaceSettings(): Promise<any> {
return this.backend.getWorkspaceSettings();
}
async updateGitSyncConfig(config: any): Promise<void> {
await this.backend.updateGitSyncConfig(config);
}
async createAdditionalGitRepo(repoPath: string, description: string): Promise<void> {
await this.backend.createAdditionalGitRepo(repoPath, description);
}
async listAllScripts(): Promise<any[]> {
return this.backend.listAllScripts();
}
async listAllApps(): Promise<any[]> {
return this.backend.listAllApps();
}
async listAllResources(): Promise<any[]> {
return this.backend.listAllResources();
}
async listAllVariables(): Promise<any[]> {
return this.backend.listAllVariables();
}
}
// Global backend instance
let globalBackend: TestBackend | null = null;
/**
* Get the backend type from environment
*/
function getBackendType(): "cargo" | "docker" {
const envType = process.env["TEST_BACKEND"]?.toLowerCase();
if (envType === "docker") {
return "docker";
}
return "cargo"; // Default to cargo
}
/**
* Create a new backend instance based on configuration
*/
export function createTestBackend(type?: "cargo" | "docker"): TestBackend {
const backendType = type || getBackendType();
if (backendType === "docker") {
console.log("📦 Using Docker-based test backend");
return new ContainerizedBackendAdapter();
} else {
console.log("🦀 Using Cargo-based test backend");
return new CargoBackendAdapter({
verbose: process.env["VERBOSE"] === "1",
});
}
}
/**
* Get or create global backend instance
*/
export async function getTestBackend(): Promise<TestBackend> {
if (!globalBackend) {
globalBackend = createTestBackend();
registerCleanup();
await globalBackend.start();
}
return globalBackend;
}
/**
* Convenience function for tests - runs test with backend
*/
export async function withTestBackend<T>(
testFn: (backend: TestBackend, tempDir: string) => Promise<T>
): Promise<T> {
const backend = await getTestBackend();
const tempDir = await mkdtemp(join(tmpdir(), "windmill_cli_test_"));
try {
await backend.reset();
if (backend.seedTestData) {
await backend.seedTestData();
}
return await testFn(backend, tempDir);
} finally {
await rm(tempDir, { recursive: true });
}
}
/**
* Cleanup function for test suites
*/
export async function cleanupTestBackend(): Promise<void> {
if (globalBackend) {
await globalBackend.stop();
globalBackend = null;
}
}
// Auto-cleanup on process exit
let cleanupRegistered = false;
function registerCleanup() {
if (cleanupRegistered) return;
cleanupRegistered = true;
process.on("exit", () => {
if (globalBackend) {
// Synchronous kill — can't await in exit handler
try {
(globalBackend as any).backend?.process?.kill();
} catch {
// Best effort
}
}
});
// Handle graceful shutdown
for (const signal of ["SIGINT", "SIGTERM"] as const) {
process.on(signal, async () => {
await cleanupTestBackend();
process.exit(0);
});
}
}
/**
* Create a non-admin user, add them to the workspace, and return their token.
*/
export async function createNonAdminUser(
backend: TestBackend,
workspaceId: string = backend.workspace
): Promise<string> {
if (!backend.apiRequest) {
throw new Error("Backend does not support apiRequest");
}
const email = `nonadmin_${Date.now()}@test.dev`;
const password = "testpass123";
// Create user globally (as admin)
const createResp = await backend.apiRequest("/api/users/create", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email,
password,
super_admin: false,
name: "Non-Admin Test User",
}),
});
if (!createResp.ok) {
throw new Error(`Failed to create user: ${await createResp.text()}`);
}
await createResp.text();
// Add user to workspace as non-admin
const addResp = await backend.apiRequest(
`/api/w/${workspaceId}/workspaces/add_user`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email,
is_admin: false,
operator: false,
}),
}
);
if (!addResp.ok) {
throw new Error(`Failed to add user to workspace: ${await addResp.text()}`);
}
await addResp.text();
// Login as the non-admin user to get a token
const loginResp = await fetch(`${backend.baseUrl}/api/auth/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password }),
});
if (!loginResp.ok) {
throw new Error(`Failed to login as non-admin: ${await loginResp.text()}`);
}
return await loginResp.text();
}
/**
* Create workspace dependencies via the API (e.g. a shared package.json for bun scripts).
*/
export async function createRemoteWorkspaceDeps(
backend: TestBackend,
language: string,
content: string,
name?: string,
): Promise<void> {
if (!backend.apiRequest) {
throw new Error("Backend does not support apiRequest");
}
const resp = await backend.apiRequest(
`/api/w/${backend.workspace}/workspace_dependencies/create`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
workspace_id: backend.workspace,
language,
content,
...(name ? { name } : {}),
}),
}
);
if (!resp.ok) {
throw new Error(`Failed to create workspace deps (${resp.status}): ${await resp.text()}`);
}
await resp.text();
}
// Re-export for convenience
export type { CargoBackendConfig } from "./cargo_backend.ts";
export type { ContainerConfig } from "./containerized_backend.ts";