#!/usr/bin/env python3
"""独立 K3 客户五项测试，只用 Python3 标准库。

运行环境：Python 3.9或更新版本，自带标准库即可，无需安装依赖或项目代码。
将本文件复制到任意目录即可运行；离线预览和自测不需要密钥或网络。
只预览（默认dry-run，零网络、零密钥读取）：
  python3 k3-customer-tests.py --output-parent ./结果
离线验证（只用程序内假响应，无费用）：
  python3 k3-customer-tests.py --self-test
查看完整说明：
  python3 k3-customer-tests.py --help
接收者实际执行前，自己在终端设置环境变量（以下是占位示例，不是真实密钥）：
  export K3_API_KEY='接收者自行填写的密钥'
然后实际运行：
  python3 k3-customer-tests.py --run --base-url https://api.example.com/v1 --model kimi-k3 --output-parent ./结果
每项只发一次：
  python3 k3-customer-tests.py --run --once --base-url https://api.example.com --model kimi-k3
URL填写：换成接收者有权调用的HTTPS服务地址，可带/v1，程序只补一次
/v1/chat/completions。不要填写完整接口路径、密钥、查询参数或网址片段。
model填写：默认kimi-k3；仅替换请求体model字段，不修改其它字段或转换协议。
认证：只在--run时读取K3_API_KEY，发送Authorization: Bearer请求头；
不接受命令行密钥、不取项目密钥、不记录认证头，回显的输入密钥会脱敏。
费用：--run默认43次（长文3次，其余各10次），含大量长文输出，可能很贵；
--once共5次，仍可能付费。无内置价格或费用上限，请先确认供应方价格及授权。
预览、自测不会付费。每发不重试，失败也记一次；不跟随重定向发送凭证。
默认每项分批执行：长文3并发，其余5并发；各项分开，不合并判定。
超时为每发总耗时上限，长文1800秒，其余600秒；不是整轮总时限。
连接、发送、等响应、逐块读取都按剩余时间设置超时；超过期限即不通过。
看门狗在期限到达时关闭已建立的连接，阻断慢滴流。系统DNS解析可能不可中断，
因此不保证进程能在该秒数内退出；DNS返回后仍按总期限判失败，不补发。

报告阅读：每次在--output-parent中新建k3-customer-*目录，避免覆盖旧结果。
results.json供程序读取；report.txt为完整中文可读报告；两者包含相同的逐次事实。
每一发完成即保存，不等待编号靠前的慢请求；尚未收齐预定次数的项目不通过。
先看每项“期望结果”和“是否通过”，再看每次“实际结果”及judgement中的原因。
每项只有全部预定次数通过才通过，混合成功失败仍逐次列出，不只给总评。
预览只列计划、说明和请求，没有执行结果，不能视为通过。
request=实际请求；status=HTTP状态；elapsed_seconds=耗时秒数；
transport_error=连接/超时/读取错误；raw_response=完整响应文本；
raw_response_base64=脱敏后的原响应字节编码，保留无法按UTF8显示的内容；
response_headers仅保存诊断白名单（server/via/cf-ray/x-cache/x-request-id/
x-oneapi-request-id），不保存认证头、Cookie等。
judgement记录finish_reason结束原因、content正文、tool_calls完整工具参数、
usage上游用量、error上游错误、passed布尔值及中文判定原因；用量为上游自报。
HTTP200且error为null、空对象、空字符串等空值不单独判失败；非空错误判失败。
每次结果、每项结果都仅为“通过”或“不通过”。进程退出码0表示预览/自测成功
或实际五项全部通过；1表示实际至少一项不通过；2表示命令参数错误。
请求出处：customer_cases_probe.py；官方链接只作出处，不影响布尔判定。
长文计数：正文中非空白 Unicode 字符数（含标点），不评价文笔或主题。

结构：CaseInfo 承载固定请求；judge 为离线纯判定；send_once 只负责传输；
main 编排批次与落盘。独立离线工具无数据库、平台 MCP 或服务依赖。
"""
from __future__ import annotations

import argparse
import ast
import base64
import concurrent.futures
import copy
import http.client
import json
import os
from pathlib import Path
import re
import socket
import tempfile
import threading
import time
from dataclasses import dataclass
from urllib.parse import urlsplit


@dataclass(frozen=True)
class CaseInfo:
    """一项固定请求、发送参数与客观判据。"""

    case_id: str
    body: dict
    attempts: int
    concurrency: int
    timeout_seconds: int
    expectation: str
    official_url: str = ""


def weather_tool(strict=True, description="Get current weather for a city."):
    parameters = {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}
    if strict:
        parameters["additionalProperties"] = False
    return {"type": "function", "function": {"name": "get_weather", "description": description,
                                              "parameters": parameters}}


def cases():
    """固定五项；只由调用方另行替换 model，不引入额外请求字段。"""
    chat_doc = "https://platform.kimi.com/docs/api/chat"
    tools_doc = "https://platform.kimi.com/docs/guide/use-dynamic-tool-loading"
    tool_expectation = ("HTTP200、无错误、finish_reason=tool_calls；至少一个工具调用，全部为 "
                        "get_weather；每个调用有非空字符串id、type=function，参数仅含 city，值为 北京 或 Beijing。")
    return [
        CaseInfo("longtimeout", {
            "model": "kimi-k3", "messages": [{"role": "user", "content": "写一篇5000字关于冬天的散文"}],
            "max_tokens": 36800, "reasoning_effort": "low"}, 3, 3, 1800,
            "HTTP200、完整读取、无错误、finish_reason=stop；正文不少于5000个非空白Unicode字符"
            "（含标点）；不评价文笔或主题。截断、超时、断开、短答均不通过。"),
        CaseInfo("jsonschema", {
            "model": "kimi-k3",
            "messages": [{"role": "user", "content": "请用 JSON 格式回答：北京是中国的首都吗？答案需要包含键 capital。"}],
            "response_format": {"type": "json_schema", "json_schema": {
                "name": "capital_answer", "strict": True,
                "schema": {"type": "object", "properties": {"capital": {"type": "boolean"}},
                           "required": ["capital"], "additionalProperties": False}}},
            "max_tokens": 256}, 10, 5, 600,
            'HTTP200、完整读取、无错误、stop；正文严格为仅含布尔键的 {"capital":true} JSON对象'
            '（允许JSON空白）；围栏、重复键、附加字段、非布尔、false均不通过。', chat_doc),
        CaseInfo("allowedtools", {
            "model": "kimi-k3", "messages": [{"role": "user", "content": "查询北京当前天气，请调用天气工具。"}],
            "reasoning_effort": "low", "max_tokens": 4096, "stream": False,
            "tools": [weather_tool(), {"type": "function", "function": {
                "name": "get_population", "description": "Get population of a city.",
                "parameters": {"type": "object", "properties": {"city": {"type": "string"}},
                               "required": ["city"], "additionalProperties": False}}}],
            "tool_choice": {"type": "allowed_tools", "allowed_tools": {
                "mode": "required", "tools": [{"type": "function", "function": {"name": "get_weather"}}]}}},
            10, 5, 600, tool_expectation + "不支持或返回400也不通过。", chat_doc),
        CaseInfo("msgtools", {
            "model": "kimi-k3",
            "messages": [{"role": "system", "tools": [weather_tool(False, "Get city weather")]},
                         {"role": "user", "content": "Call get_weather for Beijing."}],
            "tool_choice": "required"}, 10, 5, 600, tool_expectation, tools_doc),
        CaseInfo("msgtoolsconflict", {
            "model": "kimi-k3",
            "messages": [{"role": "system", "content": "You are a helpful assistant.",
                          "tools": [weather_tool(False, "Get city weather")]},
                         {"role": "user", "content": "Hello."}]}, 10, 5, 600,
            "HTTP400、完整读取、结构化error对象明确说明content与tools不能同时使用；任意无关400不通过。",
            tools_doc),
    ]


