Files
windmill/cli/test/unified_generate_metadata.test.ts
Ruben FiszelandClaude Opus 4.6 31d6660d56 feat: script module mode with CLI sync, preview, and WAC UI improvements (#8380)
* feat: add script module mode with folder model for Bun and Python

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

* fix: add missing modules field to RawCode in bun_executor

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

* sqlx

* feat: enrich WAC templates with checkpoint and replay semantics

Add prominent comments explaining that all computation must happen
inside task/step/taskScript or it will be replayed on resume/retry.
Clarify that waitForApproval does not hold a worker and that
approve/reject URLs are available in the timeline step details.

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

* fix(cli): script module sync idempotency, per-module hash tracking, and preview support

- Fix pull→push idempotency: use `??` instead of `||` for module lock
  field so empty strings are preserved (matches API's `lock: ""`)
- Add per-module hash tracking in wmill-lock.yaml following the flow
  inline script pattern (SCRIPT_TOP_HASH + per-module subpath hashes)
- Selective module lock regeneration: only regenerate locks for modules
  whose content actually changed, not all modules
- Use unfiltered rawWorkspaceDependencies for module hashes to match
  what updateModuleLocks passes to fetchScriptLock
- Show changed module names in stale script output for clarity
- Add module support to `script preview` command: read modules from
  __mod/ folder and pass them in the preview API request
- Add preview tests for taskScript pattern (flat and folder layout)
- Update test assertion for module stale detection output

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

* feat(frontend): WAC UI improvements — reorder templates, module tab rename, import consolidation

- Reorder WAC template buttons: TypeScript before Python in
  ScriptBuilder, CreateActionsScript, and CreateActionsFlow
- Remove dropdown items from +Script button (simplify to direct link)
- Move "Import Workflow-as-Code" to +Flow dropdown with dedicated drawer
- Add module tab rename: pencil icon on hover opens popover with
  validation, fixed-width icon container prevents layout shift

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

* feat: remaining module-mode changes from working branch

- Backend parser updates for WAC detection
- CLI sync/types updates for raw app path and module support
- Frontend UI polish (Dev.svelte, ScriptRow, script hash page)
- Test fixture updates

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

* test(cli): add test for module modification detection in generate-metadata

Verifies that modifying a single module file re-triggers stale
detection and only the changed module is listed, not all modules.

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

* fix(backend): critical fixes from PR review

- Fix hardcoded dev path in bun_executor.rs WAC v2 wrapper — use
  "windmill-client" import instead of absolute filesystem path
- Fix missed no_main_func → auto_kind rename in parser TS test
- Add modules column to clone_script SQL (windmill-common and
  windmill-api-workspaces) so cloned scripts retain their modules
- Add modules: None to RawCode structs in worker tests
- Restore complete sqlx cache (merge main's cache + our new queries)

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

* fix(backend): fix clone warning treated as error in CI

Change `.clone()` on double reference to `*k` dereference in
scripts.rs hash implementation. Update sqlx cache with new query
hashes from modified clone_script SQL.

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

* fix(frontend): use published parser wasm versions for CI build

The local file:// paths for windmill-parser-wasm-py and
windmill-parser-wasm-ts don't exist in the Cloudflare Pages build
environment. Revert to published npm versions (1.655.0).

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

* fix(frontend): update parser wasm packages to 1.657.2

Use newly published windmill-parser-wasm-ts and windmill-parser-wasm-py
v1.657.2 which include auto_kind/WAC detection changes.

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

* fix(frontend): regenerate package-lock.json for npm ci compatibility

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

* fix(frontend): use main's lockfile as base, update only parser wasm packages

Regenerating package-lock.json from scratch pulled different dependency
versions causing svelte-check type errors. Instead, start from main's
lockfile and only update the two changed packages.

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

* fix(backend): add modules column to fetch_script_for_update query

The Script<SR> struct has a modules field (FromRow), but
fetch_script_for_update didn't SELECT modules, causing a runtime
error "no column found for name: modules" when the worker processed
dependency jobs. This was the root cause of the relock_skip test
timeout.

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

* fix(backend): fix script module execution for Python and Bun

- Fix modules not passed through job queue: inject _MODULES into
  PushArgs.extra when pushing Code jobs so worker can extract them
- Fix Python module imports: use relative imports (from .helper)
  and add sys.path.insert for module directory in wrapper
- Fix Python tests: use relative imports and empty lock to prevent
  pip from resolving module names as packages
- Add local file check in Bun loader for module resolution
- Ignore Bun module test (bundle mode loader integration tracked
  separately)
- Add missing modules column to fetch_script_for_update query

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

* fix(backend): remove unnecessary empty lock in Python module tests

Relative imports (from .helper) are not parsed as pip packages,
so the empty lock workaround is not needed.

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

* fix(backend): fix module execution for Python and Bun — all tests pass

Python modules:
- Use relative imports (from .helper import greet) since scripts run
  as packages
- Add sys.path.insert for module directory in wrapper to ensure local
  modules take precedence over pip packages with same name

Bun modules:
- Use bundled output (./out/main.js) as wrapper import when modules
  are present — the bundled output has module content inlined by
  Bun.build, avoiding runtime loader resolution issues
- Add local file check in loader.bun.js onResolve to short-circuit
  API URL resolution for module files on disk

Job queue:
- Inject _MODULES into PushArgs.extra when pushing Code jobs so
  the worker can extract them at execution time

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

* refactor: address PR review — simplify, fix correctness, remove dead code

Critical fixes:
- Replace all CLI `no_main_func` references with `auto_kind` (string)
  to match the backend migration and API changes
- Remove duplicated `compute_python_module_dir` in worker.rs, use
  the canonical version from python_executor.rs

High priority:
- Auto-create `__init__.py` in intermediate directories for nested
  Python modules so imports like `from .utils.math import add` work
  without users manually creating __init__.py files
- Remove redundant `sys_path_insert` — relative imports use Python's
  package system, not sys.path

Medium:
- Fix lock file base name extraction: use regex to strip only the
  final extension (`.replace(/\.[^.]+$/, '')`) instead of `indexOf(".")`
  which breaks for files like `helper.test.ts`

Simplification:
- Remove dead `{#if false}` Popover block in ScriptEditor.svelte
- Guard loader.bun.js local file check to only run for relative paths
  (matching the Windows loader pattern)
- Add clarifying comment on Bun dual mechanism (build + run phases)
- Add maintenance comment on manual Hash impl for NewScript

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

* fix: final review fixes — stale cleanup, baseName, auto_kind export

- Fix sync.ts baseName extraction using indexOf(".") → regex
  (same fix as script.ts/metadata.ts, missed this instance)
- Add stale module file cleanup in writeModulesToDisk: removes files
  from __mod/ that are no longer in the modules map before writing,
  fixing the pull→push cycle that couldn't delete modules
- Log warning when _MODULES serialization fails in job push instead
  of silently dropping modules
- Use strict equality (===) for auto_kind comparison
- Exclude auto_kind from workspace export — it is auto-detected by
  the parser at deploy time from script content

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

* fix(cli): remove auto_kind from push, comparison, and metadata

auto_kind is auto-detected by the parser at deploy time, so the CLI
should not send it, compare it, or write it to script.yaml.

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

* fix: remove erroneously added backend/backend/.sqlx directory

Duplicate .sqlx cache was committed at the wrong nested path.

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

* fix: address PR review feedback + fix CI dead_code warning

Frontend (ScriptEditor.svelte):
- Fix switchToMain() missing lastSyncedCode update — prevents stale
  code sync on external changes while editing a module tab
- Fix formatAction saving module code to main script's localStorage
  draft — now saves main code when on a module tab
- Fix non-null assertion on inferModuleLang in renameModule — fall
  back to original language instead of force unwrap
- Remove redundant activeModuleTab truthy check in runTest

CLI (script.ts):
- Clean up empty directories after removing stale module files in
  writeModulesToDisk

Backend:
- Add path traversal guard in write_module_files — reject module
  paths containing ".."
- Fix dead_code warning on auto_kind field in workspace export struct

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

* feat(frontend): improve auto_kind UX + address review findings

- Rename "Include without main function" toggle to "Include library
  scripts" in script list (ItemsList.svelte)
- Update NoMainFuncBadge: "No main" → "Library" with clearer tooltip
- Filter module file extensions by main script language — Python
  scripts only allow .py modules, TypeScript only .ts, etc.
- Split flushModuleState into flushModuleContent (no UI side-effect)
  and flushModuleState (flush + reset tab), reducing duplication
- Dynamic placeholder and hint text in add module popover based on
  main script language

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 01:20:09 +00:00

780 lines
26 KiB
TypeScript

/**
* Unified generate-metadata Command Tests
*
* Tests the new unified `generate-metadata` command that processes
* scripts, flows, and apps together.
*/
import { expect, test, describe } from "bun:test";
import { withTestBackend } from "./test_backend.ts";
import { addWorkspace } from "../workspace.ts";
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
import {
createLocalScript,
createLocalFlow,
createLocalApp,
createLocalRawApp,
createLocalScriptWithModules,
} from "./test_fixtures.ts";
/**
* Helper to set up a workspace with wmill.yaml
*/
async function setupWorkspace(
backend: any,
tempDir: string,
workspaceName: string,
nonDottedPaths = false
) {
const testWorkspace = {
remote: backend.baseUrl,
workspaceId: backend.workspace,
name: workspaceName,
token: backend.token
};
await addWorkspace(testWorkspace, { force: true, configDir: backend.testConfigDir });
await writeFile(`${tempDir}/wmill.yaml`, `defaultTs: bun
${nonDottedPaths ? "nonDottedPaths: true\n" : ""}includes:
- "**"
excludes: []`, "utf-8");
}
async function createLocalNonDottedFlow(tempDir: string, name: string) {
const flowDir = `${tempDir}/f/test/${name}__flow`;
await mkdir(flowDir, { recursive: true });
await writeFile(
`${flowDir}/a.ts`,
`export async function main() {\n return "Hello from flow ${name}";\n}`,
"utf-8"
);
await writeFile(
`${flowDir}/flow.yaml`,
`summary: "${name} flow"
description: "A flow for testing"
value:
modules:
- id: a
value:
type: rawscript
content: "!inline a.ts"
language: bun
input_transforms: {}
schema:
$schema: "https://json-schema.org/draft/2020-12/schema"
type: object
properties: {}
required: []
`,
"utf-8"
);
}
async function createLocalNonDottedApp(tempDir: string, name: string) {
const appDir = `${tempDir}/f/test/${name}__app`;
await mkdir(appDir, { recursive: true });
await writeFile(
`${appDir}/app.yaml`,
`summary: "${name} app"
value:
type: app
grid:
- id: button1
data:
type: buttoncomponent
componentInput:
type: runnable
runnable:
type: runnableByName
inlineScript:
content: |
export async function main() {
return "hello from app";
}
language: bun
hiddenInlineScripts: []
css: {}
norefreshbar: false
policy:
on_behalf_of: null
on_behalf_of_email: null
triggerables: {}
execution_mode: viewer
`,
"utf-8"
);
}
async function fileExists(filePath: string): Promise<boolean> {
try {
await stat(filePath);
return true;
} catch {
return false;
}
}
// =============================================================================
// Main test: processes scripts, flows, and apps together
// =============================================================================
test("generate-metadata: processes scripts, flows, and apps together", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "unified_all_test");
// Create one of each type
await createLocalScript(tempDir, "f/test", "my_script");
await createLocalFlow(tempDir, "f/test", "my_flow");
await createLocalApp(tempDir, "f/test", "my_app");
const result = await backend.runCLICommand(
["generate-metadata", "--yes"],
tempDir,
"unified_all_test"
);
expect(result.code).toEqual(0);
// Should find stale items
expect(result.stdout).toContain("Found");
expect(result.stdout).toContain("stale metadata");
});
});
// =============================================================================
// Flag tests
// =============================================================================
describe("generate-metadata flags", () => {
test("--includes filters to specific paths", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "includes_test");
// Create two scripts in different folders
await createLocalScript(tempDir, "f/included", "script_a");
await createLocalScript(tempDir, "f/excluded", "script_b");
// Run with --includes to only process f/included
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "-i", "f/included/**"],
tempDir,
"includes_test"
);
expect(result.code).toEqual(0);
// Should only mention the included script
const output = result.stdout + result.stderr;
expect(output).toContain("script_a");
expect(output).not.toContain("script_b");
});
});
test("--excludes filters out specific paths", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "excludes_test");
// Create two scripts
await createLocalScript(tempDir, "f/keep", "script_keep");
await createLocalScript(tempDir, "f/skip", "script_skip");
// Run with --excludes to skip f/skip
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "-e", "f/skip/**"],
tempDir,
"excludes_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
expect(output).toContain("script_keep");
expect(output).not.toContain("script_skip");
});
});
test("--dry-run shows stale items without updating", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "dry_run_test");
await createLocalScript(tempDir, "f/test", "my_script");
// Run with --dry-run
const result = await backend.runCLICommand(
["generate-metadata", "--dry-run"],
tempDir,
"dry_run_test"
);
expect(result.code).toEqual(0);
// Should show stale items (Scripts section header)
expect(result.stdout).toContain("Scripts");
expect(result.stdout).toContain("my_script");
// Should NOT show "Done" (didn't actually update)
expect(result.stdout).not.toContain("Done");
// Run again without --dry-run to verify it would still be stale
const result2 = await backend.runCLICommand(
["generate-metadata", "--dry-run"],
tempDir,
"dry_run_test"
);
expect(result2.stdout).toContain("Scripts");
});
});
test("--lock-only only regenerates locks", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "lock_only_test");
await createLocalScript(tempDir, "f/test", "my_script");
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "--lock-only"],
tempDir,
"lock_only_test"
);
expect(result.code).toEqual(0);
});
});
test("--lock-only preserves non-dotted flow filenames", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "lock_only_non_dotted_test", true);
await createLocalNonDottedFlow(tempDir, "my_flow");
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "--lock-only"],
tempDir,
"lock_only_non_dotted_test"
);
expect(result.code).toEqual(0);
const flowDir = `${tempDir}/f/test/my_flow__flow`;
const flowYaml = await readFile(`${flowDir}/flow.yaml`, "utf-8");
expect(flowYaml).toContain("!inline a.ts");
expect(flowYaml).toContain("!inline a.lock");
expect(flowYaml).not.toContain(".inline_script.");
expect(await fileExists(`${flowDir}/a.lock`)).toEqual(true);
expect(await fileExists(`${flowDir}/a.inline_script.ts`)).toEqual(false);
expect(await fileExists(`${flowDir}/a.inline_script.lock`)).toEqual(false);
});
});
test("generate-metadata preserves non-dotted flow inline script filenames", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "full_gen_non_dotted_flow_test", true);
await createLocalNonDottedFlow(tempDir, "my_flow");
const result = await backend.runCLICommand(
["generate-metadata", "--yes"],
tempDir,
"full_gen_non_dotted_flow_test"
);
expect(result.code).toEqual(0);
const flowDir = `${tempDir}/f/test/my_flow__flow`;
const flowYaml = await readFile(`${flowDir}/flow.yaml`, "utf-8");
// Inline script references should use non-dotted naming
expect(flowYaml).toContain("!inline a.ts");
expect(flowYaml).toContain("!inline a.lock");
expect(flowYaml).not.toContain(".inline_script.");
expect(await fileExists(`${flowDir}/a.ts`)).toEqual(true);
expect(await fileExists(`${flowDir}/a.lock`)).toEqual(true);
expect(await fileExists(`${flowDir}/a.inline_script.ts`)).toEqual(false);
expect(await fileExists(`${flowDir}/a.inline_script.lock`)).toEqual(false);
});
});
test("generate-metadata uses non-dotted app inline script filenames", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "non_dotted_app_gen_test", true);
await createLocalNonDottedApp(tempDir, "my_app");
const result = await backend.runCLICommand(
["generate-metadata", "--yes"],
tempDir,
"non_dotted_app_gen_test"
);
expect(result.code).toEqual(0);
const appDir = `${tempDir}/f/test/my_app__app`;
const appYaml = await readFile(`${appDir}/app.yaml`, "utf-8");
// Inline script references should use non-dotted naming
expect(appYaml).not.toContain(".inline_script.");
// Verify no dotted inline script files were created
const { readdir: readdirAsync } = await import("node:fs/promises");
const files = await readdirAsync(appDir);
const dottedFiles = files.filter((f: string) => f.includes(".inline_script."));
expect(dottedFiles.length).toEqual(0);
});
});
test("--schema-only only processes scripts (skips flows and apps)", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "schema_only_test");
// Create one of each type
await createLocalScript(tempDir, "f/test", "my_script");
await createLocalFlow(tempDir, "f/test", "my_flow");
await createLocalApp(tempDir, "f/test", "my_app");
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "--schema-only"],
tempDir,
"schema_only_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
// Should show "Checking scripts..." only
expect(output).toContain("Checking scripts...");
// Should find the script (Scripts section header)
expect(output).toContain("Scripts");
// Should NOT find flows or apps
expect(output).not.toContain("Flows");
expect(output).not.toContain("Apps");
});
});
test("--skip-scripts skips scripts", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "skip_scripts_test");
await createLocalScript(tempDir, "f/test", "my_script");
await createLocalFlow(tempDir, "f/test", "my_flow");
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "--skip-scripts"],
tempDir,
"skip_scripts_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
// Should NOT contain script
expect(output).not.toContain("Scripts");
// Should contain flow
expect(output).toContain("Flows");
});
});
test("--skip-flows skips flows", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "skip_flows_test");
await createLocalScript(tempDir, "f/test", "my_script");
await createLocalFlow(tempDir, "f/test", "my_flow");
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "--skip-flows"],
tempDir,
"skip_flows_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
// Should contain script
expect(output).toContain("Scripts");
// Should NOT contain flow
expect(output).not.toContain("Flows");
});
});
test("--skip-apps skips apps", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "skip_apps_test");
await createLocalScript(tempDir, "f/test", "my_script");
await createLocalApp(tempDir, "f/test", "my_app");
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "--skip-apps"],
tempDir,
"skip_apps_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
// Should contain script
expect(output).toContain("Scripts");
// Should NOT contain app
expect(output).not.toContain("Apps");
});
});
test("shows 'All metadata up-to-date' when nothing to update", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "uptodate_test");
// Create a script and run generate-metadata twice
await createLocalScript(tempDir, "f/test", "my_script");
// First run - generates metadata
await backend.runCLICommand(
["generate-metadata", "--yes"],
tempDir,
"uptodate_test"
);
// Second run - should be up-to-date
const result = await backend.runCLICommand(
["generate-metadata", "--yes"],
tempDir,
"uptodate_test"
);
expect(result.code).toEqual(0);
expect(result.stdout).toContain("up-to-date");
});
});
test("skipping all types shows warning", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "skip_all_test");
await createLocalScript(tempDir, "f/test", "my_script");
const result = await backend.runCLICommand(
["generate-metadata", "--skip-scripts", "--skip-flows", "--skip-apps"],
tempDir,
"skip_all_test"
);
expect(result.code).toEqual(0);
expect(result.stdout).toContain("Nothing to check");
});
});
});
// =============================================================================
// Folder argument tests
// =============================================================================
describe("generate-metadata folder argument", () => {
test("filters to specific script folder", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "folder_script_test");
// Create scripts in different folders
await createLocalScript(tempDir, "f/included", "script_a");
await createLocalScript(tempDir, "f/excluded", "script_b");
// Run with folder argument
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "f/included/script_a.ts"],
tempDir,
"folder_script_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
expect(output).toContain("script_a");
expect(output).not.toContain("script_b");
});
});
test("filters to specific flow folder", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "folder_flow_test");
// Create flows in different folders
await createLocalFlow(tempDir, "f/included", "flow_a");
await createLocalFlow(tempDir, "f/excluded", "flow_b");
// Run with folder argument (flow folder path - uses .flow suffix by default)
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "f/included/flow_a.flow"],
tempDir,
"folder_flow_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
expect(output).toContain("flow_a");
expect(output).not.toContain("flow_b");
});
});
test("filters to specific app folder", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "folder_app_test");
// Create apps in different folders
await createLocalApp(tempDir, "f/included", "app_a");
await createLocalApp(tempDir, "f/excluded", "app_b");
// Run with folder argument (app folder path - uses .app suffix by default)
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "f/included/app_a.app"],
tempDir,
"folder_app_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
expect(output).toContain("app_a");
expect(output).not.toContain("app_b");
});
});
test("shows up-to-date when folder has no stale items", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "folder_uptodate_test");
await createLocalScript(tempDir, "f/test", "my_script");
// First run to generate metadata
await backend.runCLICommand(
["generate-metadata", "--yes"],
tempDir,
"folder_uptodate_test"
);
// Second run with folder - should be up-to-date
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "f/test/my_script.ts"],
tempDir,
"folder_uptodate_test"
);
expect(result.code).toEqual(0);
expect(result.stdout).toContain("up-to-date");
});
});
test("trailing slash is stripped (matches deprecated behavior)", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "trailing_slash_test");
await createLocalScript(tempDir, "f/test", "my_script");
// Run with trailing slash
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "f/test/my_script.ts/"],
tempDir,
"trailing_slash_test"
);
expect(result.code).toEqual(0);
expect(result.stdout).toContain("my_script");
});
});
test("parent folder matches all children", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "parent_folder_test");
// Create scripts in nested folders
await createLocalScript(tempDir, "f/parent", "script_a");
await createLocalScript(tempDir, "f/parent/child", "script_b");
await createLocalScript(tempDir, "f/other", "script_c");
// Run with parent folder - should match both scripts in f/parent tree
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "f/parent"],
tempDir,
"parent_folder_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
expect(output).toContain("script_a");
expect(output).toContain("script_b");
expect(output).not.toContain("script_c");
});
});
test("non-existent folder shows up-to-date", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "nonexistent_folder_test");
await createLocalScript(tempDir, "f/exists", "my_script");
// Run with non-existent folder
const result = await backend.runCLICommand(
["generate-metadata", "--yes", "f/does_not_exist"],
tempDir,
"nonexistent_folder_test"
);
expect(result.code).toEqual(0);
expect(result.stdout).toContain("up-to-date");
});
});
});
// =============================================================================
// Scripts with modules
// =============================================================================
describe("generate-metadata with script modules", () => {
test("script with modules is detected as a single stale item", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "module_script_test");
// Create a script with module files
await createLocalScriptWithModules(tempDir, "f/test", "order_workflow", "bun", [
{ path: "helper.ts", content: 'export function validate() { return true; }\n' },
{ path: "utils.ts", content: 'export const VERSION = "1.0";\n' },
]);
const result = await backend.runCLICommand(
["generate-metadata", "--dry-run"],
tempDir,
"module_script_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
// The main script should be listed as stale
expect(output).toContain("order_workflow");
// Module files should NOT appear as separate stale scripts (only within [changed modules: ...])
const lines = output.split("\n");
const staleLines = lines.filter((l: string) => l.includes("f/test/"));
expect(staleLines.length).toBe(1);
expect(staleLines[0]).toContain("order_workflow");
});
});
test("module files are not treated as standalone scripts", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "module_not_standalone_test");
// Create a script with modules plus a regular script
await createLocalScriptWithModules(tempDir, "f/test", "my_script", "bun", [
{ path: "helper.ts", content: 'export function greet() { return "hi"; }\n' },
]);
await createLocalScript(tempDir, "f/test", "standalone_script");
const result = await backend.runCLICommand(
["generate-metadata", "--dry-run"],
tempDir,
"module_not_standalone_test"
);
expect(result.code).toEqual(0);
const output = result.stdout + result.stderr;
// Should list both the main script and standalone script
expect(output).toContain("my_script");
expect(output).toContain("standalone_script");
// Should NOT list module helper as a separate entry
// Count occurrences of "Scripts" header — there should be exactly one
expect(output).toContain("Scripts");
});
});
test("script with modules generates metadata and becomes up-to-date", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "module_uptodate_test");
await createLocalScriptWithModules(tempDir, "f/test", "my_script", "bun", [
{ path: "helper.ts", content: 'export function greet() { return "hi"; }\n' },
]);
// First run — should find stale items and generate metadata
const result1 = await backend.runCLICommand(
["generate-metadata", "--yes"],
tempDir,
"module_uptodate_test"
);
expect(result1.code).toEqual(0);
expect(result1.stdout).toContain("Done");
// Second run — should be up-to-date
const result2 = await backend.runCLICommand(
["generate-metadata", "--yes"],
tempDir,
"module_uptodate_test"
);
expect(result2.code).toEqual(0);
expect(result2.stdout).toContain("up-to-date");
});
});
test("modifying a module re-triggers stale detection", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "module_modify_test");
await createLocalScriptWithModules(tempDir, "f/test", "order_workflow", "bun", [
{ path: "helper.ts", content: 'export function greet() { return "hi"; }\n' },
{ path: "utils.ts", content: 'export const VERSION = "1.0";\n' },
]);
// First run — generate metadata
const result1 = await backend.runCLICommand(
["generate-metadata", "--yes"],
tempDir,
"module_modify_test"
);
expect(result1.code).toEqual(0);
// Second run — should be up-to-date
const result2 = await backend.runCLICommand(
["generate-metadata", "--dry-run"],
tempDir,
"module_modify_test"
);
expect(result2.code).toEqual(0);
const output2 = result2.stdout + result2.stderr;
expect(output2).not.toContain("order_workflow");
// Modify one module
await writeFile(
`${tempDir}/f/test/order_workflow__mod/helper.ts`,
'export function greet() { return "hello world"; }\n',
"utf-8"
);
// Third run — should detect the script as stale with the changed module
const result3 = await backend.runCLICommand(
["generate-metadata", "--dry-run"],
tempDir,
"module_modify_test"
);
expect(result3.code).toEqual(0);
const output3 = result3.stdout + result3.stderr;
expect(output3).toContain("order_workflow");
expect(output3).toContain("helper.ts");
// utils.ts was not modified, should not be listed as changed
expect(output3).not.toContain("utils.ts");
});
});
test("script with modules and lock files does not crash", async () => {
await withTestBackend(async (backend, tempDir) => {
await setupWorkspace(backend, tempDir, "module_with_locks_test");
// Create a script with modules that have lock files
await createLocalScriptWithModules(tempDir, "f/test", "my_script", "bun", [
{
path: "helper.ts",
content: 'import lodash from "lodash";\nexport const x = lodash.identity(1);\n',
lock: "lodash@4.17.21\n",
},
]);
// Should not crash on lock files inside __mod/
const result = await backend.runCLICommand(
["generate-metadata", "--dry-run"],
tempDir,
"module_with_locks_test"
);
expect(result.code).toEqual(0);
// Should find the main script as stale, not crash on .lock files
expect(result.stdout).toContain("my_script");
});
});
});