Files
windmill/cli/test/elements_to_map_branch_specific.test.ts
Ruben Fiszel a2cefdf0a2 refactor(cli): migrate CLI from Deno to Bun/Node.js (#8041)
* fix: only enable EE features in test backend when license key is available

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

* fix: skip EE tests without license key and exclude test-skills from test discovery

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

* fix: unskip passing tests and add duplicate (remote, workspaceId) check in addWorkspace

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

* refactor(cli): migrate from Deno APIs to Node.js/Bun-compatible APIs

Replace Deno-specific APIs with Node.js equivalents across the entire CLI
codebase to enable running on Node.js/Bun. Switch build system from dnt
to bun, update imports from jsr:/npm: prefixed to bare specifiers, and
add package.json/tsconfig.json for the Node.js ecosystem.

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

* all

* test(cli): expand test coverage with new integration and unit tests

Add standalone_commands.test.ts covering folder list, schedule list,
resource-type list/push/update, script show/run/bootstrap, and user
commands. Add unit tests for filePathExtensionFromContentType and
removeExtensionToPath. Add git_unit, local_encryption_unit,
resource_folders_unit, and settings_unit test files. Fix schedule
cron expressions (6-field format), add includeSchedules flag, improve
test setup with pre-build and auto-cleanup, and support TEST_CLI_RUNTIME=node.

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

* fix(cli): replace Deno.readFile with node:fs in WASM loaders and add schema parsing tests

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

* refactor(cli): switch WASM parsers from local files to npm packages

Use published windmill-parser-wasm-* npm packages instead of local
wasm/ files. A loadParser() helper uses createRequire to resolve the
.wasm binary from node_modules and passes it to init() via
readFileSync, avoiding fetch() and Deno.readFile() patches.

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

* test(cli): add coverage for --locks-required lint feature

Add 15 tests covering the lock-checking functionality merged from main:
- checkMissingLocks: standalone scripts (python, bun, bash), inline
  lock file resolution (valid, empty, missing), flow inline rawscripts
  (with/without locks, nested forloopflow), app inline scripts, raw
  apps without backend folder
- runLint --locks-required integration: reports issues when locks
  missing, skips checks when flag absent, passes when locks exist

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

* ci(cli): replace Deno with Bun in CI workflows

- cli-tests.yml: remove Deno setup, use `bun test` instead of
  `deno test`, add `bun install` step for dependency installation
- npm_on_release.yml: replace Deno setup with Bun setup for CLI
  publishing
- build.sh: add `bun install` before building so CI has dependencies

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

* fix(cli): pre-start backend in test preload and remove Deno test leftovers

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

* fix(cli): normalize path separators for Windows compatibility

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

* more tests + windows

* ci(cli): use Blacksmith runner for Windows tests

Switch test-windows job from windows-latest to blacksmith-16vcpu-windows-2025
for faster CI execution.

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

* fix(cli): fix Windows path separator expectations in unit tests

buildMetadataPath and extractResourceName normalize to forward slashes
internally, so tests should not expect platform-specific separators in
their output.

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

* fix(cli): fix Windows CI test failures for dev_server and script_run

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

* fix(cli): set BUN_PATH and NODE_BIN_PATH for backend worker on Windows

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

* ci(cli): add SSH debug step on Windows test failure

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

* fix(cli): use native path separators for ignore check in dev mode on Windows

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 21:19:04 +00:00

426 lines
12 KiB
TypeScript

import { expect, test } from "bun:test";
// Import the function we need to test
import { elementsToMap } from "../src/commands/sync/sync.ts";
import type { SpecificItemsConfig } from "../src/core/specific_items.ts";
// =============================================================================
// elementsToMap TESTS FOR BRANCH-SPECIFIC ITEMS
// Tests for the regression where remote base files were incorrectly skipped
// when configured as branch-specific, causing them to be marked for deletion
// on pull operations.
//
// Regression: PR #7643 (commit 287b7e7d9, Jan 21, 2026)
// =============================================================================
/**
* Mock DynFSElement implementation for testing
*/
interface MockFile {
path: string;
content: string;
isDirectory?: boolean;
}
function createMockDynFSElement(files: MockFile[]): {
isDirectory: boolean;
path: string;
getContentText(): Promise<string>;
getChildren(): AsyncIterable<{
isDirectory: boolean;
path: string;
getContentText(): Promise<string>;
getChildren(): AsyncIterable<unknown>;
}>;
} {
return {
isDirectory: true,
path: "",
async getContentText() {
return "";
},
async *getChildren() {
for (const file of files) {
yield {
isDirectory: file.isDirectory ?? false,
path: file.path,
async getContentText() {
return file.content;
},
async *getChildren() {
// No children for files
},
};
}
},
};
}
const noIgnore = () => false;
const defaultSkips = {};
// =============================================================================
// REGRESSION TEST: Remote base files should NOT be skipped
// =============================================================================
test("elementsToMap: remote base file is NOT skipped when configured as branch-specific (isRemote=true)", async () => {
// This is the key regression test.
// When pulling from remote, the workspace only has base paths (e.g., TestVar.variable.yaml)
// These should NOT be skipped even if configured as branch-specific, because the remote
// workspace doesn't have branch-specific file naming.
const config: SpecificItemsConfig = {
variables: ["f/Shared/Variable/**"],
};
const remoteFiles: MockFile[] = [
{
path: "f/Shared/Variable/TestVar.variable.yaml",
content: "value: test\nis_secret: false",
},
];
const mockElement = createMockDynFSElement(remoteFiles);
// When isRemote=true, base files should be included even if they match branch-specific config
const result = await elementsToMap(
mockElement,
noIgnore,
false,
defaultSkips,
config,
"staging", // branchOverride
true, // isRemote = true
);
// The base file should be in the map
expect(Object.keys(result).includes("f/Shared/Variable/TestVar.variable.yaml")).toEqual(true);
});
test("elementsToMap: local base file IS skipped when configured as branch-specific (isRemote=false)", async () => {
// When processing local files, if a base file is configured as branch-specific,
// it should be skipped because we expect the branch-specific version to be used instead.
const config: SpecificItemsConfig = {
variables: ["f/Shared/Variable/**"],
};
const localFiles: MockFile[] = [
{
path: "f/Shared/Variable/TestVar.variable.yaml",
content: "value: test\nis_secret: false",
},
];
const mockElement = createMockDynFSElement(localFiles);
// When isRemote=false, base files should be skipped if configured as branch-specific
const result = await elementsToMap(
mockElement,
noIgnore,
false,
defaultSkips,
config,
"staging", // branchOverride
false, // isRemote = false
);
// The base file should NOT be in the map (skipped because branch-specific expected)
expect(Object.keys(result).includes("f/Shared/Variable/TestVar.variable.yaml")).toEqual(false);
});
test("elementsToMap: local branch-specific file is mapped to base path (isRemote=false)", async () => {
// When processing local files with branch-specific naming, they should be mapped to base paths
const config: SpecificItemsConfig = {
variables: ["f/Shared/Variable/**"],
};
const localFiles: MockFile[] = [
{
path: "f/Shared/Variable/TestVar.staging.variable.yaml",
content: "value: staging-test\nis_secret: false",
},
];
const mockElement = createMockDynFSElement(localFiles);
const result = await elementsToMap(
mockElement,
noIgnore,
false,
defaultSkips,
config,
"staging", // branchOverride
false, // isRemote = false
);
// The branch-specific file should be mapped to the base path
expect(Object.keys(result).includes("f/Shared/Variable/TestVar.variable.yaml")).toEqual(true);
expect(result["f/Shared/Variable/TestVar.variable.yaml"]).toEqual("value: staging-test\nis_secret: false");
});
// =============================================================================
// PULL SCENARIO: Remote (base path) vs Local (branch-specific path)
// This simulates the actual pull scenario where:
// - Remote has: f/Shared/Variable/TestVar.variable.yaml
// - Local has: f/Shared/Variable/TestVar.staging.variable.yaml
// - Expected: No deletion, the files should match
// =============================================================================
test("elementsToMap: pull scenario - remote and local maps should align correctly", async () => {
const config: SpecificItemsConfig = {
variables: ["f/Shared/Variable/**"],
};
// Remote workspace has base path
const remoteFiles: MockFile[] = [
{
path: "f/Shared/Variable/TestVar.variable.yaml",
content: "value: test\nis_secret: false",
},
];
// Local has branch-specific path
const localFiles: MockFile[] = [
{
path: "f/Shared/Variable/TestVar.staging.variable.yaml",
content: "value: staging-test\nis_secret: false",
},
];
const remoteElement = createMockDynFSElement(remoteFiles);
const localElement = createMockDynFSElement(localFiles);
// Process remote (isRemote=true)
const remoteMap = await elementsToMap(
remoteElement,
noIgnore,
false,
defaultSkips,
config,
"staging",
true, // isRemote
);
// Process local (isRemote=false)
const localMap = await elementsToMap(
localElement,
noIgnore,
false,
defaultSkips,
config,
"staging",
false, // isRemote
);
// Both maps should have the same base path key
const remoteKeys = Object.keys(remoteMap);
const localKeys = Object.keys(localMap);
expect(remoteKeys.includes("f/Shared/Variable/TestVar.variable.yaml")).toEqual(true);
expect(localKeys.includes("f/Shared/Variable/TestVar.variable.yaml")).toEqual(true);
});
// =============================================================================
// NON-CONFIGURED ITEMS: Should work the same regardless of isRemote
// =============================================================================
test("elementsToMap: non-configured items included regardless of isRemote", async () => {
const config: SpecificItemsConfig = {
variables: ["f/Other/**"], // Only "Other" folder is branch-specific
};
const files: MockFile[] = [
{
path: "f/Shared/Variable/TestVar.variable.yaml",
content: "value: test\nis_secret: false",
},
];
const mockElement = createMockDynFSElement(files);
// Test with isRemote=true
const remoteResult = await elementsToMap(
mockElement,
noIgnore,
false,
defaultSkips,
config,
"staging",
true,
);
// Test with isRemote=false
const localResult = await elementsToMap(
createMockDynFSElement(files),
noIgnore,
false,
defaultSkips,
config,
"staging",
false,
);
// Both should include the file since it's not in the branch-specific config
expect(Object.keys(remoteResult).includes("f/Shared/Variable/TestVar.variable.yaml")).toEqual(true);
expect(Object.keys(localResult).includes("f/Shared/Variable/TestVar.variable.yaml")).toEqual(true);
});
// =============================================================================
// RESOURCE TYPE TESTS
// =============================================================================
test("elementsToMap: remote resource base file not skipped when configured", async () => {
const config: SpecificItemsConfig = {
resources: ["f/db/**"],
};
const remoteFiles: MockFile[] = [
{
path: "f/db/connection.resource.yaml",
content: "value: { host: localhost }",
},
];
const mockElement = createMockDynFSElement(remoteFiles);
const result = await elementsToMap(
mockElement,
noIgnore,
false,
defaultSkips,
config,
"staging",
true, // isRemote
);
expect(Object.keys(result).includes("f/db/connection.resource.yaml")).toEqual(true);
});
// =============================================================================
// TRIGGER TYPE TESTS
// =============================================================================
test("elementsToMap: remote trigger base file not skipped when configured", async () => {
const config: SpecificItemsConfig = {
triggers: ["f/webhooks/**"],
};
const remoteFiles: MockFile[] = [
{
path: "f/webhooks/handler.http_trigger.yaml",
content: "path: /webhook",
},
];
const mockElement = createMockDynFSElement(remoteFiles);
const result = await elementsToMap(
mockElement,
noIgnore,
false,
{ includeTriggers: true }, // Must include triggers explicitly
config,
"staging",
true, // isRemote
);
expect(Object.keys(result).includes("f/webhooks/handler.http_trigger.yaml")).toEqual(true);
});
// =============================================================================
// SETTINGS TYPE TESTS
// =============================================================================
test("elementsToMap: remote settings.yaml not skipped when configured", async () => {
const config: SpecificItemsConfig = {
settings: true,
};
const remoteFiles: MockFile[] = [
{
path: "settings.yaml",
content: "openai_resource_path: null",
},
];
const mockElement = createMockDynFSElement(remoteFiles);
const result = await elementsToMap(
mockElement,
noIgnore,
false,
{ includeSettings: true },
config,
"staging",
true, // isRemote
);
expect(Object.keys(result).includes("settings.yaml")).toEqual(true);
});
// =============================================================================
// FOLDER TYPE TESTS
// =============================================================================
test("elementsToMap: remote folder meta not skipped when configured", async () => {
const config: SpecificItemsConfig = {
folders: ["f/env_*"],
};
const remoteFiles: MockFile[] = [
{
path: "f/env_staging/folder.meta.yaml",
content: "display_name: Staging Environment",
},
];
const mockElement = createMockDynFSElement(remoteFiles);
const result = await elementsToMap(
mockElement,
noIgnore,
false,
defaultSkips,
config,
"staging",
true, // isRemote
);
expect(Object.keys(result).includes("f/env_staging/folder.meta.yaml")).toEqual(true);
});
// =============================================================================
// BACKWARD COMPATIBILITY: isRemote undefined behaves like local (false)
// =============================================================================
test("elementsToMap: isRemote undefined behaves like local (backward compatible)", async () => {
const config: SpecificItemsConfig = {
variables: ["f/**"],
};
const files: MockFile[] = [
{
path: "f/test.variable.yaml",
content: "value: test\nis_secret: false",
},
];
const mockElement = createMockDynFSElement(files);
// When isRemote is undefined (backward compatibility), it should behave like local
const result = await elementsToMap(
mockElement,
noIgnore,
false,
defaultSkips,
config,
"staging",
// isRemote omitted
);
// Base file should be skipped (same behavior as isRemote=false)
expect(Object.keys(result).includes("f/test.variable.yaml")).toEqual(false);
});