def strict_json(text):
    """解析标准JSON，拒绝重复键及非标准数值；错误显式抛给判定层。"""
    def pairs(items):
        result = {}
        for key, value in items:
            if key in result:
                raise ValueError("重复JSON键")
            result[key] = value
        return result

    def invalid_constant(value):
        raise ValueError("非标准JSON常量：" + value)

    return json.loads(text, object_pairs_hook=pairs, parse_constant=invalid_constant)


def case_manual(case, body=None):
    """五项各自的中文手册，帮助、默认预览和报告复用同一份说明。"""
    tool_example = {"choices": [{"message": {"role": "assistant", "content": None, "tool_calls": [
        {"id": "call_weather_1", "type": "function", "function": {
            "name": "get_weather", "arguments": '{"city":"北京"}'}}]}, "finish_reason": "tool_calls"}]}
    descriptions = {
        "longtimeout": {
            "目的": "检查非流式长文请求能否等待到完整响应，并达到客观长度要求。",
            "关键字段": "messages要求写5000字冬天散文；max_tokens=36800是输出令牌预算，并非保证字数；"
            "reasoning_effort=low为原客户参数；未开启stream，等待完整响应。",
            "合格结构示例": {"choices": [{"message": {"role": "assistant", "content":
                "此处省略至少5000个非空白字符的完整正文；仅结构示意，此占位字符串本身不合格"},
                "finish_reason": "stop"}]},
            "失败示例及判据": "HTTP200但仅返回短答、正文4999个非空白字符、finish_reason=length、"
            "超时、断开、响应长度不完整、非空error，均不通过。字符使用Python逐Unicode字符计数，"
            "isspace为真的空格/制表符/换行等不计，其余含标点均计；不是词数、字节数或令牌数。"
            "不自动评价文笔和是否围绕冬天。",
        },
        "jsonschema": {
            "目的": "检查严格JSON结构化输出以及capital的布尔真值。",
            "关键字段": "messages提问北京是否首都；response_format.type=json_schema选择结构；"
            "strict=true要求严格遵守；schema仅允许必填capital:boolean，additionalProperties=false"
            "禁止其它键；max_tokens=256为原输出预算。",
            "合格结构示例": {"choices": [{"message": {"role": "assistant", "content": '{"capital":true}'},
                                               "finish_reason": "stop"}]},
            "失败示例及判据": '正文{"capital":false}、{"capital":1}、{"capital":"true"}、'
            '{"capital":true,"extra":1}、重复键、代码围栏或附加解释均不通过。'
            '允许JSON自身空白，必须正文只有一个合法JSON对象；空正文、非stop、非空error同样不通过。',
        },
        "allowedtools": {
            "目的": "检查请求限定可调用工具后，返回是否只调用指定天气工具。",
            "关键字段": "tools同时定义get_weather和get_population，两者都仅接受必填city:string；"
            "additionalProperties=false禁止额外参数；tool_choice.type=allowed_tools且mode=required"
            "要求调用白名单中的get_weather；reasoning_effort=low、max_tokens=4096、stream=false"
            "均原样保留。",
            "合格结构示例": tool_example,
            "失败示例及判据": "未支持allowed_tools而返回400、普通文字回答、调用get_population或其它工具、"
            "工具调用没有非空字符串id、type不是function、city不是北京/Beijing、参数损坏或额外参数、"
            "finish_reason不是tool_calls均不通过。至少一个调用，每个都必须合格。",
        },
        "msgtools": {
            "目的": "检查system消息内动态声明的工具能否被有效调用。",
            "关键字段": "第一条system消息只带role和tools，没有content；其中get_weather要求city:string；"
            "原请求此处没有additionalProperties字段，不能擅自加；user要求Call get_weather for Beijing；"
            "tool_choice=required要求调用工具；无顶层tools，也不补充max_tokens等字段。",
            "合格结构示例": tool_example,
            "失败示例及判据": "400、仅普通正文、空工具列表、非get_weather调用、缺少非空字符串id、"
            "type不是function、工具参数不是仅含city=北京/Beijing的JSON对象、结束原因不是tool_calls"
            "均不通过；至少一个调用且全部有效。",
        },
        "msgtoolsconflict": {
            "目的": "检查同一system消息同时提供content与tools时是否明确拒绝这项冲突。",
            "关键字段": "system同时带content说明和tools天气声明，刻意制造冲突；user仅说Hello；"
            "原请求没有tool_choice等其它控制字段，不能擅自加入。",
            "合格结构示例": {"error": {"message": "tools cannot be used with content", "type": "invalid_request_error"}},
            "失败示例及判据": "合格示例HTTP状态必须为400。HTTP200、401、500、空/非结构化错误、"
            "400但只说invalid api key或unsupported model、仅分别提到content/tools却没有冲突关系"
            "均不通过；error必须是对象且message明确关联两个字段不能同时使用。",
        },
    }
    description = descriptions[case.case_id]
    lines = ["测试：" + case.case_id, "目的：" + description["目的"],
             "原始次数/并发/每发总耗时上限秒："
             f"{case.attempts}/{case.concurrency}/{case.timeout_seconds}；--once仅将次数及有效并发改为1。",
             "原始请求（仅model可由命令行替换）：", json.dumps(body or case.body, ensure_ascii=False, indent=2),
             "关键字段含义：" + description["关键字段"], "期望结果：" + case.expectation,
             "合格返回结构示例（除冲突项为HTTP400外均HTTP200；可省略与判定无关的上游字段）：",
             json.dumps(description["合格结构示例"], ensure_ascii=False, indent=2),
             "失败示例及判据：" + description["失败示例及判据"]]
    if case.official_url:
        lines.append("出处链接（仅作出处）：" + case.official_url)
    return "\n".join(lines)


