SplashGUI

// Headless assertions for SplashGUI's config store and per-model profiles — no GUI, no Xcode.
// Run via ./run.sh (it handles the config.json backup/fixture dance). Exits non-zero on any failure.
import Foundation
import SwiftUI

let A = "unsloth/Qwen3.8-27B-GGUF:UD-IQ3_XXS"
let B = "prism-ml/Ternary-Bonsai-2-27B-gguf:PQ2_0"
let C = "unsloth/Qwen3.8-27B-GGUF:UD-Q4_K_M"

var failures = 0
func check(_ label: String, _ cond: Bool, _ detail: String = "") {
    print((cond ? "PASS  " : "FAIL  ") + label + (detail.isEmpty ? "" : "  — " + detail))
    if !cond { failures += 1 }
}

@MainActor
func runTests() {
    // A: a pre-profiles config still decodes (no fallback to factory) and migrates.
    let cfg = ConfigStore.shared.load()
    check("old config decodes — typed values survived",
          cfg.run.maxContext == "100K" && cfg.sampling.temperature == 0.23,
          "\(cfg.run.maxContext) / \(cfg.sampling.temperature)")
    check("schemaVersion migrated to 6", cfg.run.schemaVersion == 6, "\(cfg.run.schemaVersion ?? -1)")
    // v2 flipped int8→bf16, v3 flipped bf16→auto; v6 then resolved the still-auto KV because
    // the typed maxContext keeps Auto off. The endpoint that matters: a concrete KV pick.
    check("typed serve fields migrate to Auto off", cfg.run.auto == false)
    check("auto KV resolved on the Auto-off migration",
          cfg.run.kvFormat == "bf16" || cfg.run.kvFormat == "int8", cfg.run.kvFormat)
    check("v4->v5 turned the SSD tier on", cfg.run.maxCacheDisk == "10G", cfg.run.maxCacheDisk)
    check("idle release defaults to 60m", cfg.run.idleRelease == "60m", cfg.run.idleRelease)
    check("persistent cache defaults on", cfg.run.persistentCache == true)
    check("current model got a seeded profile", cfg.modelProfiles[A] != nil)
    check("seed keeps tuned temp 0.23", cfg.modelProfiles[A]?.sampling.temperature == 0.23)
    check("key keeps the :variant suffix", cfg.modelProfiles.keys.contains { $0.contains(":UD-IQ3_XXS") })

    // B: switching to an untuned model starts it on its CARD — the thinking-mode set both cards
    // publish (1.0 / 0.95 / 20, reasoning left at model default), not the old invented numbers.
    let m = AppModel()
    m.config.sampling.temperature = 0.42
    m.config.sampling.reasoningEffort = "low"
    m.config.run.defaultReasoningEffort = "low"
    m.config.run.maxContext = "100K"
    m.config.run.languageOnly = true
    m.selectModelID(B)
    check("modelID switched", m.config.run.modelID == B)
    check("outgoing temp archived", m.config.modelProfiles[A]?.sampling.temperature == 0.42)
    check("outgoing maxContext archived", m.config.modelProfiles[A]?.maxContext == "100K")
    check("Bonsai card temp 1.0", m.config.sampling.temperature == 1.0, "\(m.config.sampling.temperature)")
    check("Bonsai card topP 0.95", m.config.sampling.topP == 0.95, "\(m.config.sampling.topP)")
    check("Bonsai card topK 20", m.config.sampling.topK == 20, "\(m.config.sampling.topK)")
    check("card leaves reasoning effort at model default",
          m.config.sampling.reasoningEffort == nil && m.config.run.defaultReasoningEffort == nil)
    check("untuned model: maxContext back to auto", m.config.run.maxContext == "auto", m.config.run.maxContext)
    check("untuned model: languageOnly off", m.config.run.languageOnly == false)

    // C: switching back restores the saved profile.
    m.selectModelID(A)
    check("restored temp 0.42", m.config.sampling.temperature == 0.42, "\(m.config.sampling.temperature)")
    check("restored maxContext 100K", m.config.run.maxContext == "100K")
    check("restored reasoning effort (both places)",
          m.config.sampling.reasoningEffort == "low" && m.config.run.defaultReasoningEffort == "low")

    // The served name names the model, so it swaps with the profile, and a model entered
    // without a saved name starts blank rather than keeping the name of the one left.
    m.config.run.servedModelName = "qwen27"
    m.config.run.announceServedName = true
    m.selectModelID(B)
    check("served name archived with the outgoing profile",
          m.config.modelProfiles[A]?.servedModelName == "qwen27"
            && m.config.modelProfiles[A]?.announceServedName == true)
    check("switching restores a blank served name for a model without one",
          m.config.run.servedModelName.isEmpty && m.config.run.announceServedName == false)
    m.selectModelID(A)
    check("served name restored with the profile",
          m.config.run.servedModelName == "qwen27" && m.config.run.announceServedName == true)

    // D: a second Qwen row is also card-driven, and card data is per row.
    m.selectModelID(C)
    check("Qwen Q4_K_M card temp 1.0", m.config.sampling.temperature == 1.0, "\(m.config.sampling.temperature)")
    check("Qwen Q4_K_M card topP 0.95", m.config.sampling.topP == 0.95)
    let bonsai = CatalogModel.catalog.first { $0.id == B }!
    check("Bonsai hides the unsupported 'low' effort",
          bonsai.supportedEfforts?.contains("low") == false
            && bonsai.supportedEfforts?.contains("xhigh") == true,
          "\(bonsai.supportedEfforts ?? [])")
    m.config.run.modelID = B
    check("model exposes the filtered effort list", m.supportedEfforts == bonsai.supportedEfforts)
    m.config.run.modelID = "someone/Custom-Model:x"
    check("hand-typed ID offers every effort", m.supportedEfforts == CatalogModel.allEfforts)

    // E: reset forgets this model's profile and returns to its card.
    m.config.run.modelID = A
    m.resetModelProfile()
    check("reset cleared the stored profile", m.config.modelProfiles[A] == nil)
    check("reset re-applies the card (1.0 / 0.95 / 20)",
          m.config.sampling.temperature == 1.0 && m.config.sampling.topP == 0.95 && m.config.sampling.topK == 20,
          "\(m.config.sampling.temperature)/\(m.config.sampling.topP)/\(m.config.sampling.topK)")
    check("reset returns serve fields to auto",
          m.config.run.maxContext == "auto" && m.config.run.kvFormat == "auto" && m.config.run.languageOnly == false)

    // G: thinking off offers the card's non-thinking set, and only on click.
    m.config.sampling.reasoningEffort = "none"
    m.config.run.defaultReasoningEffort = "none"
    m.applyCardOffSampling()
    check("card's non-thinking set applied on request (0.7 / 0.80 / 20)",
          m.config.sampling.temperature == 0.7 && m.config.sampling.topP == 0.8 && m.config.sampling.topK == 20,
          "\(m.config.sampling.temperature)/\(m.config.sampling.topP)/\(m.config.sampling.topK)")
    m.config.sampling.temperature = 0.55          // hand-tuned value must survive
    m.config.run.modelID = "someone/Custom-Model:x"
    m.applyCardOffSampling()
    check("hand-typed ID has no card, so nothing is rewritten",
          m.config.sampling.temperature == 0.55, "\(m.config.sampling.temperature)")

    // H: what landed on disk reloads the same.
    let reread = ConfigStore.shared.load()
    check("B's card profile persisted", reread.modelProfiles[B]?.sampling.temperature == 1.0,
          "\(reread.modelProfiles[B]?.sampling.temperature ?? -1)")
    check("typed temp 0.55 survives a save", reread.sampling.temperature == 0.55,
          "\(reread.sampling.temperature)")
    check("schemaVersion stays 6", reread.run.schemaVersion == 6)
    // startOnLaunch is absent from this v2 fixture — the tolerant decoder must default it
    // false, not throw the whole config back to factory (the Codable missing-key trap).
    check("startOnLaunch defaults false when the key is absent", reread.startOnLaunch == false)

    // F: the SSD tier and the idle release reach the engine's command line, and the tier's
    // persistence follows the quota (the engine refuses it without one).
    var run = RunConfig()
    run.maxCacheDisk = "10G"
    run.persistentCache = true
    run.idleRelease = "60m"
    let args = SplashServer.buildArguments(for: run)
    check("serve carries the SSD quota", args.contains("--max-cache-disk") && args.contains("10G"))
    check("serve carries the persistent cache", args.contains("--persistent-cache"))
    check("serve carries the idle release", args.contains("--idle-release") && args.contains("60m"))
    var off = RunConfig()
    off.maxCacheDisk = "0"
    off.persistentCache = true
    check("no persistent cache without a quota",
          !SplashServer.buildArguments(for: off).contains("--persistent-cache"))
    off.persistentCache = false
    off.idleRelease = "auto"
    let offArgs = SplashServer.buildArguments(for: off)
    check("persistent cache can be turned off", !offArgs.contains("--persistent-cache"))
    check("auto leaves the engine's own idle release", !offArgs.contains("--idle-release"))
    off.maxCacheDisk = "auto"
    check("auto leaves the SSD quota to the engine", !SplashServer.buildArguments(for: off).contains("--max-cache-disk"))

    // F2: LAN serving. --host goes to the engine only when the toggle is on, and a config
    // written before the toggle existed must decode it defaulted, not throw (the Codable trap).
    check("loopback default: no --host on the command line",
          !SplashServer.buildArguments(for: RunConfig()).contains("--host"))
    var lan = RunConfig()
    lan.lanAccess = true
    let lanArgs = SplashServer.buildArguments(for: lan)
    check("LAN on: --host 0.0.0.0 reaches the engine",
          lanArgs.firstIndex(of: "--host").map { $0 + 1 < lanArgs.count && lanArgs[$0 + 1] == "0.0.0.0" } == true,
          lanArgs.joined(separator: " "))
    let noLan = #"{"run":{"port":8123,"modelID":"someone/Model:x"}}"#
    if let nd = noLan.data(using: .utf8),
       let ncfg = try? JSONDecoder().decode(AppConfig.self, from: nd) {
        check("lanAccess absent decodes false without losing the config",
              ncfg.run.lanAccess == false && ncfg.run.port == 8123
                && ncfg.run.modelID == "someone/Model:x")
    } else {
        check("lanAccess absent decodes false without losing the config", false, "decoder threw")
    }
    var lcfg = AppConfig()
    lcfg.run.lanAccess = true
    if let ld = try? JSONEncoder().encode(lcfg),
       let lback = try? JSONDecoder().decode(AppConfig.self, from: ld) {
        check("lanAccess round-trips through a save", lback.run.lanAccess == true)
    } else {
        check("lanAccess round-trips through a save", false, "encode/decode threw")
    }

    // F3: the serve options from the 1.5.0 option audit (offline, thinking budget, verify-rows,
    // served name, arch memory knobs). Defaults stay off the command line, a pre-audit config
    // decodes them defaulted (the Codable trap), and the arch knobs reach only models that
    // have the memory they tune.
    func flagValue(_ args: [String], _ flag: String) -> String? {
        guard let i = args.firstIndex(of: flag), i + 1 < args.count else { return nil }
        return args[i + 1]
    }
    let plainArgs = SplashServer.buildArguments(for: RunConfig())
    check("new serve options stay off the command line by default",
          !plainArgs.contains("--offline") && !plainArgs.contains("--thinking-token-budget")
            && !plainArgs.contains("--verify-rows") && !plainArgs.contains("--served-model-name")
            && !plainArgs.contains("--announce-served-name") && !plainArgs.contains("--ple-memory")
            && !plainArgs.contains("--expert-memory") && !plainArgs.contains("--sparse-kv-memory"),
          plainArgs.joined(separator: " "))
    var so = RunConfig()
    so.offline = true
    so.thinkingTokenBudget = "16384"
    so.verifyRowsAll = true
    so.servedModelName = "qwen27"
    so.announceServedName = true
    so.pleMemory = "2G"
    so.expertMemory = "16G"
    so.sparseKvMemory = "1200M"
    let soArgs = SplashServer.buildArguments(for: so)
    check("offline reaches the engine", soArgs.contains("--offline"))
    check("thinking budget reaches the engine",
          flagValue(soArgs, "--thinking-token-budget") == "16384", soArgs.joined(separator: " "))
    check("verify-rows all reaches the engine", flagValue(soArgs, "--verify-rows") == "all")
    check("served name reaches the engine", flagValue(soArgs, "--served-model-name") == "qwen27")
    check("announce reaches the engine with a name", soArgs.contains("--announce-served-name"))
    check("PLE memory reaches the engine", flagValue(soArgs, "--ple-memory") == "2G")
    check("expert memory reaches the engine", flagValue(soArgs, "--expert-memory") == "16G")
    check("sparse-KV memory reaches the engine", flagValue(soArgs, "--sparse-kv-memory") == "1200M")
    var nameless = RunConfig()
    nameless.announceServedName = true
    check("announce without a served name passes nothing",
          !SplashServer.buildArguments(for: nameless).contains("--announce-served-name"))
    let noOptions = #"{"run":{"port":8123,"modelID":"someone/Model:x"}}"#
    if let od = noOptions.data(using: .utf8),
       let ocfg = try? JSONDecoder().decode(AppConfig.self, from: od) {
        check("serve options absent decode defaulted without losing the config",
              ocfg.run.offline == false && ocfg.run.thinkingTokenBudget.isEmpty
                && ocfg.run.verifyRowsAll == false && ocfg.run.servedModelName.isEmpty
                && ocfg.run.announceServedName == false && ocfg.run.pleMemory.isEmpty
                && ocfg.run.expertMemory.isEmpty && ocfg.run.sparseKvMemory.isEmpty
                && ocfg.run.port == 8123 && ocfg.run.modelID == "someone/Model:x")
    } else {
        check("serve options absent decode defaulted without losing the config", false, "decoder threw")
    }
    var ocfg2 = AppConfig()
    ocfg2.run.offline = true
    ocfg2.run.thinkingTokenBudget = "16384"
    ocfg2.run.servedModelName = "qwen27"
    ocfg2.run.announceServedName = true
    ocfg2.run.pleMemory = "2G"
    if let od2 = try? JSONEncoder().encode(ocfg2),
       let oback = try? JSONDecoder().decode(AppConfig.self, from: od2) {
        check("serve options round-trip through a save",
              oback.run.offline == true && oback.run.thinkingTokenBudget == "16384"
                && oback.run.servedModelName == "qwen27" && oback.run.announceServedName == true
                && oback.run.pleMemory == "2G")
    } else {
        check("serve options round-trip through a save", false, "encode/decode threw")
    }
    // The arch gate on the launch copy: knobs typed for a model that lacks the memory never
    // reach the engine; the models that have them keep them.
    let flashNextID = "ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF:Q2_0"
    let moeID = "unsloth/Qwen3.6-35B-A3B-GGUF:UD-Q4_K_M"
    func knobsKept(_ id: String) -> (ple: Bool, expert: Bool, sparseKv: Bool) {
        var r = RunConfig()
        r.pleMemory = "2G"
        r.expertMemory = "16G"
        r.sparseKvMemory = "1200M"
        r.clearingUnsupportedMemoryKnobs(forModelID: id)
        return (!r.pleMemory.isEmpty, !r.expertMemory.isEmpty, !r.sparseKvMemory.isEmpty)
    }
    let denseKept = knobsKept(A)
    check("knobs dropped for a dense 27B row",
          !denseKept.ple && !denseKept.expert && !denseKept.sparseKv)
    let fnKept = knobsKept(flashNextID)
    check("Flash-Next keeps PLE and sparse-KV, drops experts",
          fnKept.ple && fnKept.sparseKv && !fnKept.expert)
    let moeKept = knobsKept(moeID)
    check("MoE keeps experts, drops PLE and sparse-KV",
          moeKept.expert && !moeKept.ple && !moeKept.sparseKv)
    let customFN = knobsKept("someone/MyQwen3.8-Flash-Next-Quant:Q4")
    check("hand-typed Flash-Next ID keeps its knobs by name",
          customFN.ple && customFN.sparseKv && !customFN.expert)
    let customNone = knobsKept("someone/Custom-Model:x")
    check("unknown custom ID gets no knobs",
          !customNone.ple && !customNone.expert && !customNone.sparseKv)

    // I: the Auto switch resolves the four covered fields at launch.
    let prof = AutoProfile(maxMemoryBytes: 40_000_000_000, contextTokens: 131_072, kvFormat: "int8")
    var arun = RunConfig()   // shipped default: Auto on
    let effAuto = arun.effective(using: prof)
    check("Auto on: profile sizes context and memory",
          effAuto.maxContext == "131072" && effAuto.maxMemory == "40000000000",
          "\(effAuto.maxContext) / \(effAuto.maxMemory)")
    check("Auto on: KV follows the profile", effAuto.kvFormat == "int8")
    check("Auto on: the SSD cache goes to the engine",
          effAuto.maxCacheDisk == "auto"
            && !SplashServer.buildArguments(for: effAuto).contains("--max-cache-disk"))
    let effCustom = arun.effective(using: nil)
    check("Auto on with no profile: Splash sizes context and memory, KV falls back to bf16",
          !SplashServer.buildArguments(for: effCustom).contains("--max-context")
            && !SplashServer.buildArguments(for: effCustom).contains("--max-memory")
            && effCustom.kvFormat == "bf16")
    arun.auto = false
    arun.maxContext = "64K"; arun.maxMemory = "24G"; arun.kvFormat = "bf16"; arun.maxCacheDisk = "10G"
    let effTyped = arun.effective(using: prof)
    check("Auto off: the typed values launch untouched",
          effTyped.maxContext == "64K" && effTyped.maxMemory == "24G"
            && effTyped.kvFormat == "bf16" && effTyped.maxCacheDisk == "10G")

    // J: the Codable trap on the new fields — a profile saved before `auto` existed must
    // decode with it defaulted, and grantedAuto must round-trip.
    let legacy = #"{"sampling":{"temperature":0.5,"topP":0.9,"topK":10},"maxContext":"50K","maxMemory":"auto","kvFormat":"bf16","languageOnly":false}"#
    if let pdata = legacy.data(using: .utf8),
       let prof2 = try? JSONDecoder().decode(ModelProfile.self, from: pdata) {
        check("legacy profile without auto decodes defaulted",
              prof2.auto == true && prof2.maxContext == "50K")
    } else {
        check("legacy profile without auto decodes defaulted", false, "decoder threw")
    }
    var gcfg = AppConfig()
    gcfg.grantedAuto["someone/Custom-Model:x"] = GrantedSettings(contextTokens: 65536, memoryBytes: 25_769_803_776)
    if let gd = try? JSONEncoder().encode(gcfg),
       let back = try? JSONDecoder().decode(AppConfig.self, from: gd) {
        check("grantedAuto round-trips",
              back.grantedAuto["someone/Custom-Model:x"]?.contextTokens == 65536
                && back.grantedAuto["someone/Custom-Model:x"]?.memoryBytes == 25_769_803_776)
    } else {
        check("grantedAuto round-trips", false, "encode/decode threw")
    }

    // K: a schema-5 config whose four covered fields are all "auto" migrates to Auto ON,
    // leaving the KV field alone for the launch-time resolution.
    let cfgURL = AppPaths.dataDir.appendingPathComponent("config.json")
    if let raw = try? Data(contentsOf: cfgURL),
       var dict = try? JSONSerialization.jsonObject(with: raw) as? [String: Any],
       var runDict = dict["run"] as? [String: Any] {
        runDict["schemaVersion"] = 5
        runDict["maxContext"] = "auto"
        runDict["maxMemory"] = "auto"
        runDict["kvFormat"] = "auto"
        runDict["maxCacheDisk"] = "auto"
        dict["run"] = runDict
        if let out = try? JSONSerialization.data(withJSONObject: dict) {
            try? out.write(to: cfgURL)
            let migrated = ConfigStore.shared.load()
            check("all-auto schema 5 migrates to Auto on", migrated.run.auto == true)
            check("all-auto migration leaves KV at auto",
                  migrated.run.kvFormat == "auto", migrated.run.kvFormat)
            check("all-auto migration lands on schema 6", migrated.run.schemaVersion == 6)
        } else {
            check("all-auto schema 5 migrates to Auto on", false, "fixture write failed")
        }
    } else {
        check("all-auto schema 5 migrates to Auto on", false, "fixture unreadable")
    }

    // L: the Provider tab's new sampling fields and client configs.
    let legacySampling = #"{"temperature":0.5,"topP":0.9,"topK":10,"maxTokens":null,"seed":null,"reasoningEffort":"none"}"#
    if let sdata = legacySampling.data(using: .utf8),
       let sam = try? JSONDecoder().decode(SamplingDefaults.self, from: sdata) {
        check("legacy sampling without presencePenalty/minP decodes defaulted",
              sam.presencePenalty == 0 && sam.minP == 0 && sam.reasoningEffort == "none")
    } else {
        check("legacy sampling without presencePenalty/minP decodes defaulted", false, "threw")
    }
    let bonsaiRow = CatalogModel.catalog.first { $0.id == B }!
    check("Bonsai card: min_p 0.05, presence 0 in thinking mode",
          bonsaiRow.cardProfile().sampling.minP == 0.05
            && bonsaiRow.cardProfile().sampling.presencePenalty == 0)
    m.config.run.modelID = A
    m.applyCardOffSampling()
    check("card-off applies presence 1.5 and min_p 0",
          m.config.sampling.presencePenalty == 1.5 && m.config.sampling.minP == 0
            && m.config.sampling.temperature == 0.7)
    check("parseContext reads the field spellings",
          AppModel.parseContext("100K") == 102_400 && AppModel.parseContext("131072") == 131_072
            && AppModel.parseContext("auto") == nil)

    var sam = SamplingDefaults()
    sam.seed = 12_345_678_901
    let base = ClientConfigContext(client: .opencode, modelID: "someone/Model:x", label: "Model X",
                                   port: 8095, contextTokens: 65_536,
                                   inputModalities: ["text", "image", "pdf"], sampling: sam)
    if let data = ClientConfigs.text(for: base).data(using: .utf8),
       let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
       let provider = (root["providers"] as? [String: Any])?["splashgui"] as? [String: Any],
       let models = provider["models"] as? [String: Any],
       let model = models["someone/Model:x"] as? [String: Any] {
        let body = model["body"] as? [String: Any]
        check("OpenCode: sampling in the model body",
              (body?["temperature"] as? Double) == 1.0 && (body?["top_k"] as? Int) == 20
                && (body?["presence_penalty"] as? Double) == 0 && (body?["min_p"] as? Double) == 0)
        check("OpenCode: seed passes through body", body?["seed"] != nil)
        let limit = model["limit"] as? [String: Any]
        check("OpenCode: limit math at 64K — out 16384, in 49152",
              (limit?["context"] as? Int) == 65_536 && (limit?["output"] as? Int) == 16_384
                && (limit?["input"] as? Int) == 49_152)
        check("OpenCode: no root model/small_model or agent overrides",
              root["model"] == nil && root["small_model"] == nil
                && root["agent"] == nil && root["agents"] == nil)
        let variants = model["variants"] as? [[String: Any]]
        let variantIDs = variants?.compactMap { $0["id"] as? String }
        let highBody = variants?.first { ($0["id"] as? String) == "high" }?["body"] as? [String: Any]
        check("OpenCode: one variant per effort, reasoning_effort in the body",
              variantIDs == CatalogModel.allEfforts
                && (highBody?["reasoning_effort"] as? String) == "high")
        let reasoning = (model["capabilities"] as? [String: Any])?["reasoning"] as? [String: Any]
        check("OpenCode: reasoning capability advertises the efforts",
              (reasoning?["supported"] as? Bool) == true && (reasoning?["toggle"] as? Bool) == true
                && (reasoning?["effort"] as? [String])?.contains("high") == true)
        let settings = provider["settings"] as? [String: Any]
        let compatibility = model["compatibility"] as? [String: Any]
        check("OpenCode: V2 package + baseURL, reasoning_content and max_tokens compatibility",
              (provider["package"] as? String) == "@opencode/ai/providers/openai-compatible"
                && (settings?["baseURL"] as? String) == "http://127.0.0.1:8095/v1"
                && (compatibility?["reasoningField"] as? String) == "reasoning_content"
                && (compatibility?["maxTokensField"] as? String) == "max_tokens")
    } else {
        check("OpenCode: sampling in the model body", false, "config did not parse")
    }
    let zctx = ClientConfigContext(client: .zed, modelID: base.modelID, label: base.label,
                                   port: base.port, contextTokens: base.contextTokens,
                                   inputModalities: base.inputModalities, sampling: base.sampling)
    let ztext = ClientConfigs.text(for: zctx)
    check("Zed: openai_compatible + api_url + temperature only",
          ztext.contains("openai_compatible") && ztext.contains("api_url")
            && ztext.contains("model_parameters") && !ztext.contains("top_k")
            && ztext.contains("interleaved_reasoning"))
    let cctx = ClientConfigContext(client: .claudeCode, modelID: base.modelID, label: base.label,
                                   port: base.port, contextTokens: base.contextTokens,
                                   inputModalities: base.inputModalities, sampling: base.sampling)
    let ctext = ClientConfigs.text(for: cctx)
    check("Claude Code: env block, bare base URL (no /v1), no sampling",
          ctext.contains("ANTHROPIC_BASE_URL") && ctext.contains("http://127.0.0.1:8095\"")
            && !ctext.contains("/v1") && !ctext.contains("temperature"))
    let xctx = ClientConfigContext(client: .codex, modelID: base.modelID, label: base.label,
                                   port: base.port, contextTokens: base.contextTokens,
                                   inputModalities: base.inputModalities, sampling: base.sampling)
    let xtext = ClientConfigs.text(for: xctx)
    check("Codex: TOML provider, responses wire, 90% compact limit",
          xtext.contains("wire_api = \"responses\"") && xtext.contains("env_key = \"SPLASH_API_KEY\"")
            && xtext.contains("model_auto_compact_token_limit = 58982")
            && xtext.contains("model_provider = \"splash\""))

    // The served name, when set, replaces the Hub ID in the configs the Provider tab writes.
    let priorID = m.config.run.modelID
    m.config.run.modelID = A
    m.config.run.servedModelName = "qwen27"
    let stext = m.clientConfigText(for: .claudeCode)
    check("served name: client configs name the model by it",
          stext.contains("\"qwen27\"") && !stext.contains(A))
    m.config.run.servedModelName = ""
    let ftext = m.clientConfigText(for: .claudeCode)
    check("no served name: the full Hub ID is written as before",
          ftext.contains(A) && !ftext.contains("qwen27"))
    m.config.run.modelID = priorID

    // M: engine source — the mode field's tolerant decode and the latest-release resolver.
    // Synthetic JSON, not the live fixture: the app now WRITES engineMode, so the on-disk
    // config always has the key and cannot prove the absent-key default.
    let noMode = #"{"engineURLOverride":"keep-me","sampling":{"temperature":0.3}}"#
    if let nm = noMode.data(using: .utf8),
       let ncfg = try? JSONDecoder().decode(AppConfig.self, from: nm) {
        check("engineMode absent decodes pinned without losing the config",
              ncfg.engineMode == .pinned && ncfg.sampling.temperature == 0.3
                && ncfg.engineURLOverride == "keep-me")
    } else {
        check("engineMode absent decodes pinned without losing the config", false, "decoder threw")
    }
    let oddMode = #"{"engineMode":"sideways","sampling":{"temperature":0.3}}"#
    if let od = oddMode.data(using: .utf8),
       let om = try? JSONDecoder().decode(AppConfig.self, from: od) {
        check("unknown engineMode falls back to pinned without losing the config",
              om.engineMode == .pinned && om.sampling.temperature == 0.3)
    } else {
        check("unknown engineMode falls back to pinned without losing the config", false, "decoder threw")
    }
    var mcfg = AppConfig()
    mcfg.engineMode = .latest
    if let md = try? JSONEncoder().encode(mcfg),
       let mback = try? JSONDecoder().decode(AppConfig.self, from: md) {
        check("engineMode round-trips", mback.engineMode == .latest)
    } else {
        check("engineMode round-trips", false, "encode/decode threw")
    }
    // The resolver's pure half, fed the real 1.5.0 release fixture. A latest that equals the
    // compiled-in pin must produce the identical URL so EngineInstaller reuses its directory
    // instead of keying a hash-named twin off the URL.
    let v150 = "1ddc28bf7bfda8833859a913d7072e2aac9072addb9eb5664f3bf1a83a8d6ee2"
    let assets150 = ["splash-1.5.0-arm64-macos26.tar.gz", "splash-1.5.0-arm64-macos26.tar.gz.sha256",
                     "Splash-1.5.0.dmg", "SHA256SUMS", "appcast.xml"]
    if let lp = EngineResolver.pin(tagName: "1.5.0", assetNames: assets150, sha256Text: v150 + "\n") {
        check("latest 1.5.0 resolves to the compiled-in pin exactly",
              lp == EngineInstaller.defaultPin, "\(lp.version) \(lp.url)")
        check("latest-equals-pinned reuses the pinned install dir",
              EngineInstaller.dirName(for: lp) == EngineInstaller.dirName(for: EngineInstaller.defaultPin))
    } else {
        check("latest 1.5.0 resolves to the compiled-in pin exactly", false, "pin() returned nil")
        check("latest-equals-pinned reuses the pinned install dir", false)
    }
    if let vp = EngineResolver.pin(tagName: "v1.6.1", assetNames: ["splash-1.6.1-arm64-macos26.tar.gz",
                                                                   "splash-1.6.1-arm64-macos26.tar.gz.sha256"],
                                   sha256Text: String(repeating: "A", count: 64)) {
        check("v-prefixed tag: version stripped, download URL keeps the tag",
              vp.version == "1.6.1" && vp.url == "https://github.com/incoai/splash/releases/download/v1.6.1/splash-1.6.1-arm64-macos26.tar.gz"
                && vp.sha256 == String(repeating: "a", count: 64), "\(vp.version) \(vp.url)")
    } else {
        check("v-prefixed tag: version stripped, download URL keeps the tag", false, "pin() returned nil")
    }
    check("release without the .sha256 asset resolves to nil",
          EngineResolver.pin(tagName: "1.6.0", assetNames: ["splash-1.6.0-arm64-macos26.tar.gz"],
                             sha256Text: v150) == nil)
    check("release with a malformed checksum resolves to nil",
          EngineResolver.pin(tagName: "1.6.0", assetNames: assets150.map { $0.replacingOccurrences(of: "1.5.0", with: "1.6.0") },
                             sha256Text: "not-hex") == nil)
    check("release with a renamed payload resolves to nil",
          EngineResolver.pin(tagName: "1.6.0", assetNames: ["splash-1.6.0-x64-macos15.tar.gz",
                                                            "splash-1.6.0-x64-macos15.tar.gz.sha256"],
                             sha256Text: v150) == nil)
    var ccfg = AppConfig()
    ccfg.engineMode = .custom
    ccfg.engineVersionOverride = "1.4.1"
    ccfg.engineURLOverride = "https://github.com/incoai/splash/releases/download/1.4.1/splash-1.4.1-arm64-macos26.tar.gz"
    ccfg.engineSHA256Override = String(repeating: "b", count: 64)
    check("custom mode with all three fields yields the override pin",
          ccfg.enginePinOverride()?.version == "1.4.1")
    ccfg.engineSHA256Override = ""
    check("custom mode with a blank SHA falls back to no override", ccfg.enginePinOverride() == nil)

    // The mode table start() installs by: the custom pin applies only in custom mode, pinned
    // mode ignores the override fields, and latest falls back to the installed engine, never
    // downward to the compiled pin.
    let stale = EnginePin(version: "1.4.1", sha256: String(repeating: "b", count: 64),
                          url: "https://github.com/incoai/splash/releases/download/1.4.1/splash-1.4.1-arm64-macos26.tar.gz")
    let pinnedPlan = AppModel.enginePlan(mode: .pinned, customOverride: stale, installed: true,
                                         checkUpdate: true, latest: nil)
    check("pinned mode ignores complete custom fields",
          pinnedPlan.override == nil && pinnedPlan.ensure == true, "\(pinnedPlan)")
    let customPlan = AppModel.enginePlan(mode: .custom, customOverride: stale, installed: true,
                                         checkUpdate: false, latest: nil)
    check("custom mode installs the custom pin regardless of the update toggle",
          customPlan.ensure == true && customPlan.override == stale)
    let partialPlan = AppModel.enginePlan(mode: .custom, customOverride: nil, installed: true,
                                          checkUpdate: false, latest: nil)
    check("custom mode with partial fields keeps pinned behavior",
          partialPlan.ensure == false && partialPlan.override == nil)
    let latestHit = AppModel.enginePlan(mode: .latest, customOverride: nil, installed: true,
                                        checkUpdate: false, latest: stale)
    check("latest mode installs the resolver answer",
          latestHit.ensure == true && latestHit.override == stale)
    let latestKeep = AppModel.enginePlan(mode: .latest, customOverride: nil, installed: true,
                                         checkUpdate: false, latest: nil)
    check("latest mode offline keeps the installed engine",
          latestKeep.ensure == false && latestKeep.override == nil)
    let latestBootstrap = AppModel.enginePlan(mode: .latest, customOverride: nil, installed: false,
                                              checkUpdate: false, latest: nil)
    check("latest mode offline with nothing installed installs the compiled pin",
          latestBootstrap.ensure == true && latestBootstrap.override == nil)

    // N: the catalog must track the installed engine's suggested list (the sync guard for the
    // Splash-defaults swap). Skips with a PASS when no engine is installed on the test machine.
    let tsv = EngineInstaller.currentLink.appendingPathComponent("install/suggested-models.tsv")
    if let tsvText = try? String(contentsOf: tsv, encoding: .utf8) {
        let ids = tsvText.split(separator: "\n").compactMap { line -> String? in
            let s = line.trimmingCharacters(in: .whitespaces)
            guard !s.isEmpty, !s.hasPrefix("#") else { return nil }
            return s.split(separator: "\t").first.map(String.init)
        }
        let cat = CatalogModel.catalog.map(\.id).filter { $0 != "custom" }
        check("catalog matches the installed engine's suggested list",
              cat == ids, "catalog \(cat.count) vs tsv \(ids.count)"
                + (cat == ids ? "" : " diff: \(Set(cat).symmetricDifference(ids).sorted())"))
        let mem = tsvText.split(separator: "\n").compactMap { line -> (String, Int)? in
            let s = line.trimmingCharacters(in: .whitespaces)
            guard !s.isEmpty, !s.hasPrefix("#") else { return nil }
            let cols = s.split(separator: "\t").map(String.init)
            guard cols.count >= 3, let gb = Int(cols[2]) else { return nil }
            return (cols[0], gb)
        }
        let tsvMemory = Dictionary(mem.map { ($0.0, $0.1) }, uniquingKeysWith: { a, _ in a })
        let memoryDrift = CatalogModel.catalog.filter { $0.id != "custom" }.compactMap { entry -> String? in
            guard let gb = entry.memoryGB else { return "\(entry.id): catalog row lacks MEMORY" }
            return tsvMemory[entry.id] == gb ? nil : "\(entry.id): tsv \(tsvMemory[entry.id] ?? -1) vs catalog \(gb)"
        }
        check("catalog MEMORY figures match the engine's suggested list",
              memoryDrift.isEmpty, memoryDrift.joined(separator: "; "))
    } else {
        check("catalog matches the installed engine's suggested list", true, "no engine installed, skipped")
    }

    // Auto sizing constants are 27B-arch-only: other archs return nil so the engine sizes itself.
    // The synthetic weight is small so nil can only come from the arch gate, not the budget.
    check("Auto sizing skips non-27B arch rows",
          MachineInfo.autoProfile(modelID: "ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF:Q2_0",
                                  languageOnly: false, weightsBytes: 1_000_000_000) == nil)
    check("Auto sizing still covers the 27B arch",
          MachineInfo.autoProfile(modelID: A, languageOnly: false, weightsBytes: 10_000_000_000) != nil)

    // The memory-knob support flags follow the archs: the Flash-Next row tunes PLE and
    // sparse-KV, the three A3B rows tune experts, the rest tune nothing.
    let fnK = CatalogModel.memoryKnobs(forID: flashNextID)
    let moeK = CatalogModel.memoryKnobs(forID: moeID)
    let mlxK = CatalogModel.memoryKnobs(forID: "mlx-community/Qwen3.6-35B-A3B-4bit")
    let q2K = CatalogModel.memoryKnobs(forID: "unsloth/Qwen3.6-35B-A3B-GGUF:UD-Q2_K_XL")
    let denseK = CatalogModel.memoryKnobs(forID: A)
    check("catalog rows carry their memory-knob support",
          fnK.ple && fnK.sparseKv && !fnK.expert
            && moeK.expert && !moeK.ple && !moeK.sparseKv
            && mlxK.expert && q2K.expert
            && !denseK.ple && !denseK.expert && !denseK.sparseKv)

    // O: local models + the saved custom list (spec scratch/spec-local-and-saved-models.md).
    // The Codable trap on the new list, the engine's argv rules for --model-dir/--mmproj/
    // --revision, the ID validator mirroring parse_model_id, the local file validators, and
    // the saved entry behaving like a catalog row for profiles.
    let noSaved = #"{"run":{"port":8123,"modelID":"someone/Model:x"},"sampling":{"temperature":0.4}}"#
    if let nsd = noSaved.data(using: .utf8),
       let nscfg = try? JSONDecoder().decode(AppConfig.self, from: nsd) {
         check("savedModels absent decodes [] without losing the config",
               nscfg.savedModels.isEmpty && nscfg.run.port == 8123 && nscfg.sampling.temperature == 0.4)
    } else {
        check("savedModels absent decodes [] without losing the config", false, "decoder threw")
    }
    let minEntry = #"{"id":"a/b","label":"B"}"#
    if let med = minEntry.data(using: .utf8),
       let ment = try? JSONDecoder().decode(SavedModel.self, from: med) {
        check("SavedModel without path/mmproj decodes defaulted",
              ment.path.isEmpty && ment.mmproj.isEmpty && ment.isLocal == false)
    } else {
        check("SavedModel without path/mmproj decodes defaulted", false, "decoder threw")
    }
    var scfg = AppConfig()
    scfg.savedModels = [SavedModel(id: "someone/Model:x", label: "X"),
                        SavedModel(id: "local/test", label: "test",
                                   path: "/m/test.gguf", mmproj: "/m/mmproj.gguf")]
    if let sd = try? JSONEncoder().encode(scfg),
       let sback = try? JSONDecoder().decode(AppConfig.self, from: sd) {
        check("the saved list round-trips through a save, order kept",
              sback.savedModels.count == 2
                && sback.savedModels[0].id == "someone/Model:x" && !sback.savedModels[0].isLocal
                && sback.savedModels[1].path == "/m/test.gguf" && sback.savedModels[1].isLocal
                && sback.savedModels[1].mmproj == "/m/mmproj.gguf")
    } else {
        check("the saved list round-trips through a save, order kept", false, "encode/decode threw")
    }
    var lrun = RunConfig()
    lrun.modelID = "local/test"
    let localLaunch = SplashServer.LocalLaunch(modelDir: "/m/test.gguf", mmproj: "")
    let largs = SplashServer.buildArguments(for: lrun, local: localLaunch)
    check("--model-dir reaches the engine for a local entry",
          flagValue(largs, "--model-dir") == "/m/test.gguf", largs.joined(separator: " "))
    check("no --model-dir for a Hub entry",
          !SplashServer.buildArguments(for: RunConfig()).contains("--model-dir"))
    let visArgs = SplashServer.buildArguments(
        for: lrun, local: SplashServer.LocalLaunch(modelDir: "/m/t.gguf", mmproj: "/m/mm.gguf"))
    check("the projector passes with vision", flagValue(visArgs, "--mmproj") == "/m/mm.gguf")
    var loRun = lrun
    loRun.languageOnly = true
    let loArgs = SplashServer.buildArguments(
        for: loRun, local: SplashServer.LocalLaunch(modelDir: "/m/t.gguf", mmproj: "/m/mm.gguf"))
    check("the projector never rides with language-only (engine parser rule)",
          !loArgs.contains("--mmproj"), loArgs.joined(separator: " "))
    let revArgs = SplashServer.buildArguments(for: lrun, revision: "deadbeef", local: localLaunch)
    check("revision never accompanies a local entry, and still rides for a Hub one",
          !revArgs.contains("--revision")
            && flagValue(SplashServer.buildArguments(for: RunConfig(), revision: "abc"), "--revision") == "abc",
          revArgs.joined(separator: " "))
    check("the ID validator mirrors parse_model_id",
          SavedModel.validID("local/Quant:Q4") && SavedModel.validID("a/b:C")
            && SavedModel.validID("local/test")
            && !SavedModel.validID("nope") && !SavedModel.validID("a//b")
            && !SavedModel.validID("a b/c") && !SavedModel.validID("local/a..b")
            && !SavedModel.validID("a/b:Q4..X") && !SavedModel.validID("a/b:")
            && !SavedModel.validID("a/b:c:d"))

    // Local file validators and sizes against temp fixtures.
    let tmp = FileManager.default.temporaryDirectory
        .appendingPathComponent("splashgui-localtest-\(ProcessInfo.processInfo.processIdentifier)")
    try? FileManager.default.removeItem(at: tmp)
    try? FileManager.default.createDirectory(at: tmp, withIntermediateDirectories: true)
    let ggufFile = tmp.appendingPathComponent("m.gguf")
    let junkFile = tmp.appendingPathComponent("junk.gguf")
    let projFile = tmp.appendingPathComponent("mmproj.gguf")
    try? Data([UInt8]("GGUF".utf8) + [UInt8](repeating: 0, count: 100)).write(to: ggufFile)
    try? Data([UInt8](repeating: 7, count: 64)).write(to: junkFile)
    try? Data([UInt8]("GGUF".utf8) + [UInt8](repeating: 0, count: 16)).write(to: projFile)
    let mlxDir = tmp.appendingPathComponent("mlx")
    let emptyDir = tmp.appendingPathComponent("empty")
    try? FileManager.default.createDirectory(at: mlxDir, withIntermediateDirectories: true)
    try? FileManager.default.createDirectory(at: emptyDir, withIntermediateDirectories: true)
    try? Data("{}".utf8).write(to: mlxDir.appendingPathComponent("config.json"))
    try? Data([UInt8](repeating: 3, count: 120)).write(to: mlxDir.appendingPathComponent("model.safetensors"))
    let linkFile = tmp.appendingPathComponent("link.gguf")
    try? FileManager.default.createSymbolicLink(at: linkFile, withDestinationURL: ggufFile)
    check("the GGUF magic and directory validators read the files like the engine",
          MachineInfo.localLooksLikeModel(path: ggufFile.path)
            && !MachineInfo.localLooksLikeModel(path: junkFile.path)
            && MachineInfo.localLooksLikeModel(path: mlxDir.path)
            && !MachineInfo.localLooksLikeModel(path: emptyDir.path)
            && !MachineInfo.localLooksLikeModel(path: tmp.appendingPathComponent("gone.gguf").path))
    check("local weights size sums the file or the directory's model files",
          MachineInfo.localWeightsSize(path: ggufFile.path) == 104
            && MachineInfo.localWeightsSize(path: mlxDir.path) == 120
            && MachineInfo.localWeightsSize(path: junkFile.path) == 64
            && MachineInfo.localWeightsSize(path: linkFile.path) == 104
            && MachineInfo.localWeightsSize(path: tmp.appendingPathComponent("gone.gguf").path) == nil)
    check("the local start blocker mirrors the engine's refusals",
          AppModel.localStartBlocker(path: tmp.appendingPathComponent("gone.gguf").path,
                                     mmproj: "", languageOnly: false) != nil
            && AppModel.localStartBlocker(path: ggufFile.path, mmproj: projFile.path,
                                          languageOnly: true) != nil
            && AppModel.localStartBlocker(path: ggufFile.path, mmproj: tmp.appendingPathComponent("gone.gguf").path,
                                          languageOnly: false) != nil
            && AppModel.localStartBlocker(path: ggufFile.path, mmproj: projFile.path,
                                          languageOnly: false) == nil
            && AppModel.localStartBlocker(path: ggufFile.path, mmproj: "",
                                          languageOnly: true) == nil)
    try? FileManager.default.removeItem(at: tmp)
    check("the auto local ID follows the sanitized repo shape",
          SavedModel.localID(forPath: "/x/Qwen3.8-27B-UD-IQ3_XXS.gguf") == "local/Qwen3.8-27B-UD:IQ3_XXS"
            && SavedModel.localID(forPath: "/x/model-v2.gguf") == "local/model-v2"
            && SavedModel.localID(forPath: "/x/weird name (v3)!!") == "local/weird-name-v3"
            && SavedModel.validID(SavedModel.localID(forPath: "/x/a..b__c!!.gguf"))
            && SavedModel.validID(SavedModel.localID(forPath: "/x/!!!.gguf")))

    // The saved entry flows through the picker and profiles like a catalog row.
    let S = "someone/My-Qwen3.8-27B-Quant:Q4"
    m.config.savedModels.removeAll()
    m.selectModelID(A)
    check("saveModel refuses an ID the engine would not accept",
          m.saveModel(id: "nope", label: "N") == false && m.config.savedModels.isEmpty)
    m.saveModel(id: S, label: "My Quant")
    m.selectModelID(S)
    check("the picker selection names a saved entry",
          m.modelPickerSelection.wrappedValue == S && m.savedEntry?.label == "My Quant")
    m.config.sampling.temperature = 0.11
    m.selectModelID(A)
    check("a saved entry's profile archives on switch away",
          m.config.modelProfiles[S]?.sampling.temperature == 0.11)
    m.selectModelID(S)
    check("a saved entry restores its profile like a catalog row",
          m.config.sampling.temperature == 0.11, "\(m.config.sampling.temperature)")
    m.saveModel(id: S, label: "Renamed")
    check("saving a duplicate ID updates the entry instead of appending",
          m.config.savedModels.count == 1 && m.config.savedModels.first?.label == "Renamed",
          "\(m.config.savedModels)")
    m.removeSavedModel(id: S)
    check("remove drops the entry but keeps the profile and grantedAuto",
          m.config.savedModels.isEmpty && m.config.modelProfiles[S]?.sampling.temperature == 0.11
            && m.modelPickerSelection.wrappedValue == "custom")
    m.selectModelID(A)

    exit(failures == 0 ? 0 : 1)
}

MainActor.assumeIsolated { runTests() }