cache-apt-pkgs-action/test/instrumentation.test.ts
awalsh128 c24c2b9e97 fix: write logs under the home cache directory and await artifact upload
Instruments opened its log files at the relative path cache-apt-pkgs/
before that directory existed, so the action crashed at startup with
ENOENT. It also pointed artifact uploads there while Cache uses
~/cache-apt-pkgs. Resolve both with a shared resolveCacheDir helper and
create the directory before opening the logs.

The artifact upload was also never awaited, so a failed upload became an
unhandled rejection. Await it and report failures as a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 18:22:55 -08:00

105 lines
3.3 KiB
TypeScript

import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { Artifacts, Instruments } from "../src/instrumentation.ts";
import { afterEach, describe, expect, it, vi } from "vitest";
describe("instrumentation", () => {
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllEnvs();
});
const ARTIFACT_FILENAMES = [
"capa_app.log",
"capa_exec.log",
"cache_key.json",
"manifest_main.json",
"manifest_all.json",
];
describe("Artifacts", () => {
it("constructor initializes properties correctly", () => {
const dir = "/path/to/artifacts";
const debug = true;
const runId = "test-run-id";
const artifacts = new Artifacts(dir, ARTIFACT_FILENAMES, debug, runId);
expect(artifacts).toEqual({
dir: dir,
artifactFilenames: ARTIFACT_FILENAMES,
debug: debug,
runId: runId,
});
});
it("constructor generates a random runId when not provided", () => {
const dir = "/path/to/artifacts";
const debug = false;
// GitHub Actions sets this, so clear it to test the fallback.
vi.stubEnv("GITHUB_RUN_ID", undefined);
const artifacts = new Artifacts(dir, ARTIFACT_FILENAMES, debug);
expect(artifacts.runId).toMatch(/^ghrunid-notfound-/);
});
});
describe("Instruments", () => {
const CACHE_DIRNAME = "cache-apt-pkgs";
it("constructor cache dir missing creates it under home with the logs", async () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "instruments-test-"));
vi.stubEnv("HOME", home);
const expectedDir = path.join(home, CACHE_DIRNAME);
const instruments = new Instruments(CACHE_DIRNAME, false);
instruments.appLogger.info("app message");
await new Promise((resolve) => instruments.appLogger.end(resolve));
expect(instruments.cacheDirPath).toBe(expectedDir);
expect(instruments.artifacts.dir).toBe(expectedDir);
await vi.waitFor(() =>
expect(
fs.readFileSync(path.join(expectedDir, "capa_app.log"), "utf8"),
).toContain("app message"),
);
fs.rmSync(home, { recursive: true, force: true });
});
});
// describe("Instruments", () => {
// it("constructor initializes properties correctly", () => {
// const cacheDir = "/path/to/cache";
// const debug = true;
// const artifacts = new Artifacts(
// "/path/to/cache",
// ARTIFACT_FILENAMES,
// debug,
// );
// const instruments = new Instruments(cacheDir, debug, artifacts);
// expect(instruments).toEqual({
// cacheDir: cacheDir,
// debug: debug,
// artifacts: artifacts,
// appLogger: expect.toBeTypeOf("object"),
// execLogger: expect.toBeTypeOf("object"),
// });
// });
// it("constructor initializes artifacts when not provided", () => {
// const cacheDir = "/path/to/cache";
// const debug = false;
// const instruments = new Instruments(cacheDir, debug);
// expect(instruments).toEqual({
// cacheDir: cacheDir,
// debug: debug,
// artifacts: expect.toBeTypeOf("object"),
// appLogger: expect.toBeTypeOf("object"),
// execLogger: expect.toBeTypeOf("object"),
// });
// });
// });
});