def judge(case_id, response, key=""):
    """原始响应记录 -> 布尔通过、原因及完整结构化事实；不访问网络或状态。"""
    response = redact(response, key)
    facts = {"finish_reason": None, "tool_calls": [], "usage": {}, "content": "", "error": None}

    def result(passed, reason):
        return redact({"passed": bool(passed), "是否通过": "通过" if passed else "不通过",
                       "原因": reason, **facts}, key)

    if response.get("transport_error"):
        return result(False, "传输失败或响应未完整读取")
    try:
        envelope = strict_json(response.get("raw_response", ""))
    except (ValueError, TypeError):
        return result(False, "响应不是完整有效JSON")
    if not isinstance(envelope, dict):
        return result(False, "响应信封必须为对象")
    facts["usage"] = envelope.get("usage", {})
    facts["error"] = envelope.get("error")
    if case_id == "msgtoolsconflict":
        error = envelope.get("error")
        description = error.get("message", "") if isinstance(error, dict) else ""
        description = description.lower() if isinstance(description, str) else ""
        # 字段名之间只能是冲突关系的固定语法，不能跨分号或无关对象拼接关系。
        first = r"(?<![a-z0-9_-])[\"'`]?tools(?![a-z0-9_-])[\"'`]?"
        second = r"(?<![a-z0-9_-])[\"'`]?content(?![a-z0-9_-])[\"'`]?"
        pair = rf"(?:{first}\s*(?:and|与|和|、|/)\s*{second}|{second}\s*(?:and|与|和|、|/)\s*{first})"
        relationship = re.search(
            rf"\b{first}\s+cannot be used with\s+{second}\b"
            rf"|\b{second}\s+cannot be used with\s+{first}\b"
            rf"|{pair}\s+(?:are\s+)?mutually exclusive\b"
            rf"|cannot\s+(?:use|specify|include)\s+both\s+{pair}\b"
            rf"|{pair}\s*(?:不能同时使用|不能共存|冲突|互斥)"
            rf"|不能同时(?:使用|指定)\s*{pair}", description)
        passed = (response.get("status") == 400 and isinstance(error, dict)
                  and isinstance(error.get("message"), str) and bool(error["message"].strip())
                  and "content" in description and "tools" in description and bool(relationship))
        return result(passed, "400且结构化错误明确content/tools冲突" if passed else "未返回明确的content/tools冲突错误")
    if response.get("status") != 200 or envelope.get("error"):
        return result(False, "状态不是200或信封含非空error")
    choices = envelope.get("choices")
    if not isinstance(choices, list) or len(choices) != 1 or not isinstance(choices[0], dict):
        return result(False, "必须有且仅有一个有效choice")
    choice = choices[0]
    message = choice.get("message")
    if not isinstance(message, dict):
        return result(False, "缺少有效message")
    facts.update(finish_reason=choice.get("finish_reason"), content=message.get("content"),
                 tool_calls=message.get("tool_calls", []))
    if message.get("refusal") or message.get("error"):
        return result(False, "消息含拒答或错误")
    if case_id in ("longtimeout", "jsonschema"):
        content = message.get("content")
        if choice.get("finish_reason") != "stop" or not isinstance(content, str) or not content.strip():
            return result(False, "正文为空或结束原因不是stop")
        if message.get("tool_calls"):
            return result(False, "正文用例返回了工具调用")
        if case_id == "longtimeout":
            count = sum(not character.isspace() for character in content)
            facts["non_whitespace_characters"] = count
            return result(count >= 5000, f"非空白字符数（含标点）：{count}，要求至少5000")
        try:
            answer = strict_json(content)
        except (ValueError, TypeError):
            return result(False, "正文不是严格JSON")
        passed = isinstance(answer, dict) and set(answer) == {"capital"} and answer["capital"] is True
        return result(passed, "正文仅capital=true" if passed else "正文不满足唯一布尔键capital=true")
    if case_id not in ("allowedtools", "msgtools"):
        return result(False, "未知用例")
    calls = message.get("tool_calls")
    if choice.get("finish_reason") != "tool_calls" or not isinstance(calls, list) or not calls:
        return result(False, "缺少工具调用或结束原因不正确")
    for call in calls:
        if not isinstance(call, dict) or call.get("type") != "function":
            return result(False, "无效工具调用类型")
        if not isinstance(call.get("id"), str) or not call["id"].strip():
            return result(False, "工具调用缺少非空字符串id")
        function = call.get("function")
        if not isinstance(function, dict) or function.get("name") != "get_weather":
            return result(False, "调用了非get_weather工具")
        try:
            arguments = strict_json(function.get("arguments"))
        except (ValueError, TypeError):
            return result(False, "工具参数不是严格JSON字符串")
        if (not isinstance(arguments, dict) or set(arguments) != {"city"}
                or arguments["city"] not in ("北京", "Beijing")):
            return result(False, "工具参数必须仅含city=北京或Beijing")
    return result(True, "全部为有效get_weather调用")


DIAGNOSTIC_HEADERS = ("server", "via", "cf-ray", "x-cache", "x-request-id", "x-oneapi-request-id")


