package p.studio.api.automation; import p.studio.api.values.ValueMaps; import java.util.*; /** Neutral immutable visual evidence. Image bytes and historical identities never change. */ public final class Evidence { private Evidence() {} public static final long DEFAULT_QUOTA = 512L * 1024 * 1024; public static final int MAX_READ = 1024 * 1024; public record Pixels(int width, int height, byte[] rgba) { public Pixels { if (width <= 0 || height <= 0 || (long) width * height > 16_777_216 || (long) width * height * 4 != rgba.length) throw new IllegalArgumentException("Invalid RGBA8 layout"); rgba = rgba.clone(); } @Override public byte[] rgba() { return rgba.clone(); } public int channel(int x, int y, int channel) { if (x < 0 || y < 0 || x >= width || y >= height || channel < 0 || channel > 3) throw new IllegalArgumentException("Invalid RGBA coordinate"); return Byte.toUnsignedInt(rgba[(y * width + x) * 4 + channel]); } } public record Rect(int x, int y, int width, int height) { public Rect { if (x < 0 || y < 0 || width <= 0 || height <= 0) throw new IllegalArgumentException("Invalid rectangle"); } public boolean contains(int px, int py) { return px >= x && py >= y && (long)px < (long)x + width && (long)py < (long)y + height; } public void requireInside(int w, int h) { if ((long)x + width > w || (long)y + height > h) throw new IllegalArgumentException("Rectangle exceeds image bounds"); } } public enum Alpha { compare, ignore } public record Parameters(Rect roi, List masks, Alpha alpha, int channelLimit, long allowedDifferentPixels) { public Parameters { Objects.requireNonNull(alpha, "Explicit alpha policy required"); masks = List.copyOf(masks == null ? List.of() : masks); if (masks.size() > 1024 || channelLimit < 0 || channelLimit > 255 || allowedDifferentPixels < 0) throw new IllegalArgumentException("Invalid comparison parameters"); } public static Parameters exact(Alpha alpha) { return new Parameters(null, List.of(), alpha, 0, 0); } } public record Artifact(String id, String publicationId, String sha256, long byteSize, String mime, int width, int height, Map provenance) { public Artifact { Objects.requireNonNull(id); Objects.requireNonNull(publicationId); Objects.requireNonNull(sha256); if (!id.matches("[a-f0-9-]{36}") || !publicationId.matches("[a-f0-9-]{36}") || !sha256.matches("[a-f0-9]{64}") || byteSize <= 0 || byteSize > 32L*1024*1024 || !"image/png".equals(mime) || width <= 0 || height <= 0 || (long)width*height > 16_777_216) throw new IllegalArgumentException("Invalid evidence metadata"); provenance = normalizeProvenance(provenance); } } public enum Verdict { passed, rejected, dimension_mismatch } public record Report(String id, String expectedId, String actualId, String diffId, String expectedSha256, String actualSha256, Parameters parameters, Verdict verdict, long comparedPixels, long differentPixels, int maximumChannelError, Rect boundingBox) {} public record Baseline(String id, String previousRevision, String artifactId, Map origin, String reviewedAt) { public Baseline { origin = normalizeProvenance(origin); } } public record Publication(String id, String requestId, String parameterKey, String projectRoot, String createdAt, List artifacts, Report report, Baseline baseline) { public Publication { artifacts = List.copyOf(artifacts); } } public record Blob(Artifact artifact, byte[] bytes) { public Blob { bytes = bytes.clone(); } @Override public byte[] bytes() { return bytes.clone(); } } public record View(Artifact artifact, boolean stale) {} public record Read(String artifactId, long offset, long totalBytes, byte[] bytes) { public Read { bytes = bytes.clone(); } @Override public byte[] bytes() { return bytes.clone(); } } public record ImportedReference(byte[] bytes, String canonicalPath, boolean humanAuthorized) { public ImportedReference { bytes = bytes.clone(); Objects.requireNonNull(canonicalPath); } @Override public byte[] bytes() { return bytes.clone(); } } private static Map normalizeProvenance(Map values) { var normalized = new LinkedHashMap(values); for (var key : List.of("logicalFrameIndex", "frameId")) if (normalized.get(key) != null) { var value = new java.math.BigInteger(normalized.get(key).toString()); if (value.signum() < 0 || value.bitLength() > 64) throw new IllegalArgumentException("Frame identity must be uint64"); normalized.put(key, value); } return ValueMaps.object(normalized); } }