## Tier C tests for the CHIMERA G2 identifiability certificate ## (chimera/identifiability.nim, spec ยง12 G2). Pure Nim, no Java. ## Run: from ChimeraBot_garage/src, `nimble test`. import std/[math, strutils, sequtils] import chimera/dsl import chimera/identifiability var failed = 0 proc check(name: string, cond: bool, detail: string = "") = if cond: echo " ok ", name else: echo " FAIL ", name, " [", detail, "]"; inc failed let kernels = allKernels() let starts = canonicalStarts() let alphabet = probeAlphabet() proc serialize(pairs: seq[PairResult]): string = for p in pairs: result.add p.ki & "|" & p.kj & "|" & $p.identifiable & "|" & $p.lStar & "|" & formatFloat(p.separation, ffDecimal, 6) & "|" & p.prefix & "\n" block: echo "g2 1: adapter + total variation sanity" check("eight canonical starts", starts.len == 8) check("seven probe types", alphabet.len == 7) let labels = alphabet.mapIt(it.label) for want in ["noop", "stop", "reverse", "speedmod", "flip", "distance", "gun"]: check("alphabet has " & want, want in labels) # same key => TV 0; disjoint => TV 1. let same = @[(0, 0, 0), (1, 2, 3)] check("TV identical = 0", totalVariation(same, same) == 0.0) check("TV disjoint = 1", totalVariation(@[(0, 0, 0), (0, 0, 0)], @[(9, 9, 9), (9, 9, 9)]) == 1.0) block: echo "g2 2: determinism (two runs byte-equal)" let sub = @[kernels[0], kernels[1], kernels[2]] let a = identifiabilityCertificate(sub, starts, alphabet, maxLen = 3) let b = identifiabilityCertificate(sub, starts, alphabet, maxLen = 3) check("same pair count", a.len == b.len, $a.len & " vs " & $b.len) check("byte-identical certificates", serialize(a) == serialize(b)) check("certificate covers 3 unordered pairs", a.len == 3, $a.len) block: echo "g2 3: separation monotone in L on a separable pair" # ORBIT_LOCK vs ORBIT_BALLIST are obviously separable (tangential orbit vs # constant spin), so the best score over sequences of length <= L cannot fall. let curve = kernelPairCurve(kernels[0], kernels[1], starts, alphabet, maxLen = 6) var monotone = true var detail = "" for L in 1 ..< curve.sep.len: if curve.sep[L] + 1e-12 < curve.sep[L - 1]: monotone = false detail = $L & ": " & $curve.sep[L - 1] & " -> " & $curve.sep[L] check("sep[L] non-decreasing in L", monotone, detail) check("separable pair reaches delta", curve.sep[^1] >= G2Delta, $curve.sep[^1]) check("best prefix is non-empty", curve.prefix[^1].len > 0, curve.prefix[^1]) let r = certifyPair(kernels[0], kernels[1], starts, alphabet) check("separable pair identifiable", r.identifiable) check("minimal length consistent with curve", r.lStar >= 1 and r.lStar <= 6, $r.lStar) block: echo "g2 4: unidentifiable detection on degenerate twin params" # Two ORBIT_LOCK kernels whose range differs by 1e-6: below the intent # discretization, behaviourally indistinguishable within Lmax. var a = kernels[0] a.name = "ORBIT_DEGEN_A" var b = kernels[0] b.name = "ORBIT_DEGEN_B" b.defaultParams.range = a.defaultParams.range + 1e-6 let r = certifyPair(a, b, starts, alphabet) check("degenerate pair UNIDENTIFIABLE", not r.identifiable, r.prefix) check("no separating length recorded", r.lStar == 0, $r.lStar) # And a genuinely different scalar IS flagged: same kernel, range + 20. var c = kernels[0] c.name = "ORBIT_OFF" c.defaultParams.range = a.defaultParams.range + 100.0 let r2 = certifyPair(a, c, starts, alphabet) check("different-range ORBIT identifiable", r2.identifiable, "L=" & $r2.lStar & " sep=" & $r2.separation) echo "" if failed == 0: echo "all identifiability tests passed" else: echo failed, " identifiability test(s) FAILED"; quit(1)