def redact(value, key):
    """递归移除密钥及逐字符混合JSON转义，不截断其它响应内容。"""
    if isinstance(value, str):
        if not key:
            return value
        parts = []
        for character in key:
            codepoint = ord(character)
            if codepoint <= 0xFFFF:
                unicode_form = r"\\+u" + f"{codepoint:04x}"
            else:
                codepoint -= 0x10000
                unicode_form = (r"\\+u" + f"{0xD800 + (codepoint >> 10):04x}"
                                + r"\\+u" + f"{0xDC00 + (codepoint & 1023):04x}")
            alternatives = [re.escape(character), "(?i:" + unicode_form + ")"]
            if character in '\\"/':
                alternatives.append(r"\\+" + re.escape(character))
            parts.append("(?:" + "|".join(alternatives) + ")")
        return re.sub("".join(parts), "[REDACTED]", value)
    if isinstance(value, dict):
        result = {}
        for name, item in value.items():
            if name == "raw_response_base64" and isinstance(item, str):
                try:
                    item = base64.b64encode(redact_bytes(base64.b64decode(item, validate=True), key)).decode("ascii")
                except ValueError:
                    item = redact(item, key)
            else:
                item = redact(item, key)
            result[redact(name, key)] = item
        return result
    if isinstance(value, list):
        return [redact(item, key) for item in value]
    return value


def redact_bytes(raw, key):
    """按latin1一字节一字符映射脱敏，完整保留其余字节包括非UTF8内容。"""
    text = redact(raw.decode("latin1"), key)
    if key:
        text = redact(text, key.encode("utf-8").decode("latin1"))
    return text.encode("latin1")


def endpoint(base_url):
    """接收者明确指定HTTPS主机；不接受凭证、查询串或片段。"""
    parsed = urlsplit(base_url)
    if (parsed.scheme != "https" or not parsed.hostname or parsed.username or parsed.password
            or parsed.query or parsed.fragment):
        raise ValueError("base-url必须为不含凭证、查询串、片段的HTTPS地址")
    path = parsed.path.rstrip("/")
    if path.endswith("/v1"):
        path = path[:-3]
    return parsed.hostname, parsed.port or 443, path + "/v1/chat/completions"


def send_once(address, key, body, timeout_seconds, connection_factory, clock, timer_factory=threading.Timer):
    """一次总期限传输；看门狗关连接，不能保证中断系统DNS；保留部分正文。"""
    started = clock()
    deadline = started + timeout_seconds
    connection = None
    response_socket = None
    raw = bytearray()
    expired = threading.Event()
    timer = None
    record = {"request": body, "status": None, "response_headers": {}, "transport_error": ""}

    def remaining_timeout():
        remaining = deadline - clock()
        if remaining <= 0 or expired.is_set():
            raise TimeoutError("超过每发总耗时上限")
        if connection is not None:
            connection.timeout = remaining
            active_socket = getattr(connection, "sock", None) or response_socket
            if active_socket is not None:
                active_socket.settimeout(remaining)
        return remaining

    def abort_connection():
        expired.set()
        active_socket = getattr(connection, "sock", None) or response_socket
        if active_socket is not None:
            try:
                active_socket.shutdown(socket.SHUT_RDWR)
            except OSError as error:
                record["watchdog_close_error"] = f"{type(error).__name__}: {error}"
        if connection is not None:
            connection.close()

    try:
        host, port, path = address
        encoded_body = json.dumps(body, ensure_ascii=False).encode("utf-8")
        connection = connection_factory(host, port, timeout=remaining_timeout())
        timer = timer_factory(remaining_timeout(), abort_connection)
        timer.daemon = True
        timer.start()
        remaining_timeout()
        connection.connect()
        # 看门狗关掉连接后，禁止http.client在发送时自动重新建立连接。
        connection.auto_open = 0
        remaining_timeout()
        connection.request("POST", path, body=encoded_body,
                           headers={"Content-Type": "application/json", "Authorization": "Bearer " + key})
        remaining_timeout()
        response = connection.getresponse()
        response_socket = getattr(getattr(getattr(response, "fp", None), "raw", None), "_sock", None)
        record["status"] = response.status
        record["response_headers"] = {name.lower(): value for name, value in response.getheaders()
                                      if name.lower() in DIAGNOSTIC_HEADERS}
        # 分块留存，断开或超时也不丢已接收内容；无长度上限截断正文。
        while True:
            remaining_timeout()
            chunk = response.read1(65536)
            raw.extend(chunk)
            remaining_timeout()
            if not chunk:
                break
        declared_length = response.getheader("Content-Length")
        if declared_length is not None and len(raw) != int(declared_length):
            record["transport_error"] = "响应长度与Content-Length不符"
        if response.getheader("Transfer-Encoding", "").lower() == "chunked" and response.chunk_left is not None:
            record["transport_error"] = "分块响应未完整结束"
    except (OSError, ValueError, AttributeError, http.client.HTTPException) as error:
        if isinstance(error, http.client.IncompleteRead):
            raw.extend(error.partial)
        record["transport_error"] = f"{type(error).__name__}: {error}"
    finally:
        if timer is not None:
            timer.cancel()
        if connection is not None:
            connection.close()
    record["elapsed_seconds"] = round(clock() - started, 6)
    if record["elapsed_seconds"] >= timeout_seconds or expired.is_set():
        record["transport_error"] = "TimeoutError: 超过每发总耗时上限"
    safe_raw = redact_bytes(bytes(raw), key)
    record["raw_response"] = safe_raw.decode("utf-8", errors="replace")
    record["raw_response_base64"] = base64.b64encode(safe_raw).decode("ascii")
    return redact(record, key)


def execute_case(case, model, once, sender, on_update=None, key=""):
    """按原始批次并发发出，逐次留痕；任一失败使本项不通过。"""
    body = copy.deepcopy(case.body)
    body["model"] = model
    count = 1 if once else case.attempts
    attempts = []
    item = {"case_id": case.case_id, "说明": case_manual(case, body),
            "期望结果": case.expectation, "official_url": case.official_url,
            "planned": count, "concurrency": min(case.concurrency, count),
            "timeout_seconds": case.timeout_seconds, "request": body,
            "是否通过": "不通过", "实际结果": attempts}
    if on_update:
        on_update(item)
    with concurrent.futures.ThreadPoolExecutor(max_workers=case.concurrency) as pool:
        for offset in range(0, count, case.concurrency):
            futures = {pool.submit(sender, copy.deepcopy(body), case.timeout_seconds): index + 1
                       for index in range(offset, min(offset + case.concurrency, count))}
            for future in concurrent.futures.as_completed(futures):
                response = redact(future.result(), key)
                attempts.append(redact({"index": futures[future], **response,
                                        "judgement": judge(case.case_id, response, key)}, key))
                attempts.sort(key=lambda attempt: attempt["index"])
                item["是否通过"] = "通过" if len(attempts) == count and all(
                    attempt["judgement"]["passed"] for attempt in attempts) else "不通过"
                if on_update:
                    on_update(item)
    return redact(item, key)


