> ## 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.

# 第4组：架构模式 | Patterns & Messaging

> 测试你对 Outbox, Idempotency 等模式的理解

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

## 📝 词汇测验

export const QuizRunner = () => {
  const questions = [
    {
        "q": "'Outbox' 模式用于：",
        "o": [
            "发送邮件",
            "发件箱/一致性发送",
            "存储文件",
            "清理日志"
        ],
        "a": "发件箱/一致性发送",
        "h": "待投递队列。"
    },
    {
        "q": "'Idempotent' 意为：",
        "o": [
            "排他",
            "幂等",
            "共享",
            "递增"
        ],
        "a": "幂等",
        "h": "多次执行结果一致。"
    },
    {
        "q": "'Lease' 用于：",
        "o": [
            "租赁/任务锁定",
            "转让",
            "删除",
            "复制"
        ],
        "a": "租赁/任务锁定",
        "h": "临时锁定任务。"
    },
    {
        "q": "'ACK' 是什么缩写？",
        "o": [
            "Action",
            "Acknowledge/确认",
            "Active",
            "Account"
        ],
        "a": "Acknowledge/确认",
        "h": "告知任务已完成。"
    },
    {
        "q": "'Workflow' 意为：",
        "o": [
            "流水线",
            "工作流",
            "办公室",
            "文件夹"
        ],
        "a": "工作流",
        "h": "处理步骤集合。"
    },
    {
        "q": "'Pull' 模式是：",
        "o": [
            "推送",
            "拉取",
            "同步",
            "异步"
        ],
        "a": "拉取",
        "h": "主动获取数据。"
    },
    {
        "q": "'Push' 模式是：",
        "o": [
            "拉取",
            "推送",
            "广播",
            "轮询"
        ],
        "a": "推送",
        "h": "主动发送数据。"
    },
    {
        "q": "'Dispatch' 意为：",
        "o": [
            "分发/调度",
            "取消",
            "延迟",
            "聚合"
        ],
        "a": "分发/调度",
        "h": "分配任务。"
    },
    {
        "q": "'Retry' 意为：",
        "o": [
            "停止",
            "重试",
            "忽略",
            "跳过"
        ],
        "a": "重试",
        "h": "再次执行失败的操作。"
    },
    {
        "q": "'Timeout' 意为：",
        "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 />
