import { getImageTag, getImageUri } from './GPDDKHCR.js';
import { init_esm_shims, __name } from './6AHA7PAZ.js';
import { execFile } from 'child_process';
import { promisify } from 'util';
import * as z from 'zod';

// src/test.ts
init_esm_shims();
var COMMAND_TIMEOUT = 5 * 6e4;
var MANIFEST_TEST_TIMEOUT = 6e4;
var VERSION_TEST_TIMEOUT = 15 * 6e4;
var PULL_TIMEOUT = 10 * 6e4;
var slsaSchema = z.object({
  SLSA: z.object({})
});
var provenanceSchema = z.object({
  ["linux/amd64"]: slsaSchema,
  ["linux/arm64"]: slsaSchema
});
var runCommand = /* @__PURE__ */ __name(async (command, args, timeout = COMMAND_TIMEOUT) => (await promisify(execFile)(command, args, {
  timeout,
  killSignal: "SIGKILL"
})).stdout.trim(), "runCommand");
var isImageCached = /* @__PURE__ */ __name(async (uri) => {
  try {
    await runCommand("docker", [
      "image",
      "inspect",
      uri
    ]);
    return true;
  } catch {
    return false;
  }
}, "isImageCached");
var pullLocks = /* @__PURE__ */ new Map();
var ensureImagePulled = /* @__PURE__ */ __name(async (uri) => {
  const existingPull = pullLocks.get(uri);
  if (existingPull) {
    console.log(`\u23F3 Waiting for concurrent pull: ${uri}`);
    return existingPull;
  }
  const pullPromise = (async () => {
    if (await isImageCached(uri)) {
      console.log(`\u2705 Image already cached: ${uri}`);
      return;
    }
    console.log(`\u{1F4E5} Pulling image: ${uri}`);
    await runCommand("docker", [
      "pull",
      uri
    ], PULL_TIMEOUT);
  })().finally(() => pullLocks.delete(uri));
  pullLocks.set(uri, pullPromise);
  return pullPromise;
}, "ensureImagePulled");
var testTools = /* @__PURE__ */ __name(({ describe, expect, it, beforeAll }, images, _versionCombinations, versionCommands) => {
  describe("Docker image IDs", () => {
    for (const [name, image] of Object.entries(images)) {
      it(`has an image ID of ${name}`, () => {
        expect([
          image.name,
          getImageTag(image, "-")
        ].join(":")).toBe(name);
      });
    }
  });
  describe("Tool versions", () => {
    for (const literalImage of Object.values(images)) {
      const image = literalImage;
      if (image.unreleased) {
        continue;
      }
      describe(getImageTag(image), () => {
        let imageUri;
        beforeAll(async () => {
          imageUri = await getImageUri(image, "_");
          await ensureImagePulled(imageUri);
        }, PULL_TIMEOUT);
        for (const [tool, expectedVersion] of Object.entries(image.versions)) {
          it(`should have ${tool} of version ${expectedVersion}`, async () => {
            if (!(versionCommands[tool] instanceof Array)) {
              throw new Error("Missing version command");
            }
            const version = await runCommand("docker", [
              "run",
              "--rm",
              "--privileged",
              imageUri,
              ...versionCommands[tool]
            ]);
            expect(version).toContain(expectedVersion);
          }, VERSION_TEST_TIMEOUT);
        }
      });
    }
  });
  describe("Docker image manifests", () => {
    for (const [id, image] of Object.entries(images)) {
      if (image.unreleased) {
        continue;
      }
      it(`has a valid manifest for ${id}`, async () => {
        const { stdout } = await promisify(execFile)("docker", [
          "buildx",
          "imagetools",
          "inspect",
          "--format",
          "{{ json .Provenance }}",
          await getImageUri(image, "_")
        ]);
        expect(provenanceSchema.safeParse(JSON.parse(stdout)).success).toBe(true);
      }, MANIFEST_TEST_TIMEOUT);
    }
  });
}, "testTools");

export { testTools };
//# sourceMappingURL=PKAOFTZV.js.map
//# sourceMappingURL=PKAOFTZV.js.map