def write_report(directory, payload, key=""):
    """仅向新目录写完整JSON及逐项逐次可读报告。"""
    payload = redact(payload, key)
    json_temporary = directory / ".results.json.tmp"
    json_temporary.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
    json_temporary.replace(directory / "results.json")
    lines = ["K3客户五项测试", "模式：" + payload["mode"], "完整使用手册：", payload.get("使用手册", __doc__)]
    for item in payload["cases"]:
        lines += ["", "用例：" + item["case_id"], "期望结果：" + item["期望结果"],
                  "次数/并发/超时秒：" + "/".join(str(item[name]) for name in
                                                  ("planned", "concurrency", "timeout_seconds"))]
        if "是否通过" in item:
            lines += ["是否通过：" + item["是否通过"]]
        else:
            lines += ["预览：尚未执行，无测试结果。"]
        lines += ["原请求：", json.dumps(item["request"], ensure_ascii=False, indent=2)]
        lines += ["本项详细说明：", item.get("说明", "")]
        for attempt in item.get("实际结果", []):
            lines += [f"第{attempt['index']}次", "实际结果：", json.dumps(attempt, ensure_ascii=False, indent=2),
                      "是否通过：" + attempt["judgement"]["是否通过"]]
    text_temporary = directory / ".report.txt.tmp"
    text_temporary.write_text("\n".join(lines) + "\n", encoding="utf-8")
    text_temporary.replace(directory / "report.txt")


