> ## Documentation Index
> Fetch the complete documentation index at: https://docs.akria.net/llms.txt
> Use this file to discover all available pages before exploring further.

# 第5组：状态机核心 | State Machine

> 测试你对 Pending, Leased, DeadLetter 等状态的理解

<a href="quiz" className="text-emerald-600 hover:underline text-sm">← 返回选单</a>

## 📝 词汇测验

export const QuizRunner = () => {
  const questions = [
    {
        "q": "'Pending' 状态是：",
        "o": [
            "正在处理",
            "待处理",
            "处理完成",
            "处理失败"
        ],
        "a": "待处理",
        "h": "刚生成，还没开始。"
    },
    {
        "q": "'Leased' 状态意味着：",
        "o": [
            "已释放",
            "已占用/锁定",
            "已失效",
            "已过期"
        ],
        "a": "已占用/锁定",
        "h": "被 worker 锁定。"
    },
    {
        "q": "'Failed' 表示：",
        "o": [
            "成功",
            "失败",
            "跳过",
            "未知"
        ],
        "a": "失败",
        "h": "可重试状态。"
    },
    {
        "q": "'Dead' 信件通常指：",
        "o": [
            "快速处理",
            "无法处理/死信",
            "重复处理",
            "安全处理"
        ],
        "a": "无法处理/死信",
        "h": "重试耗尽。"
    },
    {
        "q": "'Archived' 意为：",
        "o": [
            "最新",
            "已归档",
            "已删除",
            "已修改"
        ],
        "a": "已归档",
        "h": "已结束或过期。"
    },
    {
        "q": "'Accepted' 意为：",
        "o": [
            "驳回",
            "已接收",
            "忽略",
            "待定"
        ],
        "a": "已接收",
        "h": "系统确认接受。"
    },
    {
        "q": "'Sent' 意为：",
        "o": [
            "已接收",
            "已发送",
            "待发送",
            "发送中"
        ],
        "a": "已发送",
        "h": "任务投递成功。"
    },
    {
        "q": "'Partial' 意为：",
        "o": [
            "全部",
            "部分",
            "没有",
            "错误"
        ],
        "a": "部分",
        "h": "非完整的，局部。"
    },
    {
        "q": "'Stable' 意为：",
        "o": [
            "波动的",
            "稳定的",
            "危险的",
            "快速的"
        ],
        "a": "稳定的",
        "h": "状态持久且一致。"
    },
    {
        "q": "'Current' 意为：",
        "o": [
            "过去的",
            "当前的",
            "未来的",
            "历史的"
        ],
        "a": "当前的",
        "h": "现在的，此刻的。"
    }
];

  const [idx, setIdx] = React.useState(0);
  const [scr, setScr] = React.useState(0);
  const [fin, setFin] = React.useState(false);
  const [sel, setSel] = React.useState(null);
  const [ok, setOk] = React.useState(null);
  const [ready, setReady] = React.useState(false);
  const [sq, setSq] = React.useState([]);

  React.useEffect(() => {
    setReady(true);
    setSq(questions.map(q => ({ ...q, o: [...q.o].sort(() => Math.random() - 0.5) })));
  }, []);

  const onAnswer = (o) => {
    if (sel) return;
    setSel(o);
    const isOk = o === questions[idx].a;
    setOk(isOk);
    if (isOk) setScr(scr + 1);
    setTimeout(() => {
      if (idx + 1 < questions.length) {
        setIdx(idx + 1);
        setSel(null);
        setOk(null);
      } else {
        setFin(true);
      }
    }, 1500);
  };

  if (!ready) return <div className="p-10 text-center opacity-50">Loading...</div>;

  if (fin) {
    return (
      <div className="p-10 rounded-2xl bg-white dark:bg-gray-900 border text-center shadow-xl">
        <div className="text-2xl font-bold text-emerald-600 mb-4">测试完成！</div>
        <div className="text-lg mb-8">得分: {scr} / {questions.length}</div>
        <div className="flex gap-4 justify-center">
            <button className="bg-emerald-600 hover:bg-emerald-700 text-white px-8 py-3 rounded-lg font-bold transition-all" onClick={() => {setIdx(0);setScr(0);setFin(false);setSel(null);setOk(null);}}>重新开始</button>
            <a href="quiz" className="bg-gray-100 hover:bg-gray-200 text-gray-700 px-8 py-3 rounded-lg font-bold transition-all no-underline">返回选单</a>
        </div>
      </div>
    );
  }

  const curr = sq[idx];
  if (!curr) return null;

  return (
    <div className="p-6 rounded-2xl bg-white dark:bg-gray-900 border shadow-lg">
      <div className="mb-4 text-xs opacity-50 font-mono text-right">进度: {idx + 1} / {questions.length}</div>
      
      <div className="h-1 bg-gray-100 rounded-full mb-6 overflow-hidden">
        <div className="h-full bg-emerald-500 transition-all duration-300" style={{width: ((idx + 1) / questions.length * 100) + '%'}}></div>
      </div>

      <div className="text-xl font-bold mb-8 text-gray-900 dark:text-gray-100">{curr.q}</div>
      
      <div className="space-y-3">
        {curr.o.map((o, i) => (
          <button 
            key={i} 
            className={"w-full text-left p-4 rounded-xl border-2 transition-all font-medium " + (sel === o ? (ok ? "bg-emerald-50 dark:bg-emerald-900/20 border-emerald-500 text-emerald-700 dark:text-emerald-400" : "bg-red-50 dark:bg-red-900/20 border-red-500 text-red-700 dark:text-red-400") : (sel && o === curr.a ? "bg-emerald-50 dark:bg-emerald-900/20 border-emerald-500 text-emerald-700 dark:text-emerald-400 opacity-80" : "bg-gray-50 dark:bg-gray-800 border-gray-100 dark:border-gray-700 hover:border-emerald-500 hover:shadow-md text-gray-700 dark:text-gray-300"))}
            onClick={() => onAnswer(o)}
          >
            {o}
          </button>
        ))}
      </div>
      
      {sel && (
        <div className={"mt-6 p-4 rounded-lg border-l-4 " + (ok ? "bg-emerald-50 dark:bg-emerald-900/20 border-emerald-500 text-emerald-800 dark:text-emerald-300" : "bg-red-50 dark:bg-red-900/20 border-red-500 text-red-800 dark:text-red-300")}>
          <div className="font-bold flex items-center gap-2">
            {ok ? "✅ 正确!" : "❌ 错误"}
            <span className="font-normal opacity-70 text-sm ml-auto">正确答案: {curr.a}</span>
          </div>
          <div className="text-sm opacity-80 mt-2">💡 提示: {curr.h}</div>
        </div>
      )}
    </div>
  );
};

<QuizRunner />
