// MUST be first: sets DATABASE_URL before anything that could pull in db_client. import "./setup_env"; import { describe, expect, test } from "bun:test"; import { signatureKind, isValidSignature, problemMatches, shouldAutoDisable, stateAfterVerification, canTransition, validateCandidate, fixIdFor, payloadDigest, manifestBytes, validateRelease, needsOfficialRelease, } from "../src/eddy_rules"; describe("signature kinds", () => { test("login: is the wire prefix of login-chain", () => { expect(signatureKind("login:-34018>tg-2>acct-7")).toBe("login-chain"); expect(signatureKind("crash:EXC_BAD_ACCESS:3f9a12c0")).toBe("crash"); expect(signatureKind("layout:CreatorInboxVC:TabCell:equal-min-width")).toBe("layout"); expect(signatureKind("nonsense")).toBeNull(); expect(isValidSignature("hang:main-thread")).toBe(true); expect(isValidSignature("bogus:x")).toBe(false); }); test("a merged problem matches through its aliases", () => { const p = { signature: "layout:VC:Cell:clipped", aliases: ["layout:VC:Cell:overlap"] }; expect(problemMatches(p, "layout:VC:Cell:clipped")).toBe(true); expect(problemMatches(p, "layout:VC:Cell:overlap")).toBe(true); expect(problemMatches(p, "layout:VC:Cell:missing")).toBe(false); }); }); describe("state machine", () => { test("auto-disable: two regressing machines, or a failing majority of ≥5 samples", () => { expect(shouldAutoDisable({ installed: 10, stillFailing: 0, regression: 1 }, 1)).toBe(false); expect(shouldAutoDisable({ installed: 10, stillFailing: 0, regression: 2 }, 2)).toBe(true); expect(shouldAutoDisable({ installed: 2, stillFailing: 2, regression: 0 }, 0)).toBe(false); // < 5 samples expect(shouldAutoDisable({ installed: 2, stillFailing: 3, regression: 0 }, 0)).toBe(true); expect(shouldAutoDisable({ installed: 3, stillFailing: 3, regression: 0 }, 0)).toBe(false); // exactly half is not a majority }); test("verification outcome per tier", () => { expect(stateAfterVerification("launch-config", true)).toBe("verified"); expect(stateAfterVerification("frida-guard", true)).toBe("verified"); expect(stateAfterVerification("native-dylib", true)).toBe("verified"); expect(stateAfterVerification("frida-guard", false)).toBe("rejected"); }); test("manual transitions", () => { expect(canTransition("verified", "published")).toBe(true); expect(canTransition("published", "disabled")).toBe(true); expect(canTransition("retired", "published")).toBe(false); expect(canTransition("candidate", "retired")).toBe(false); }); }); describe("candidate validation", () => { const base = { problem: { signature: "assert:9b7e0000" }, target: { baseline: "tiktokm-47.0.0-470000-af7106c6", bundle: "x" } }; test("advisory needs text, executable tiers need a payload with matching contract", () => { expect(validateCandidate({ ...base, tier: "advisory" }).ok).toBe(false); expect(validateCandidate({ ...base, tier: "advisory", advisory: "先关弹窗" }).ok).toBe(true); const payload_b64 = Buffer.from("return 1").toString("base64"); const guard = { ...base, tier: "frida-guard", payload: { path: "guard.js", sha256: payloadDigest(payload_b64) }, payload_b64 }; const ok = validateCandidate(guard); expect(ok.ok).toBe(true); if (ok.ok) { expect(ok.value.runtimeContract).toBe("eddy-compat-js-v1"); expect(ok.value.problem.kind).toBe("assert"); // derived from the signature, never trusted from the client } expect(validateCandidate({ ...guard, runtimeContract: "wrong" }).ok).toBe(false); expect(validateCandidate({ ...guard, problem: { signature: "nope" } }).ok).toBe(false); expect(validateCandidate({ ...guard, problem: { signature: "assert:9b7e0000", kind: "crash" } }).ok).toBe(false); expect(validateCandidate({ ...guard, payload: { path: "guard.js", sha256: "abc" } }).ok).toBe(false); expect(validateCandidate({ ...guard, tier: "native-dylib", payload: undefined }).ok).toBe(false); }); test("ids and digests are deterministic", () => { expect(fixIdFor("b", "s", "frida-guard")).toBe(fixIdFor("b", "s", "frida-guard")); expect(fixIdFor("b", "s", "frida-guard")).not.toBe(fixIdFor("b", "s", "launch-config")); expect(payloadDigest(Buffer.from("hello").toString("base64"))).toBe("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"); expect(manifestBytes({ b: 1, a: { d: 2, c: [3] } })).toBe('{\n "a": {\n "c": [\n 3\n ],\n "d": 2\n },\n "b": 1\n}'); }); }); describe("official releases", () => { test("knowledge submission is independent of official baseline release", () => { expect(needsOfficialRelease("frida-guard")).toBe(false); expect(needsOfficialRelease("native-dylib")).toBe(false); expect(needsOfficialRelease("launch-config")).toBe(false); expect(needsOfficialRelease("advisory")).toBe(false); }); test("a release body is an https artifact with a real digest and size", () => { const sha = "a".repeat(64); const ok = validateRelease({ artifact: { url: "https://tosv.byted.org/obj/x/baseline-tiktokm.zip", sha256: sha, bytes: 12 }, notes: " n " }); expect(ok.ok).toBe(true); if (ok.ok) { expect(ok.value.state).toBe("published"); expect(ok.value.notes).toBe("n"); expect(ok.value.eddyRelease).toBeNull(); } expect(validateRelease({ artifact: { url: "http://x/y.zip", sha256: sha, bytes: 1 } }).ok).toBe(false); expect(validateRelease({ artifact: { url: "https://x/y.zip", sha256: "abc", bytes: 1 } }).ok).toBe(false); expect(validateRelease({ artifact: { url: "https://x/y.zip", sha256: sha, bytes: 0 } }).ok).toBe(false); expect(validateRelease({ artifact: { url: "https://x/y.zip", sha256: sha, bytes: 1 }, state: "draft" }).ok).toBe(false); expect(validateRelease({}).ok).toBe(false); }); test("a source snapshot is optional, but when given it needs the same digest fields plus a commit", () => { const sha = "b".repeat(64); const artifact = { url: "https://tosv.byted.org/obj/x/baseline-tiktokm.zip", sha256: "a".repeat(64), bytes: 12 }; const none = validateRelease({ artifact }); expect(none.ok && none.value.source).toBeNull(); const nul = validateRelease({ artifact, source: null }); expect(nul.ok && nul.value.source).toBeNull(); const ok = validateRelease({ artifact, source: { url: "https://tosv.byted.org/obj/x/baseline-tiktokm-source.zip", sha256: sha, bytes: 380_000_000, commit: "74A3D8998CC6", repo: "git@code.byted.org:ugc/TikTok.git" } }); expect(ok.ok).toBe(true); if (ok.ok) { expect(ok.value.source?.commit).toBe("74a3d8998cc6"); expect(ok.value.source?.repo).toBe("git@code.byted.org:ugc/TikTok.git"); expect(ok.value.source?.bytes).toBe(380_000_000); } expect(validateRelease({ artifact, source: { url: "https://x/s.zip", sha256: sha, bytes: 1 } }).ok).toBe(false); // no commit expect(validateRelease({ artifact, source: { url: "https://x/s.zip", sha256: sha, bytes: 1, commit: "xyz" } }).ok).toBe(false); expect(validateRelease({ artifact, source: { url: "http://x/s.zip", sha256: sha, bytes: 1, commit: "abcdef0" } }).ok).toBe(false); expect(validateRelease({ artifact, source: { url: "https://x/s.zip", sha256: "nope", bytes: 1, commit: "abcdef0" } }).ok).toBe(false); }); });