def main(argv=None, environment=None, connection_factory=http.client.HTTPSConnection, clock=time.monotonic):
    """命令入口；默认只落预览文件，不读密钥、不构建网络连接。"""
    full_manual = __doc__ + "\n\n" + "\n\n".join(case_manual(case) for case in cases())
    parser = argparse.ArgumentParser(description=full_manual, formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--run", action="store_true", help="明确发出真实请求，可能产生大量费用")
    parser.add_argument("--once", action="store_true", help="每项仅一次；仍可能付费")
    parser.add_argument("--model", default="kimi-k3", help="只替换请求模型名")
    parser.add_argument("--base-url", help="实际运行必填，由接收者指定HTTPS地址")
    parser.add_argument("--output-parent", default=".", help="新结果目录的父目录")
    parser.add_argument("--self-test", action="store_true", help="只运行离线假响应测试")
    arguments = parser.parse_args(argv)
    if arguments.self_test:
        if arguments.run:
            parser.error("--self-test不能与--run一起使用")
        return self_test()
    key = ""
    address = None
    if arguments.run:
        if not arguments.base_url:
            parser.error("--run需要--base-url")
        try:
            address = endpoint(arguments.base_url)
        except ValueError as error:
            parser.error(str(error))
        key = (os.environ if environment is None else environment).get("K3_API_KEY", "")
        if not key or any(character.isspace() for character in key):
            parser.error("K3_API_KEY必须为非空且不含空白的环境变量")
    parent = Path(arguments.output_parent)
    parent.mkdir(parents=True, exist_ok=True)
    directory = Path(tempfile.mkdtemp(prefix="k3-customer-", dir=parent))
    payload = {"mode": "实际执行" if arguments.run else "默认预览（零网络）", "使用手册": __doc__, "cases": []}
    for case in cases():
        if arguments.run:
            def sender(body, timeout_seconds):
                return send_once(address, key, body, timeout_seconds, connection_factory, clock)
            def save_progress(item):
                if not any(existing is item for existing in payload["cases"]):
                    payload["cases"].append(item)
                write_report(directory, payload, key)
            execute_case(case, arguments.model, arguments.once, sender, save_progress, key)
        else:
            body = copy.deepcopy(case.body)
            body["model"] = arguments.model
            item = {"case_id": case.case_id, "request": body, "说明": case_manual(case, body),
                    "期望结果": case.expectation,
                    "official_url": case.official_url, "planned": 1 if arguments.once else case.attempts,
                    "concurrency": 1 if arguments.once else case.concurrency,
                    "timeout_seconds": case.timeout_seconds}
            payload["cases"].append(item)
            write_report(directory, payload, key)
    print("结果目录：" + str(directory.resolve()))
    return int(arguments.run and any(item["是否通过"] == "不通过" for item in payload["cases"]))


def self_test():
    """纯离线假响应与传输替身；可选源码AST核对，不导入原项目。"""
    checks = []

    def check(name, condition):
        if not condition:
            raise AssertionError(name)
        checks.append(name)

    def response(content="", finish="stop", calls=None, status=200, error=None):
        envelope = {"choices": [{"message": {"content": content}, "finish_reason": finish}],
                    "usage": {"prompt_tokens": 3, "completion_tokens": 5}}
        if calls is not None:
            envelope["choices"][0]["message"]["tool_calls"] = calls
        if error is not None:
            envelope["error"] = error
        return {"status": status, "raw_response": json.dumps(envelope, ensure_ascii=False), "transport_error": ""}

    def call(city="北京", name="get_weather", arguments=None):
        return {"id": "call_weather_1", "type": "function", "function": {"name": name,
                "arguments": json.dumps({"city": city}) if arguments is None else arguments}}

    check("固定五项", len(cases()) == 5)
    check("原始次数并发超时", [(item.attempts, item.concurrency, item.timeout_seconds) for item in cases()]
          == [(3, 3, 1800)] + [(10, 5, 600)] * 4)
    valid = {"longtimeout": response("冬" * 4999 + "。\n\t "), "jsonschema": response('{"capital":true}'),
             "allowedtools": response(finish="tool_calls", calls=[call()]),
             "msgtools": response(finish="tool_calls", calls=[call("Beijing")]),
             "msgtoolsconflict": response(status=400, error={"message": "tools cannot be used with content"})}
    for case_id, good in valid.items():
        check(case_id + "正例", judge(case_id, good)["passed"])
        if case_id != "msgtoolsconflict":
            for empty_error in (None, {}, "", [], False, 0):
                envelope = json.loads(good["raw_response"])
                envelope["error"] = empty_error
                envelope["choices"][0]["message"]["error"] = empty_error
                check(case_id + "空错误字段不误判", judge(case_id, {
                    **good, "raw_response": json.dumps(envelope)})["passed"])
        for label, bad in [("损坏JSON", {"status": 200, "raw_response": "{"}),
                           ("空正文", {"status": 200, "raw_response": ""}),
                           ("异常形状", {"status": 200, "raw_response": "[]"}),
                           ("超时", {**good, "transport_error": "TimeoutError"}),
                           ("断开", {**good, "transport_error": "IncompleteRead"}),
                           ("200错误信封", response(error={"message": "bad request"}))]:
            check(case_id + label, not judge(case_id, bad)["passed"])
    for content in ("冬" * 4999, " \n\t" * 5000, "短答", ""):
        check("长文短答及计数", not judge("longtimeout", response(content))["passed"])
    check("长文length", not judge("longtimeout", response("冬" * 5000, "length"))["passed"])
    for content in ('```json\n{"capital":true}\n```', '{"capital":false}', '{"capital":1}',
                    '{"capital":"true"}', '{"capital":true,"extra":1}',
                    '{"capital":false,"capital":true}', 'null', '[]', 'true', '', 'NaN', '{'):
        check("严格JSON反例", not judge("jsonschema", response(content))["passed"])
    for case_id in ("allowedtools", "msgtools"):
        for invalid_id in (None, "", " \t", 123):
            bad_call = {**call(), "id": invalid_id}
            check(case_id + "无效工具编号", not judge(case_id, response(finish="tool_calls", calls=[bad_call]))["passed"])
        missing_id = call()
        del missing_id["id"]
        check(case_id + "缺工具编号", not judge(case_id, response(finish="tool_calls", calls=[missing_id]))["passed"])
        for calls in ([], [call("Shanghai")], [call(name="get_population")], [call(), call(name="other")],
                      [call(arguments='{"city":"北京","extra":1}')], [call(arguments="{")],
                      [call(arguments='{"city":null}')], [None], [call(arguments="[]")]):
            check(case_id + "工具反例", not judge(case_id, response(finish="tool_calls", calls=calls))["passed"])
        check(case_id + "错误结束", not judge(case_id, response(calls=[call()]))["passed"])
        check(case_id + "不支持400", not judge(case_id, response(status=400, error={"message": "unsupported"}))["passed"])
    for error in ({"message": "invalid api key"}, {"message": "invalid content or tools"},
                  "tools cannot be used with content", {}, {"message": "invalid tools", "param": "content"}):
        check("无关400", not judge("msgtoolsconflict", response(status=400, error=error))["passed"])
    check("冲突状态必须400", not judge("msgtoolsconflict", {**valid["msgtoolsconflict"], "status": 200})["passed"])
    for message in ("content cannot be used with images; tools are unsupported",
                    "cannot use both content and images; tools are mutually exclusive",
                    "tools cannot be used with images; content is unsupported"):
        check("审查冲突关系反例", not judge("msgtoolsconflict", response(status=400, error={"message": message}))["passed"])
    for message in ("tools cannot be used with content-length header",
                    "tools cannot be used with content_type",
                    "content cannot be used with tools_extra",
                    "tools_extra cannot be used with content",
                    "content_type cannot be used with tools",
                    "extra_tools cannot be used with content",
                    "tools and content-length are mutually exclusive",
                    "cannot use both content and tools_extra"):
        check("完整字段边界反例", not judge("msgtoolsconflict", response(status=400, error={"message": message}))["passed"])
    for message in ("tools cannot be used with content", "'tools' cannot be used with 'content'",
                    "content cannot be used with tools", "content and tools are mutually exclusive",
                    "cannot use both content and tools", "content与tools不能同时使用",
                    '"tools" cannot be used with "content"',
                    "messages[0].tools cannot be used with content",
                    "'messages[0].tools' cannot be used with 'content'",
                    "messages.0.tools cannot be used with `content`"):
        check("官方冲突及特定关系正例", judge("msgtoolsconflict", response(status=400, error={"message": message}))["passed"])

    # 用原文件的AST只提取两个工具构造函数、常量和用例表达式，不导入任何项目依赖。
    source = Path(__file__).resolve().parent.parent / "service-source/xapi-tool/www/xapitool/probe/customer_cases_probe.py"
    if source.is_file():
        tree = ast.parse(source.read_text(encoding="utf-8"))
        names = {"_KIMI_CHAT_DOC", "_KIMI_DYNAMIC_TOOLS_DOC", "CASES"}
        nodes = [node for node in tree.body if
                 (isinstance(node, ast.FunctionDef) and node.name in ("_weather_tool", "_population_tool")) or
                 (isinstance(node, ast.Assign) and any(isinstance(target, ast.Name) and target.id in names
                                                      for target in node.targets)) or
                 (isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name) and node.target.id in names)]

        def source_case(case_id, body, **settings):
            from types import SimpleNamespace
            return SimpleNamespace(case_id=case_id, body=body, **settings)

        namespace = {"CustomerCase": source_case}
        exec(compile(ast.Module(body=nodes, type_ignores=[]), str(source), "exec"), namespace)
        for item in cases():
            original = namespace["CASES"][item.case_id]
            check(item.case_id + "原请求AST快照", item.body == original.body)
            check(item.case_id + "原发送设置AST快照", (item.attempts, item.concurrency, item.timeout_seconds)
                  == (original.attempts, original.concurrency, original.call_timeout_seconds))
        print("原始源码AST快照：10/10通过")
    else:
        print("原始源码不在本机：未核对源码AST；离线判据测试继续。")

    class FakeConnection:
        """离线传输替身：记录请求，不建立网络连接。"""
        def __init__(self, status=302, raw=b"{}", failure=None, declared_length=None):
            self.status, self.raw, self.failure = status, raw, failure
            self.calls = 0
            self.closed = False
            self.declared_length = declared_length

        def request(self, *args, **kwargs):
            self.calls += 1
            if self.failure:
                raise self.failure

        def connect(self):
            return None

        def getresponse(self):
            return self

        def getheaders(self):
            return [("Location", "https://other.invalid"), ("Authorization", "secret"),
                    ("Set-Cookie", "secret"), ("X-Request-ID", "diagnostic")]

        def getheader(self, name, default=None):
            if name == "Content-Length":
                return self.declared_length
            return default

        def read1(self, count):
            raw, self.raw = self.raw, b""
            return raw

        def close(self):
            self.closed = True

    for status in (301, 302, 303, 307, 308, 429, 500):
        fake = FakeConnection(status, b'{"echo":"fake-secret"}')
        record = send_once(("offline.invalid", 443, "/v1/chat/completions"), "fake-secret", {}, 600,
                           lambda *args, **kwargs: fake, lambda: 0)
        check("重定向及失败不重试", fake.calls == 1 and fake.closed and record["status"] == status)
        check("头白名单", record["response_headers"] == {"x-request-id": "diagnostic"})
        check("密钥脱敏", "fake-secret" not in json.dumps(record)
              and b"fake-secret" not in base64.b64decode(record["raw_response_base64"]))
    fake = FakeConnection(failure=TimeoutError("offline timeout"))
    record = send_once(("offline.invalid", 443, "/"), "fake-secret", {}, 600,
                       lambda *args, **kwargs: fake, lambda: 0)
    check("超时不重试", fake.calls == 1 and bool(record["transport_error"]) and fake.closed)
    fake = FakeConnection(status=200, raw=b'{"partial":true}', declared_length="100")
    record = send_once(("offline.invalid", 443, "/"), "fake-secret", {}, 600,
                       lambda *args, **kwargs: fake, lambda: 0)
    check("长度不足保留原响应并失败", record["raw_response"] == '{"partial":true}'
          and bool(record["transport_error"]) and not judge("longtimeout", record)["passed"])
    check("无关内容不冒充冲突", not judge("msgtoolsconflict", response(status=400, error={
        "message": "tools token error; content cannot be used with image"}))["passed"])
    check("转义密钥脱敏", "fake" not in redact(json.dumps({"echo": 'fake"key'}), 'fake"key'))
    secret = "fake-secret"
    for encoded_secret in (secret, r"\u0066ake-secret", r"f\u0061ke\u002dsecret",
                           "".join(r"\u%04x" % ord(character) for character in secret)):
        envelope = json.loads(response("前文" * 35000 + secret + "尾文")["raw_response"])
        envelope["usage"]["echo"] = secret
        envelope["choices"][0]["message"]["tool_calls"] = [call(arguments=json.dumps({"city": secret}))]
        raw_text = json.dumps(envelope, ensure_ascii=False).replace(secret, encoded_secret)
        fake = FakeConnection(status=200, raw=raw_text.encode())
        record = send_once(("offline.invalid", 443, "/"), secret, {}, 600,
                           lambda *args, **kwargs: fake, lambda: 0)
        projected = judge("longtimeout", record, secret)
        safe_bytes = base64.b64decode(record["raw_response_base64"])
        check("混合Unicode转义不能还原密钥", secret not in json.dumps(json.loads(record["raw_response"]), ensure_ascii=False)
              and secret not in json.dumps(json.loads(safe_bytes), ensure_ascii=False)
              and secret not in json.dumps(projected, ensure_ascii=False))
        check("完整长响应仅密钥脱敏", json.loads(record["raw_response"])["choices"][0]["message"]["content"]
              == "前文" * 35000 + "[REDACTED]尾文")
        direct = judge("longtimeout", {"status": 200, "raw_response": raw_text}, secret)
        check("判定入口及派生字段脱敏", secret not in json.dumps(direct, ensure_ascii=False)
              and "[REDACTED]" in direct["content"])
    nested = json.dumps({"nested": json.dumps({"echo": r"\u0066ake-secret"})})
    check("嵌套转义脱敏", secret not in json.loads(json.loads(redact(nested, secret))["nested"])["echo"])
    review_raw = rb'{"error":{"message":"\u0066ake-secret"}}'
    fake = FakeConnection(status=400, raw=review_raw)
    review_record = send_once(("offline.invalid", 443, "/"), secret, {}, 600,
                              lambda *args, **kwargs: fake, lambda: 0)
    review_item = execute_case(cases()[4], "kimi-k3", True, lambda body, seconds: review_record, key=secret)
    check("独立审查原始密钥反例", secret not in json.dumps(review_item)
          and review_item["实际结果"][0]["judgement"]["error"]["message"] == "[REDACTED]")

    # 假时钟明确复现601>600，各阶段返回后都检查总期限。
    for stage in ("connect", "request", "getresponse", "read1"):
        elapsed = [0]
        fake = FakeConnection(status=200, raw=valid["jsonschema"]["raw_response"].encode())
        original_method = getattr(fake, stage)

        def late_stage(*args, _original=original_method, **kwargs):
            result = _original(*args, **kwargs)
            elapsed[0] = 601
            return result

        setattr(fake, stage, late_stage)
        record = send_once(("offline.invalid", 443, "/"), secret, {}, 600,
                           lambda *args, **kwargs: fake, lambda: elapsed[0])
        check(stage + "601秒总时限", record["elapsed_seconds"] == 601 and bool(record["transport_error"])
              and not judge("jsonschema", record)["passed"] and fake.closed)
        if stage == "read1":
            check("越期已读内容仍保留", record["raw_response"] == valid["jsonschema"]["raw_response"])
    elapsed = [0]
    fake = FakeConnection(status=200, raw=valid["jsonschema"]["raw_response"].encode())

    def late_close():
        fake.closed = True
        elapsed[0] = 601

    fake.close = late_close
    record = send_once(("offline.invalid", 443, "/"), secret, {}, 600,
                       lambda *args, **kwargs: fake, lambda: elapsed[0])
    check("结束601秒仍判失败", record["elapsed_seconds"] == 601 and bool(record["transport_error"])
          and not judge("jsonschema", record)["passed"])

    class FakeSocket:
        """记录剩余超时和看门狗关闭，不使用真实套接字。"""
        def __init__(self):
            self.timeouts = []
            self.shutdown_called = False

        def settimeout(self, seconds):
            self.timeouts.append(seconds)

        def shutdown(self, how):
            self.shutdown_called = True

    class FakeTimer:
        """手动触发的离线看门狗，避免等待真实期限。"""
        def __init__(self, seconds, callback):
            self.seconds, self.callback = seconds, callback
            self.cancelled = False

        def start(self):
            return None

        def cancel(self):
            self.cancelled = True

    timers = []

    def fake_timer_factory(seconds, callback):
        timer = FakeTimer(seconds, callback)
        timers.append(timer)
        return timer

    fake = FakeConnection(status=200)
    fake.sock = FakeSocket()

    def slow_drip(count):
        timers[-1].callback()
        return b"partial"

    fake.read1 = slow_drip
    record = send_once(("offline.invalid", 443, "/"), secret, {}, 600,
                       lambda *args, **kwargs: fake, lambda: 0, fake_timer_factory)
    check("看门狗中断慢滴流", fake.sock.shutdown_called and fake.closed and bool(record["transport_error"])
          and record["raw_response"] == "partial" and timers[-1].cancelled)
    elapsed = [0]
    fake = FakeConnection(status=200, raw=valid["jsonschema"]["raw_response"].encode())
    fake.sock = FakeSocket()

    def connect_after_100_seconds():
        elapsed[0] = 100

    fake.connect = connect_after_100_seconds
    record = send_once(("offline.invalid", 443, "/"), secret, {}, 600,
                       lambda *args, **kwargs: fake, lambda: elapsed[0], fake_timer_factory)
    check("各阶段设置剩余timeout", 600 in fake.sock.timeouts and 500 in fake.sock.timeouts
          and not record["transport_error"])
    for bad_url in ("http://host", "https://key@host", "https://host/?key=secret", "https://host/#fragment"):
        try:
            endpoint(bad_url)
        except ValueError:
            check("地址拒绝凭证和不安全输入", True)
        else:
            check("地址应拒绝", False)
    check("标准地址", endpoint("https://example.invalid/v1") ==
          ("example.invalid", 443, "/v1/chat/completions"))

    def forbidden(*args, **kwargs):
        raise AssertionError("默认预览不得访问网络或读取环境")

    class NoEnvironment:
        """默认预览的环境读取哨兵。"""
        get = staticmethod(forbidden)

    with tempfile.TemporaryDirectory(prefix="k3-offline-") as temporary:
        check("默认零网络及零密钥读取", main(["--output-parent", temporary], NoEnvironment(), forbidden) == 0)
        directories = list(Path(temporary).iterdir())
        data = json.loads((directories[0] / "results.json").read_text())
        check("预览完整五项43发", len(data["cases"]) == 5 and sum(item["planned"] for item in data["cases"]) == 43)
        check("预览无伪造判定", all("是否通过" not in item for item in data["cases"]))
        check("预览包含完整中文手册", "运行环境" in data["使用手册"] and all(
            all(label in item["说明"] for label in ("目的", "原始请求", "关键字段", "合格返回", "失败示例", "期望结果"))
            for item in data["cases"]))
        main(["--once", "--output-parent", temporary], NoEnvironment(), forbidden)
        check("新目录不覆盖", len(list(Path(temporary).iterdir())) == 2)
        sent = []
        barrier = threading.Barrier(5, timeout=3)
        lock = threading.Lock()

        def fake_sender(body, timeout_seconds):
            with lock:
                sent.append((body, timeout_seconds))
                ordinal = len(sent)
            barrier.wait()
            return valid["jsonschema"] if ordinal % 2 else response("false")

        item = execute_case(cases()[1], "replacement-model", False, fake_sender)
        check("原次数无重试及模型唯一替换", len(sent) == 10 and all(
            body == {**cases()[1].body, "model": "replacement-model"} and seconds == 600 for body, seconds in sent))
        check("混合结果不合并", item["是否通过"] == "不通过" and len(item["实际结果"]) == 10
              and {attempt["judgement"]["是否通过"] for attempt in item["实际结果"]} == {"通过", "不通过"})
        check("原并发5分两批", barrier.n_waiting == 0 and not barrier.broken)
        once = execute_case(cases()[0], "kimi-k3", True, lambda body, seconds: valid["longtimeout"])
        check("每项一发选项", once["planned"] == 1 and len(once["实际结果"]) == 1 and once["是否通过"] == "通过")
        write_report(Path(temporary), {"mode": "离线假响应", "cases": [item]})
        text = (Path(temporary) / "report.txt").read_text()
        check("报告逐次完整", all(f"第{index}次" in text for index in range(1, 11))
              and all(label in text for label in ("期望结果", "实际结果", "是否通过")))
        payload = {"mode": "离线脱敏反例", "cases": [item], "extra": {
            "raw_response": r'{"echo":"\u0066ake-secret"}',
            "raw_response_base64": base64.b64encode(rb'{"echo":"\u0066ake-secret"}').decode(),
            "judgement": {"content": secret}}}
        write_report(Path(temporary), payload, secret)
        saved = json.loads((Path(temporary) / "results.json").read_text())
        check("整个落盘payload递归脱敏", secret not in json.dumps(saved)
              and secret not in json.dumps(json.loads(saved["extra"]["raw_response"]))
              and secret not in json.dumps(json.loads(base64.b64decode(saved["extra"]["raw_response_base64"]))))

        release_slow = threading.Event()
        first_started = threading.Event()
        sender_lock = threading.Lock()
        sent_count = [0]
        saved_while_slow = []

        def mixed_speed_sender(body, seconds):
            with sender_lock:
                sent_count[0] += 1
                number = sent_count[0]
            if number == 1:
                first_started.set()
                if not release_slow.wait(3):
                    raise AssertionError("已完成请求没有及时落盘并释放慢请求")
            else:
                if not first_started.wait(3):
                    raise AssertionError("慢请求没有启动")
            return valid["longtimeout"]

        def save_before_slow_finishes(progress):
            write_report(Path(temporary), {"mode": "离线逐次留存", "cases": [progress]})
            if progress["实际结果"] and not release_slow.is_set():
                saved = json.loads((Path(temporary) / "results.json").read_text())["cases"][0]
                saved_while_slow.append(len(saved["实际结果"]) >= 1
                                        and all(attempt["index"] != 1 for attempt in saved["实际结果"])
                                        and saved["是否通过"] == "不通过")
                release_slow.set()

        partial_item = execute_case(cases()[0], "kimi-k3", False, mixed_speed_sender, save_before_slow_finishes)
        check("慢请求未结束时已完成结果落盘", saved_while_slow == [True]
              and len(partial_item["实际结果"]) == 3 and partial_item["是否通过"] == "通过")
    print(f"离线自测：{len(checks)}/{len(checks)}通过；真实请求：0")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
