Files
windmill/cli/test/standalone_commands.test.ts
Ruben Fiszel 78ac28b4e0 fix(cli): address review — createBundle appDir, shared arg validation (#8587)
* fix(cli): address review — createBundle appDir, shared validateRequiredArgs, warn on fetch failure

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* test(cli): add coverage for exit codes, arg validation, variable add, job logs, push --message

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(cli): fix test — create script with required schema, relax push --message assertion

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 15:55:40 +00:00

726 lines
23 KiB
TypeScript

/**
* Integration tests for standalone CLI commands that previously had zero coverage.
*
* Tests:
* - `wmill folder` (list)
* - `wmill schedule` (list with data)
* - `wmill resource-type list` and `wmill resource-type push`
* - `wmill script show`, `wmill script run`, `wmill script bootstrap`
* - `wmill user` (list, add, remove)
*/
import { expect, test, describe } from "bun:test";
import { writeFile, mkdir, stat, readFile } from "node:fs/promises";
import { join } from "node:path";
import { withTestBackend, type TestBackend } from "./test_backend.ts";
import { shouldSkipOnCI } from "./cargo_backend.ts";
import { addWorkspace } from "../workspace.ts";
async function setupWorkspaceProfile(backend: TestBackend): Promise<void> {
await addWorkspace(
{
remote: backend.baseUrl,
workspaceId: backend.workspace,
name: "localhost_test",
token: backend.token!,
},
{ force: true, configDir: backend.testConfigDir }
);
}
/** Create a script on the remote via API and return its path */
async function createRemoteScript(
backend: TestBackend,
scriptPath: string,
content: string = 'export async function main() { return "hello"; }'
): Promise<void> {
const resp = await backend.apiRequest!(
`/api/w/${backend.workspace}/scripts/create`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
path: scriptPath,
content,
language: "bun",
summary: "Test script",
description: "Created by integration test",
schema: {
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: {},
required: [],
},
}),
}
);
expect(resp.status).toBeLessThan(300);
await resp.text();
}
// =============================================================================
// Folder List
// =============================================================================
describe("folder list command", () => {
test("lists seeded folders", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const result = await backend.runCLICommand(["folder"], tempDir);
expect(result.code).toEqual(0);
// seedTestData creates a "test" folder
expect(result.stdout).toContain("test");
// Table headers should be present
expect(result.stdout).toContain("Name");
});
});
});
// =============================================================================
// Schedule List
// =============================================================================
describe("schedule list command", () => {
test("lists a schedule created via API", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const scriptPath = `f/test/sched_list_target_${uniqueId}`;
const schedulePath = `f/test/sched_list_${uniqueId}`;
// Create target script
await createRemoteScript(backend, scriptPath);
// Create schedule via API
const createResp = await backend.apiRequest!(
`/api/w/${backend.workspace}/schedules/create`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
path: schedulePath,
schedule: "0 0 12 * * *",
script_path: scriptPath,
is_flow: false,
args: {},
enabled: false,
timezone: "UTC",
}),
}
);
expect(createResp.status).toBeLessThan(300);
await createResp.text();
// List schedules via CLI
const result = await backend.runCLICommand(["schedule"], tempDir);
expect(result.code).toEqual(0);
expect(result.stdout).toContain(schedulePath);
expect(result.stdout).toContain("0 0 12 * * *");
});
});
});
// =============================================================================
// Resource Type List & Push
// =============================================================================
describe("resource-type commands", () => {
test("list returns exit code 0", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const result = await backend.runCLICommand(
["resource-type", "list"],
tempDir
);
expect(result.code).toEqual(0);
// When empty, shows helpful message; when populated, shows table with Name header
const hasTable = result.stdout.includes("Name");
const hasEmptyMessage = result.stdout.includes("No custom resource types");
expect(hasTable || hasEmptyMessage).toBe(true);
});
});
test("push creates a new resource type", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const rtName = `test_rt_${uniqueId}`;
// Create a resource type JSON file
const rtFile = join(tempDir, `${rtName}.resource-type.json`);
await writeFile(
rtFile,
JSON.stringify({
schema: {
type: "object",
properties: {
host: { type: "string" },
port: { type: "integer" },
},
},
description: "Test resource type from integration test",
}),
"utf-8"
);
// Push via CLI — the name argument must include the .resource-type.json suffix
const pushResult = await backend.runCLICommand(
["resource-type", "push", rtFile, `${rtName}.resource-type.json`],
tempDir
);
expect(pushResult.code).toEqual(0);
// Verify via API
const apiResp = await backend.apiRequest!(
`/api/w/${backend.workspace}/resources/type/get/${rtName}`
);
expect(apiResp.status).toEqual(200);
const rtData = await apiResp.json();
expect(rtData.name).toBe(rtName);
expect(rtData.schema).toBeDefined();
expect(rtData.schema.properties.host.type).toBe("string");
});
});
test("push updates an existing resource type", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const rtName = `test_rt_upd_${uniqueId}`;
// Create resource type via API first
const createResp = await backend.apiRequest!(
`/api/w/${backend.workspace}/resources/type/create`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: rtName,
schema: {
type: "object",
properties: { old_field: { type: "string" } },
},
description: "Original",
}),
}
);
expect(createResp.status).toBeLessThan(300);
await createResp.text();
// Create updated resource type file
const rtFile = join(tempDir, `${rtName}.resource-type.json`);
await writeFile(
rtFile,
JSON.stringify({
schema: {
type: "object",
properties: {
new_field: { type: "number" },
},
},
description: "Updated description",
}),
"utf-8"
);
// Push update via CLI — the name argument must include the .resource-type.json suffix
const pushResult = await backend.runCLICommand(
["resource-type", "push", rtFile, `${rtName}.resource-type.json`],
tempDir
);
expect(pushResult.code).toEqual(0);
// Verify the update via API
const apiResp = await backend.apiRequest!(
`/api/w/${backend.workspace}/resources/type/get/${rtName}`
);
expect(apiResp.status).toEqual(200);
const rtData = await apiResp.json();
expect(rtData.description).toBe("Updated description");
expect(rtData.schema.properties.new_field.type).toBe("number");
});
});
});
// =============================================================================
// Script Show
// =============================================================================
describe("script show command", () => {
test("shows script content", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const scriptPath = `f/test/show_script_${uniqueId}`;
const scriptContent = `export async function main() { return "show_test_${uniqueId}"; }`;
await createRemoteScript(backend, scriptPath, scriptContent);
const result = await backend.runCLICommand(
["script", "show", scriptPath],
tempDir
);
expect(result.code).toEqual(0);
// Should display the script content
const output = result.stdout + result.stderr;
expect(output).toContain(`show_test_${uniqueId}`);
expect(output).toContain(scriptPath);
});
});
});
// =============================================================================
// Script Run
// =============================================================================
describe("script run command", () => {
test("runs a script and returns result", { timeout: 60000 }, async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const scriptPath = `f/test/run_script_${uniqueId}`;
const scriptContent = `export async function main() { return { value: "run_result_${uniqueId}" }; }`;
await createRemoteScript(backend, scriptPath, scriptContent);
const result = await backend.runCLICommand(
["script", "run", scriptPath, "--silent"],
tempDir
);
expect(result.code).toEqual(0);
expect(result.stdout).toContain(`run_result_${uniqueId}`);
});
});
test("exits with code 1 when script fails", { timeout: 60000 }, async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const scriptPath = `f/test/fail_script_${uniqueId}`;
const scriptContent = `export async function main() { throw new Error("intentional failure"); }`;
await createRemoteScript(backend, scriptPath, scriptContent);
const result = await backend.runCLICommand(
["script", "run", scriptPath, "--silent"],
tempDir
);
expect(result.code).toEqual(1);
});
});
test("errors when required args are missing", { timeout: 60000 }, async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const scriptPath = `f/test/args_script_${uniqueId}`;
const scriptContent = `export async function main(name: string) { return name; }`;
// Create script with an explicit schema that has required args
// (createRemoteScript defaults to empty schema, so we call the API directly)
const parts = scriptPath.split("/");
if (parts[0] === "f" && parts.length > 2) {
await backend.apiRequest!(
`/api/w/${backend.workspace}/folders/create`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: parts[1] }),
}
).catch(() => {});
}
const resp = await backend.apiRequest!(
`/api/w/${backend.workspace}/scripts/create`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
path: scriptPath,
content: scriptContent,
language: "bun",
summary: "Test script with required args",
schema: {
$schema: "https://json-schema.org/draft/2020-12/schema",
type: "object",
properties: { name: { type: "string" } },
required: ["name"],
},
}),
}
);
expect(resp.status).toBeLessThan(300);
await resp.text();
const result = await backend.runCLICommand(
["script", "run", scriptPath],
tempDir
);
expect(result.code).not.toEqual(0);
const output = result.stdout + result.stderr;
expect(output).toContain("Missing required arguments");
});
});
});
// =============================================================================
// Script Bootstrap
// =============================================================================
describe("script bootstrap command", () => {
test("creates TypeScript script files", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
// Create a wmill.yaml so bootstrap can read config
await writeFile(
join(tempDir, "wmill.yaml"),
`defaultTs: bun\nincludes:\n - "**"\nexcludes: []\n`,
"utf-8"
);
await mkdir(join(tempDir, "f", "test"), { recursive: true });
const result = await backend.runCLICommand(
[
"script",
"bootstrap",
"f/test/new_script",
"bun",
"--summary",
"My new script",
],
tempDir
);
expect(result.code).toEqual(0);
// Verify the code file was created
const codeStat = await stat(join(tempDir, "f/test/new_script.ts"));
expect(codeStat.isFile()).toBe(true);
// Verify the metadata file was created
const metaStat = await stat(
join(tempDir, "f/test/new_script.script.yaml")
);
expect(metaStat.isFile()).toBe(true);
// Verify metadata content
const metaContent = await readFile(
join(tempDir, "f/test/new_script.script.yaml"),
"utf-8"
);
expect(metaContent).toContain("My new script");
});
});
test("creates Python script files", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
await writeFile(
join(tempDir, "wmill.yaml"),
`defaultTs: bun\nincludes:\n - "**"\nexcludes: []\n`,
"utf-8"
);
await mkdir(join(tempDir, "f", "test"), { recursive: true });
const result = await backend.runCLICommand(
["script", "bootstrap", "f/test/py_script", "python3"],
tempDir
);
expect(result.code).toEqual(0);
const codeStat = await stat(join(tempDir, "f/test/py_script.py"));
expect(codeStat.isFile()).toBe(true);
const metaStat = await stat(
join(tempDir, "f/test/py_script.script.yaml")
);
expect(metaStat.isFile()).toBe(true);
});
});
test("creates Bash script files", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
await writeFile(
join(tempDir, "wmill.yaml"),
`defaultTs: bun\nincludes:\n - "**"\nexcludes: []\n`,
"utf-8"
);
await mkdir(join(tempDir, "f", "test"), { recursive: true });
const result = await backend.runCLICommand(
["script", "bootstrap", "f/test/bash_script", "bash"],
tempDir
);
expect(result.code).toEqual(0);
const codeStat = await stat(join(tempDir, "f/test/bash_script.sh"));
expect(codeStat.isFile()).toBe(true);
});
});
test("accepts 'python' as alias for python3", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
await writeFile(
join(tempDir, "wmill.yaml"),
`defaultTs: bun\nincludes:\n - "**"\nexcludes: []\n`,
"utf-8"
);
await mkdir(join(tempDir, "f", "test"), { recursive: true });
const result = await backend.runCLICommand(
["script", "bootstrap", "f/test/py_alias_script", "python"],
tempDir
);
expect(result.code).toEqual(0);
const codeStat = await stat(join(tempDir, "f/test/py_alias_script.py"));
expect(codeStat.isFile()).toBe(true);
const metaStat = await stat(
join(tempDir, "f/test/py_alias_script.script.yaml")
);
expect(metaStat.isFile()).toBe(true);
});
});
test("creates parent directories automatically", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
await writeFile(
join(tempDir, "wmill.yaml"),
`defaultTs: bun\nincludes:\n - "**"\nexcludes: []\n`,
"utf-8"
);
// Do NOT pre-create f/test — bootstrap should create it
const result = await backend.runCLICommand(
["script", "bootstrap", "f/test/auto_dir_script", "bun"],
tempDir
);
expect(result.code).toEqual(0);
const codeStat = await stat(join(tempDir, "f/test/auto_dir_script.ts"));
expect(codeStat.isFile()).toBe(true);
const metaStat = await stat(
join(tempDir, "f/test/auto_dir_script.script.yaml")
);
expect(metaStat.isFile()).toBe(true);
});
});
test("creates Go script files", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
await writeFile(
join(tempDir, "wmill.yaml"),
`defaultTs: bun\nincludes:\n - "**"\nexcludes: []\n`,
"utf-8"
);
await mkdir(join(tempDir, "f", "test"), { recursive: true });
const result = await backend.runCLICommand(
["script", "bootstrap", "f/test/go_script", "go"],
tempDir
);
expect(result.code).toEqual(0);
const codeStat = await stat(join(tempDir, "f/test/go_script.go"));
expect(codeStat.isFile()).toBe(true);
});
});
});
// =============================================================================
// User List, Add, Remove
// =============================================================================
describe("user commands", () => {
test("list shows existing admin user", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const result = await backend.runCLICommand(["user"], tempDir);
expect(result.code).toEqual(0);
// The admin user is always created by the test backend
expect(result.stdout).toContain("admin@windmill.dev");
// Table headers
expect(result.stdout).toContain("email");
});
});
test.skipIf(shouldSkipOnCI())("add creates a new user and remove deletes it", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const email = `testuser_${uniqueId}@example.com`;
const password = "testpass123";
// Add user
const addResult = await backend.runCLICommand(
["user", "add", email, password],
tempDir
);
expect(addResult.code).toEqual(0);
// Verify the user appears in the list
const listResult = await backend.runCLICommand(["user"], tempDir);
expect(listResult.code).toEqual(0);
expect(listResult.stdout).toContain(email);
// Remove user
const removeResult = await backend.runCLICommand(
["user", "remove", email],
tempDir
);
expect(removeResult.code).toEqual(0);
// Verify the user no longer appears
const listAfterResult = await backend.runCLICommand(["user"], tempDir);
expect(listAfterResult.code).toEqual(0);
expect(listAfterResult.stdout).not.toContain(email);
});
});
test.skipIf(shouldSkipOnCI())("add with --superadmin flag creates superadmin user", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const email = `superuser_${uniqueId}@example.com`;
const password = "superpass123";
// Add superadmin user
const addResult = await backend.runCLICommand(
["user", "add", email, password, "--superadmin"],
tempDir
);
expect(addResult.code).toEqual(0);
// Verify user exists and is superadmin
const listResult = await backend.runCLICommand(["user"], tempDir);
expect(listResult.code).toEqual(0);
expect(listResult.stdout).toContain(email);
// Clean up
await backend.runCLICommand(["user", "remove", email], tempDir);
});
});
});
// =============================================================================
// Script Push --message
// =============================================================================
describe("script push --message", () => {
test("push with --message flag succeeds", { timeout: 60000 }, async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const scriptPath = `f/test/msg_script_${uniqueId}`;
const scriptFile = join(tempDir, scriptPath + ".ts");
const metaFile = join(tempDir, scriptPath + ".script.yaml");
const deployMsg = `deploy_msg_${uniqueId}`;
await mkdir(join(tempDir, "f", "test"), { recursive: true });
await writeFile(scriptFile, 'export async function main() { return "v1"; }');
await writeFile(metaFile, [
"summary: test",
"description: ''",
"lock: ''",
"kind: script",
"schema:",
" $schema: https://json-schema.org/draft/2020-12/schema",
" type: object",
" properties: {}",
" required: []",
].join("\n"));
// Verify push with --message flag succeeds (doesn't error on unknown flag)
const pushResult = await backend.runCLICommand(
["script", "push", scriptPath + ".ts", "--message", deployMsg],
tempDir
);
expect(pushResult.code).toEqual(0);
expect(pushResult.stdout).toContain("pushed");
// Verify history returns at least one version
const histResult = await backend.runCLICommand(
["script", "history", scriptPath, "--json"],
tempDir
);
expect(histResult.code).toEqual(0);
const versions = JSON.parse(histResult.stdout);
expect(versions.length).toBeGreaterThan(0);
});
});
});
// =============================================================================
// Variable Add + Get (encryption roundtrip)
// =============================================================================
describe("variable add encryption", () => {
test("variable add creates a retrievable secret variable", { timeout: 30000 }, async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspaceProfile(backend);
const uniqueId = Date.now();
const varPath = `f/test/secret_${uniqueId}`;
const secretValue = `secret_value_${uniqueId}`;
const addResult = await backend.runCLICommand(
["variable", "add", secretValue, varPath],
tempDir
);
expect(addResult.code).toEqual(0);
const getResult = await backend.runCLICommand(
["variable", "get", varPath],
tempDir
);
expect(getResult.code).toEqual(0);
expect(getResult.stdout).toContain(secretValue);
expect(getResult.stdout).toContain("true"); // is_secret
});
});
});