cache-apt-pkgs-action/test/action.test.ts
Andrew Walsh 9792dbe32f
style: set Prettier print width to 110 and reformat (#246)
* style: set Prettier print width to 110 and reformat

Without a repo config, the Prettier CLI and Trunk used the default
width of 80, while editors with a user printWidth reflowed files on
save, producing formatting-only diffs. Pin the width in .prettierrc.json
and reformat source, tests, scripts and JS/TS config files to match.

Files that already failed Prettier at width 80 for unrelated reasons
(tsconfig*.json, .trunk/*, V2_FAQ.MD) are left untouched.

No code changes.

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

* test: cover cache restore and save failure paths

Codecov flagged lines in #246 as uncovered patch lines; they were
untested before the reformat touched them. Add behavior tests for:

- Cache: cache miss, missing or empty manifests, unreadable cache dir,
  metapackage-only restore, install scripts running after their archive
  with dpkg actions, existing archives, unavailable cache service, and
  save failures.
- ManifestEntry.fromJSON round trip.
- buildFileList dropping missing files, updateAptLists skipping update
  for populated lists and searching only to max depth, and fromInput
  ignoring a version without a package name.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 11:45:29 -08:00

113 lines
3.5 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { runAction, type ActionInputs, type ActionOutputs } from "../src/action.js";
import { Manifest, ManifestEntry } from "../src/manifest.js";
import { CacheKey } from "../src/cache.js";
import { createPackageName } from "ts-apt";
import { ActionPackageNames } from "../src/packages.js";
const NATIVE_ARCH = "amd64";
const FOREIGN_ARCH = "i386";
vi.mock("ts-apt", async (importOriginal) => {
const actual = await importOriginal<typeof import("ts-apt")>();
return {
...actual,
createPackageManager: async () => ({
getNativeArchitecture: async () => NATIVE_ARCH,
}),
};
});
describe("runAction", () => {
const PKG_NAME = "curl";
const PKG_VER = "1.2.3";
const PKG2_NAME = "git";
const PKG3_NAME = "jq";
it("returns expected outputs for cache hit", async () => {
const inputs: ActionInputs = {
packages: `${PKG_NAME},${PKG2_NAME},${PKG3_NAME}`,
version: PKG_VER,
executeInstallScripts: false,
emptyPackagesBehavior: "error",
debug: false,
};
const packageNames = ActionPackageNames.fromInput(inputs.packages);
const cacheKey = new CacheKey(inputs.version, "0", process.arch, packageNames);
const manifest = new Manifest(
new Date(),
[
new ManifestEntry(createPackageName(PKG_NAME, PKG_VER, undefined), []),
new ManifestEntry(createPackageName(PKG2_NAME, PKG_VER, undefined), []),
new ManifestEntry(createPackageName(PKG3_NAME, PKG_VER, undefined), []),
],
cacheKey,
);
const mockCache = {
loadAndRestore: async () => manifest,
archiveAndSave: async () => {},
};
const mockCommandRunner = {
run: async () => {},
};
const mockLogger = {
info: () => {},
error: () => {},
debug: () => {},
};
const outputs: ActionOutputs = await runAction(
inputs,
mockCommandRunner as never,
mockCache as never,
mockLogger as never,
);
expect(outputs.cacheHit).toBe(true);
expect(outputs.packageVersionList).toBe(
`${PKG_NAME}=${PKG_VER},${PKG2_NAME}=${PKG_VER},${PKG3_NAME}=${PKG_VER}`,
);
expect(outputs.allPackageVersionList).toBe(
`${PKG_NAME}=${PKG_VER},${PKG2_NAME}=${PKG_VER},${PKG3_NAME}=${PKG_VER}`,
);
});
it("runAction cache hit with dependencies lists requested and all packages sorted with foreign arch only", async () => {
const inputs: ActionInputs = {
packages: `${PKG2_NAME} ${PKG_NAME}`,
version: PKG_VER,
executeInstallScripts: false,
emptyPackagesBehavior: "error",
debug: false,
};
const cacheKey = new CacheKey(
inputs.version,
"0",
process.arch,
ActionPackageNames.fromInput(inputs.packages),
);
// Install order, with a native-arch dependency and a foreign-arch request.
const manifest = new Manifest(
new Date(),
[
new ManifestEntry(createPackageName(PKG2_NAME, PKG_VER, FOREIGN_ARCH)),
new ManifestEntry(createPackageName(PKG3_NAME, PKG_VER, NATIVE_ARCH)),
new ManifestEntry(createPackageName(PKG_NAME, PKG_VER, undefined)),
],
cacheKey,
);
const mockCache = { loadAndRestore: async () => manifest };
const outputs = await runAction(inputs, {} as never, mockCache as never, {} as never);
expect(outputs.packageVersionList).toBe(`${PKG_NAME}=${PKG_VER},${PKG2_NAME}:${FOREIGN_ARCH}=${PKG_VER}`);
expect(outputs.allPackageVersionList).toBe(
`${PKG_NAME}=${PKG_VER},${PKG2_NAME}:${FOREIGN_ARCH}=${PKG_VER},${PKG3_NAME}=${PKG_VER}`,
);
});
});