Files
ansiblings/packages/nopy/tests/executor.test.ts
T
Benjamin Diedrichsen 587ff2cf47
Publish snapshot / snapshot (push) Failing after 1m58s
Add release pipeline and upgrade toolchain to TypeScript 7
Publishing infrastructure
- Three Gitea workflows: ci.yml (PRs, non-main pushes), publish-snapshot.yml
  (main -> Gitea under dist-tag @main) and release.yml (tags -> Gitea + npmjs)
- Tag-driven releases as <package-dir>-v<version>; the manifest stays the
  source of truth and release.yml refuses to run if tag and manifest disagree
- Every publish is idempotent: each step checks the registry first, so a run
  that fails on the second registry can simply be re-run
- Hard coverage gate (85% branches) shared by CI, the pre-push hook and local
  runs, since the thresholds live in vitest.config.ts rather than a CI flag
- README.PUBLISH.md documents the whole mechanism

Toolchain
- TypeScript 7 native compiler; drop tsgo and ts-node, use tsx for dev runs
- Biome 1.9 -> 2.x, Vitest 1 -> 4, zod 3 -> 4, inquirer 8 -> 14, pnpm 11.17.0
- Replace inquirer-checkbox-plus-prompt, which is peer-capped at inquirer <9,
  with enquirer's AutoComplete; the CubeSelection contract is unchanged
- Stand in for zod 4's removed z.AnyZodObject with a local AnyObjectSchema

Repo hygiene
- Stop tracking dist/; ignore coverage/, *.tsbuildinfo, .npmrc* and release.json
- Drop package-lock.json in favour of pnpm-lock.yaml

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-27 15:17:14 +02:00

176 lines
4.7 KiB
TypeScript

/**
* Tests for nopy.executor module
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
type DeployCall,
type ExecutionResult,
outputExecutionPlan,
summarizeResults,
} from '../src/nopy.executor.js';
/**
* Helper to create a test deploy call
*/
function createTestCall(cube: string, host: string, deps: string[] = []): DeployCall {
return {
cube,
host,
cwd: `/test/${cube}`,
command: ['pyinfra', host, '-y', `${cube}.deploy.py`],
env: { VAR: 'value' },
dependencies: deps,
};
}
/**
* Helper to create a test execution result
*/
function createTestResult(
cube: string,
host: string,
success: boolean,
duration = 1000
): ExecutionResult {
return {
cube,
host,
success,
duration,
...(success ? {} : { error: new Error('Test error') }),
};
}
describe('summarizeResults', () => {
it('summarizes successful results', () => {
const results: ExecutionResult[] = [
createTestResult('cube-a', 'host1', true, 1000),
createTestResult('cube-b', 'host1', true, 2000),
];
const summary = summarizeResults(results);
expect(summary.total).toBe(2);
expect(summary.successful).toBe(2);
expect(summary.failed).toBe(0);
expect(summary.totalDuration).toBe(3000);
expect(summary.failures).toHaveLength(0);
});
it('summarizes failed results', () => {
const results: ExecutionResult[] = [
createTestResult('cube-a', 'host1', true, 1000),
createTestResult('cube-b', 'host1', false, 500),
];
const summary = summarizeResults(results);
expect(summary.total).toBe(2);
expect(summary.successful).toBe(1);
expect(summary.failed).toBe(1);
expect(summary.totalDuration).toBe(1500);
expect(summary.failures).toHaveLength(1);
expect(summary.failures[0].cube).toBe('cube-b');
});
it('handles empty results', () => {
const summary = summarizeResults([]);
expect(summary.total).toBe(0);
expect(summary.successful).toBe(0);
expect(summary.failed).toBe(0);
expect(summary.totalDuration).toBe(0);
});
it('handles all failed results', () => {
const results: ExecutionResult[] = [
createTestResult('cube-a', 'host1', false, 100),
createTestResult('cube-b', 'host1', false, 200),
];
const summary = summarizeResults(results);
expect(summary.successful).toBe(0);
expect(summary.failed).toBe(2);
expect(summary.failures).toHaveLength(2);
});
});
describe('outputExecutionPlan', () => {
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
});
// vitest reuses an existing spy rather than re-wrapping, so recorded calls
// would otherwise leak from one test into the next.
afterEach(() => {
vi.restoreAllMocks();
});
it('outputs text format by default', () => {
const calls = [createTestCall('cube-a', 'host1')];
outputExecutionPlan(calls);
expect(consoleLogSpy).toHaveBeenCalled();
const output = consoleLogSpy.mock.calls.map((c) => c[0]).join('\n');
expect(output).toContain('Execution Plan');
expect(output).toContain('cube-a');
expect(output).toContain('host1');
});
it('outputs JSON format when requested', () => {
const calls = [createTestCall('cube-a', 'host1')];
outputExecutionPlan(calls, true);
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
const output = consoleLogSpy.mock.calls[0][0];
const parsed = JSON.parse(output);
expect(parsed.plan).toHaveLength(1);
expect(parsed.plan[0].cube).toBe('cube-a');
expect(parsed.plan[0].host).toBe('host1');
});
it('masks password variables in text output', () => {
const call: DeployCall = {
...createTestCall('cube-a', 'host1'),
env: { PASSWORD: 'secret', OTHER: 'visible' },
};
outputExecutionPlan([call]);
const output = consoleLogSpy.mock.calls.map((c) => c[0]).join('\n');
expect(output).toContain('********');
expect(output).not.toContain('secret');
expect(output).toContain('visible');
});
it('shows step numbers', () => {
const calls = [createTestCall('cube-a', 'host1'), createTestCall('cube-b', 'host1')];
outputExecutionPlan(calls);
const output = consoleLogSpy.mock.calls.map((c) => c[0]).join('\n');
expect(output).toContain('Step 1');
expect(output).toContain('Step 2');
});
it('shows total count', () => {
const calls = [
createTestCall('cube-a', 'host1'),
createTestCall('cube-b', 'host1'),
createTestCall('cube-c', 'host1'),
];
outputExecutionPlan(calls);
const output = consoleLogSpy.mock.calls.map((c) => c[0]).join('\n');
expect(output).toContain('Total: 3');
});
});