'use strict';

var _533SFXIE_cjs = require('./533SFXIE.cjs');
var _24WEKBY3_cjs = require('./24WEKBY3.cjs');
var child_process = require('child_process');
var util = require('util');
var z = require('zod');

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var z__namespace = /*#__PURE__*/_interopNamespace(z);

// src/test.ts
_24WEKBY3_cjs.init_cjs_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__namespace.object({
  SLSA: z__namespace.object({})
});
var provenanceSchema = z__namespace.object({
  ["linux/amd64"]: slsaSchema,
  ["linux/arm64"]: slsaSchema
});
var runCommand = /* @__PURE__ */ _24WEKBY3_cjs.__name(async (command, args, timeout = COMMAND_TIMEOUT) => (await util.promisify(child_process.execFile)(command, args, {
  timeout,
  killSignal: "SIGKILL"
})).stdout.trim(), "runCommand");
var isImageCached = /* @__PURE__ */ _24WEKBY3_cjs.__name(async (uri) => {
  try {
    await runCommand("docker", [
      "image",
      "inspect",
      uri
    ]);
    return true;
  } catch {
    return false;
  }
}, "isImageCached");
var pullLocks = /* @__PURE__ */ new Map();
var ensureImagePulled = /* @__PURE__ */ _24WEKBY3_cjs.__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__ */ _24WEKBY3_cjs.__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,
          _533SFXIE_cjs.getImageTag(image, "-")
        ].join(":")).toBe(name);
      });
    }
  });
  describe("Tool versions", () => {
    for (const literalImage of Object.values(images)) {
      const image = literalImage;
      if (image.unreleased) {
        continue;
      }
      describe(_533SFXIE_cjs.getImageTag(image), () => {
        let imageUri;
        beforeAll(async () => {
          imageUri = await _533SFXIE_cjs.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 util.promisify(child_process.execFile)("docker", [
          "buildx",
          "imagetools",
          "inspect",
          "--format",
          "{{ json .Provenance }}",
          await _533SFXIE_cjs.getImageUri(image, "_")
        ]);
        expect(provenanceSchema.safeParse(JSON.parse(stdout)).success).toBe(true);
      }, MANIFEST_TEST_TIMEOUT);
    }
  });
}, "testTools");

exports.testTools = testTools;
//# sourceMappingURL=Q6HPQZPT.cjs.map
//# sourceMappingURL=Q6HPQZPT.cjs.map