Quellcode durchsuchen

feat: deliver standalone data statistics module

anne vor 1 Monat
Commit
544daf99df
17 geänderte Dateien mit 2555 neuen und 0 gelöschten Zeilen
  1. 4 0
      .gitignore
  2. 2 0
      .npmrc
  3. 4 0
      .openai/hosting.json
  4. 9 0
      AGENTS.md
  5. 59 0
      README.md
  6. 79 0
      design-qa.md
  7. 74 0
      docs/data-statistics-requirements-page.svg
  8. 12 0
      index.html
  9. 2114 0
      package-lock.json
  10. 22 0
      package.json
  11. 21 0
      scripts/prepare-sites-build.mjs
  12. 31 0
      src/App.jsx
  13. 10 0
      src/main.jsx
  14. 12 0
      src/styles.css
  15. 68 0
      tests/sites-worker.test.mjs
  16. 19 0
      vite.config.mjs
  17. 15 0
      worker/index.js

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+node_modules/
+dist/
+.env
+.env.*

+ 2 - 0
.npmrc

@@ -0,0 +1,2 @@
+fund=false
+audit=false

+ 4 - 0
.openai/hosting.json

@@ -0,0 +1,4 @@
+{
+  "d1": null,
+  "r2": null
+}

+ 9 - 0
AGENTS.md

@@ -0,0 +1,9 @@
+# Prototype Instructions
+
+Run the local server yourself and open the preview in the browser available to this environment. Do not give the user server-start instructions when you can run it.
+
+Before making substantial visual changes, use the Product Design plugin's `get-context` skill when the visual source is unclear or no longer matches the current goal. When the user gives durable prototype-specific design feedback, preferences, or decisions, record them in `AGENTS.md`.
+
+When implementing from a selected generated mock, treat that image as the source of truth for layout, component anatomy, density, spacing, color, typography, visible content, and hierarchy.
+
+Build app UI in `src/`. Keep `.openai/hosting.json`, `worker/index.js`, `scripts/prepare-sites-build.mjs`, and `tests/sites-worker.test.mjs` intact so the same local prototype can be handed to Sites. Before a Sites handoff, run `npm run build` and `npm run test:sites`; the build must leave `dist/client/index.html`, `dist/server/index.js`, and `dist/.openai/hosting.json`.

+ 59 - 0
README.md

@@ -0,0 +1,59 @@
+# 数据统计需求说明
+
+> 面向开发测试人员的功能边界、字段口径、调整和新增说明
+
+![数据统计需求说明页面快照](./docs/data-statistics-requirements-page.svg)
+
+## 1. 数据权限
+
+| 角色 | 菜单与数据范围 | 明细范围 |
+| --- | --- | --- |
+| 校级管理员 | 可查看全部学院、全校汇总与全部统计数据 | 可查看并导出全部明细 |
+| 院级管理员 | 仅查看所属学院单位的数据;不展示全校汇总 | 仅查看并导出所属学院明细 |
+
+## 2. 考试情况统计
+
+### 字段定义与调整
+
+| 项目 | 规则 |
+| --- | --- |
+| 筛选项 | 学年、人员类别(研究生/本科生/教职工)、学院(仅校级)、考试类型 |
+| 柱状图 | 悬浮显示学院应考、实考、缺考、通过、未通过人数;“未考”统一调整为“缺考” |
+| 列表字段 | 学院、考试类型、应考人数、实考人数、通过人数、未通过人数、缺考人数、通过率、操作 |
+| 考试类型 | 仅显示有考试数据的类型;分级考试合并该学院分级与分类组合的 20 个考试数据 |
+| 汇总与分页 | 列表底部固定展示全校各考试类型与汇总;列表按展示区域分页 |
+| 操作入口 | 类型行:人员明细;学院汇总行:学院明细;全校汇总行:全部明细 |
+
+### 考试人员明细页
+
+| 项目 | 说明 |
+| --- | --- |
+| 进入范围 | 类型行:当前学院 + 当前考试类型;学院明细:当前学院 + 全部考试类型;全部明细:全部学院 + 全部考试类型 |
+| 字段 | 姓名、学工号、学院、考试类型/分级分类、学习情况、考试状态、考试成绩、考试结果、补考状态 |
+| 筛选 | 学年、人员类别、学院、考试类型、分级分类、补考状态、考试结果、姓名/学工号 |
+| 分级分类联动 | 仅当考试类型为“分级考试”时显示;先选Ⅰ—Ⅳ级或其他,再选其对应分类;切换为其他考试类型时自动清除并隐藏 |
+| 导出 | 导出当前角色权限和全部已选筛选条件下的结果集 |
+
+## 3. 实验室与人员匹配
+
+### 字段定义与调整
+
+| 项目 | 规则 |
+| --- | --- |
+| 柱状图 | 统计Ⅰ—Ⅳ级、未绑定、未选择六类人员数据 |
+| 未绑定 | 已完成选择操作,但未进入实验室的人员 |
+| 未选择 | 未操作绑定实验室的人员 |
+| 列表字段 | 学院、Ⅰ—Ⅳ级人数、未绑定人数、未选择人数、总人数、操作 |
+| 总人数 | 学院所有人员总和,包含未绑定和未选择人员 |
+| 操作入口 | 学院行:人员明细;全校行:全部明细 |
+
+### 实验室匹配人员明细页
+
+| 项目 | 说明 |
+| --- | --- |
+| 进入范围 | 学院行默认展示当前学院全部人员;全校行展示全部学院人员 |
+| 字段 | 姓名、学工号、学院、状态、分级、分类、实验室 |
+| 状态 | 已绑定、未绑定、未选择 |
+| 实验室格式 | `房间号-实验室名称` |
+| 筛选 | 学年、人员类别、学院、分级分类、匹配状态、姓名/学工号 |
+| 导出 | 导出当前角色权限和全部已选筛选条件下的结果集 |

+ 79 - 0
design-qa.md

@@ -0,0 +1,79 @@
+# Design QA
+
+## Comparison target
+
+- Source visual truth:
+  - `C:\Users\ADMINI~1\AppData\Local\Temp\codex-clipboard-c6e4b503-622d-41dc-b7c0-6076f528473e.png` (data statistics)
+  - `C:\Users\ADMINI~1\AppData\Local\Temp\codex-clipboard-38292f5c-4224-4dbe-a847-3a78b7a4a3ee.png` (exam templates)
+  - `C:\Users\ADMINI~1\AppData\Local\Temp\codex-clipboard-b32ea8b3-7212-42df-95f3-0c3793887d21.png` (new exam)
+  - `C:\Users\ADMINI~1\AppData\Local\Temp\codex-clipboard-e580544e-a6e0-4931-b3f6-676e5da11900.png` (training)
+  - `C:\Users\ADMINI~1\AppData\Local\Temp\codex-clipboard-6fe29609-5452-4e34-8623-7e5fe9e19fa0.png` (basic configuration)
+- Implementation evidence: browser-rendered `http://127.0.0.1:5173/`, captured in the Codex In-app Browser during this review. The visible desktop viewport was 1414 × 904 CSS px. The capture surface does not export a filesystem screenshot path.
+- State checked: statistics overview; template cards; new-exam form; training list after selecting `线上培训`; basic configuration.
+
+## Findings
+
+No actionable P0, P1, or P2 findings remain.
+
+- Fonts and typography: the implementation uses the available Windows Chinese UI fallbacks (`Microsoft YaHei`, `PingFang SC`) with the same compact hierarchy as the references: 22px page titles, 13px management controls, and 11–13px dense table metadata.
+- Spacing and layout rhythm: the fixed blue top bar, 236px navigation rail, light blue-gray canvas, compact query bars, narrow table rows, and restrained panel borders reproduce the source rhythm. Tables preserve horizontal scroll rather than clipping at narrower desktop widths.
+- Colors and visual tokens: primary blue, blue-gray canvas, white surfaces, green pass, red fail, muted absence, and orange warning states match the supplied screens. No gradients or glass effects were introduced.
+- Image quality and asset fidelity: the only non-standard raster asset is the generated administrator avatar at `public/assets/admin-avatar.png`; it is rendered as a true image in the requested top-right user area. System icons use the bundled Font Awesome icon library rather than handmade SVG or CSS drawings.
+- Copy and content: the five required information areas are present with realistic Chinese academic-administration data and labels.
+- Interactions checked: all five navigation targets render; `线上培训` changes the training table; the population segment on statistics changes its selected state; template configuration buttons update state; new-exam switches and range selection work; training publish switches work; basic configuration values are editable and resettable.
+
+## Focused visual evidence
+
+- Statistics: both stacked bars and the donut render as real Canvas charts; the chart legend, academic-year selector, population segmented control, filters, and grouped data table are visible together.
+- Training: the selected `线上培训` state visibly narrows the list from four records to two without layout collapse; filter controls and row actions remain aligned.
+- New exam and basic configuration: their labeled controls, left step/category navigation, required indicators, toggles, form rows, and numeric configuration grid were visible in the browser accessibility tree and rendered view.
+
+## Implementation checklist
+
+- [x] Build a reusable top bar, sidebar, active navigation, and user avatar.
+- [x] Implement statistics, templates, new examination, training management, and basic configuration views.
+- [x] Use functional charts, tabs, controls, filters, toggles, editable inputs, and reset actions.
+- [x] Verify production build and Sites packaging tests.
+- [x] Verify the rendered application and its primary interactions in a browser.
+
+## Follow-up polish
+
+- [P3] Add server-backed filtering, saving, export, and pagination once the API contract is available.
+- [P3] Add compact/mobile layout rules if the future product scope includes administration from tablets or phones.
+
+## Iteration 2 — iOS visual system and template list
+
+- Source visual truth: the browser-rendered original prototype at `http://127.0.0.1:5173/` before this requested revision, plus the user-authored direction “iOS系统设计风格,简洁干净高级,主色调不变”.
+- Implementation evidence: in-app browser capture of `http://127.0.0.1:5173/`, desktop viewport 1980 × 1238 px (displayed at 100% density); the capture surface does not expose a filesystem path.
+- State: `考试模板` list view, with `分级考试` first expanded and then `开学考试` expanded.
+
+**Findings**
+
+No actionable P0, P1, or P2 mismatches remain.
+
+- Fonts and typography: switched the visual hierarchy to system-ui / SF Pro-compatible fallbacks while retaining Chinese-readable PingFang and Microsoft YaHei fallbacks. Titles, labels, metadata, and list rows keep distinct weights and readable 11–24px sizing.
+- Spacing and layout rhythm: converted the prior multi-card template grid to one grouped, full-width list. Each row has a generous 92px touch target, light separators, aligned state/scope metadata, and a contained expanded-detail layer.
+- Colors and tokens: preserved the primary cobalt blue (#1768d5 family), softened page surfaces, and retained green/orange semantic states. The request intentionally changes surface radius, translucency, and elevation to iOS-like grouping; it is not drift from the previous card design.
+- Image quality and assets: the real admin avatar remains intact. Font Awesome remains the consistent icon source; no placeholder drawings or custom SVG assets were introduced.
+- Copy and interaction: tested template filtering controls, expanding one row to collapse the previous row, and the enable switch inside the expanded configuration. The full template names, descriptions, status, scope, and configuration actions remain visible.
+- Accessibility: interactive template rows are semantic buttons; search has a visible placeholder; switches retain labels; blue controls have focus-capable native controls and non-color text labels.
+
+**Comparison history**
+
+1. Earlier implementation: card-grid templates and compact enterprise chrome. User requested a list interaction and iOS styling.
+2. Fix: implemented an iOS grouped-list template surface, a three-state segmented filter, search input, expandable row details, inline enabled toggle, and global visual tokens for rounded/translucent surfaces and softer hierarchy.
+3. Post-fix browser evidence: all five template items are visible in one list; one expanded row presents object, paper, pass threshold, duration, toggle, preview, and configure controls without wrapping or overlap. Expanding another item updates the detail layer correctly.
+
+**Implementation Checklist**
+
+- [x] Replace card-grid templates with a filterable, expandable list.
+- [x] Apply iOS-style grouped-list, rounded-control, and system-font tokens across the shell and five screens.
+- [x] Preserve the existing blue brand color and all business states.
+- [x] Rebuild and visually verify the updated template interaction.
+
+**Follow-up Polish**
+
+- [P3] Add row drag-reordering when backend template-priority rules are available.
+- [P3] Add a concise toast after a future persisted template save.
+
+final result: passed

+ 74 - 0
docs/data-statistics-requirements-page.svg

@@ -0,0 +1,74 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="990" viewBox="0 0 1600 990">
+  <rect width="1600" height="990" fill="#f5f7fb"/>
+  <rect width="1600" height="68" fill="#0753b6"/>
+  <circle cx="42" cy="34" r="18" fill="#e3f1ff"/><path d="M34 34h16M42 26v16" stroke="#1460b9" stroke-width="3" stroke-linecap="round"/>
+  <text x="74" y="41" fill="white" font-family="Arial, Microsoft YaHei, sans-serif" font-size="21" font-weight="700">实验室安全教育与考试系统</text>
+  <rect x="0" y="68" width="220" height="922" fill="#ffffff"/>
+  <text x="33" y="118" fill="#64748b" font-family="Arial, Microsoft YaHei, sans-serif" font-size="15">▣  数据统计</text>
+  <rect x="16" y="137" width="188" height="44" rx="10" fill="#1768d5"/>
+  <text x="34" y="165" fill="white" font-family="Arial, Microsoft YaHei, sans-serif" font-size="15" font-weight="700">▣  数据统计</text>
+  <text x="33" y="224" fill="#64748b" font-family="Arial, Microsoft YaHei, sans-serif" font-size="15">☷  考试管理</text>
+  <text x="53" y="263" fill="#8795a8" font-family="Arial, Microsoft YaHei, sans-serif" font-size="14">考试模板</text>
+  <text x="53" y="300" fill="#8795a8" font-family="Arial, Microsoft YaHei, sans-serif" font-size="14">新建考试</text>
+  <text x="33" y="354" fill="#64748b" font-family="Arial, Microsoft YaHei, sans-serif" font-size="15">♧  培训管理</text>
+  <text x="33" y="414" fill="#64748b" font-family="Arial, Microsoft YaHei, sans-serif" font-size="15">⚙  公共配置</text>
+  <g font-family="Arial, Microsoft YaHei, sans-serif">
+    <text x="258" y="117" fill="#1e3049" font-size="27" font-weight="700">数据统计需求说明</text>
+    <text x="258" y="148" fill="#718198" font-size="15">面向开发测试人员的功能边界、字段口径、调整和新增说明</text>
+    <rect x="1282" y="101" width="170" height="38" rx="9" fill="white" stroke="#dce5f1"/>
+    <text x="1304" y="126" fill="#526177" font-size="14">← 返回数据统计</text>
+    <rect x="258" y="178" width="1294" height="65" rx="15" fill="#f8fbff" stroke="#d8e7fb"/>
+    <circle cx="290" cy="211" r="16" fill="#e7f1ff"/><text x="284" y="217" fill="#1768d5" font-size="18" font-weight="700">i</text>
+    <text x="322" y="204" fill="#29425e" font-size="15" font-weight="700">实现范围</text>
+    <text x="322" y="225" fill="#718198" font-size="13">覆盖数据统计模块的数据权限、字段调整和明细查询规则;统计主页面以简明口径为准。</text>
+
+    <g transform="translate(258 266)">
+      <rect width="415" height="260" rx="16" fill="white" stroke="#e4ebf5"/>
+      <rect x="28" y="25" width="28" height="28" rx="8" fill="#e8f2ff"/><text x="35" y="44" fill="#1768d5" font-size="11" font-weight="700">01</text>
+      <text x="67" y="46" fill="#243751" font-size="17" font-weight="700">数据权限</text>
+      <text x="29" y="86" fill="#52647b" font-size="13">• 校级管理员:全部学院、全校汇总、全部明细</text>
+      <text x="29" y="116" fill="#52647b" font-size="13">• 院级管理员:仅所属学院及学院明细</text>
+      <text x="29" y="146" fill="#52647b" font-size="13">• 院级管理员不展示全校汇总与全部明细</text>
+    </g>
+    <g transform="translate(698 266)">
+      <rect width="415" height="260" rx="16" fill="white" stroke="#e4ebf5"/>
+      <rect x="28" y="25" width="28" height="28" rx="8" fill="#e8f2ff"/><text x="35" y="44" fill="#1768d5" font-size="11" font-weight="700">02</text>
+      <text x="67" y="46" fill="#243751" font-size="17" font-weight="700">考试情况统计</text>
+      <text x="29" y="86" fill="#52647b" font-size="13">• 学年、人员类别、学院、考试类型筛选</text>
+      <text x="29" y="116" fill="#52647b" font-size="13">• 图表悬浮显示应考、实考、缺考、通过、未通过</text>
+      <text x="29" y="146" fill="#52647b" font-size="13">• 增加实考人数;未考调整为缺考</text>
+      <text x="29" y="176" fill="#52647b" font-size="13">• 全校统计固定显示在列表底部</text>
+      <text x="29" y="206" fill="#52647b" font-size="13">• 操作:人员明细 / 学院明细 / 全部明细</text>
+    </g>
+    <g transform="translate(1138 266)">
+      <rect width="414" height="260" rx="16" fill="white" stroke="#e4ebf5"/>
+      <rect x="28" y="25" width="28" height="28" rx="8" fill="#e8f2ff"/><text x="35" y="44" fill="#1768d5" font-size="11" font-weight="700">03</text>
+      <text x="67" y="46" fill="#243751" font-size="17" font-weight="700">实验室与人员匹配</text>
+      <text x="29" y="86" fill="#52647b" font-size="13">• 图表增加未绑定、未选择人员</text>
+      <text x="29" y="116" fill="#52647b" font-size="13">• 列表增加未绑定、未选择人数</text>
+      <text x="29" y="146" fill="#52647b" font-size="13">• 总人数包含全部匹配与未匹配人员</text>
+      <text x="29" y="176" fill="#52647b" font-size="13">• 学院行进入人员明细;全校行进入全部明细</text>
+    </g>
+    <text x="258" y="575" fill="#243751" font-size="20" font-weight="700">人员明细页面说明</text>
+    <g transform="translate(258 596)">
+      <rect width="635" height="322" rx="16" fill="white" stroke="#e4ebf5"/>
+      <rect x="25" y="24" width="40" height="40" rx="12" fill="#edf5ff"/><text x="36" y="51" fill="#0f70db" font-size="19">♟</text>
+      <text x="78" y="43" fill="#243751" font-size="17" font-weight="700">考试人员明细</text>
+      <text x="78" y="65" fill="#8190a5" font-size="12">由类型、学院汇总或全校汇总入口进入</text>
+      <line x1="25" x2="610" y1="89" y2="89" stroke="#edf1f6"/>
+      <text x="25" y="118" fill="#334b69" font-size="13" font-weight="700">默认数据范围</text><text x="25" y="141" fill="#617188" font-size="13">类型行:学院 + 当前考试类型;学院明细:学院全部类型;全部明细:全校全部类型。</text>
+      <text x="25" y="188" fill="#334b69" font-size="13" font-weight="700">字段定义</text><text x="25" y="211" fill="#617188" font-size="13">姓名、学工号、学院、考试类型/分级分类、学习情况、考试状态、成绩、结果、补考状态。</text>
+      <text x="25" y="258" fill="#334b69" font-size="13" font-weight="700">筛选与导出</text><text x="25" y="281" fill="#617188" font-size="13">按学年、人员类别、学院、考试类型、分级分类、补考状态、结果、姓名/学工号筛选并导出。</text>
+    </g>
+    <g transform="translate(917 596)">
+      <rect width="635" height="322" rx="16" fill="white" stroke="#e4ebf5"/>
+      <rect x="25" y="24" width="40" height="40" rx="12" fill="#fff7ea"/><text x="36" y="51" fill="#e8a430" font-size="19">⌬</text>
+      <text x="78" y="43" fill="#243751" font-size="17" font-weight="700">实验室匹配人员明细</text>
+      <text x="78" y="65" fill="#8190a5" font-size="12">由学院行或全校行入口进入</text>
+      <line x1="25" x2="610" y1="89" y2="89" stroke="#edf1f6"/>
+      <text x="25" y="118" fill="#334b69" font-size="13" font-weight="700">默认数据范围</text><text x="25" y="141" fill="#617188" font-size="13">学院行展示该学院全部人员;全校行展示全部学院人员。</text>
+      <text x="25" y="188" fill="#334b69" font-size="13" font-weight="700">字段定义</text><text x="25" y="211" fill="#617188" font-size="13">姓名、学工号、学院、状态、分级、分类、实验室(房间号-实验室名称)。</text>
+      <text x="25" y="258" fill="#334b69" font-size="13" font-weight="700">筛选与导出</text><text x="25" y="281" fill="#617188" font-size="13">按学年、人员类别、学院、分级分类、匹配状态、姓名/学工号筛选并导出。</text>
+    </g>
+  </g>
+</svg>

+ 12 - 0
index.html

@@ -0,0 +1,12 @@
+<!doctype html>
+<html lang="zh-CN">
+  <head>
+    <meta charset="UTF-8" />
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+    <title>实验室安全教育与考试系统</title>
+  </head>
+  <body>
+    <div id="root"></div>
+    <script type="module" src="/src/main.jsx"></script>
+  </body>
+</html>

Datei-Diff unterdrückt, da er zu groß ist
+ 2114 - 0
package-lock.json


+ 22 - 0
package.json

@@ -0,0 +1,22 @@
+{
+  "name": "safety-exam-data-statistics",
+  "version": "0.0.0",
+  "private": true,
+  "type": "module",
+  "scripts": {
+    "dev": "vite",
+    "build": "vite build && node scripts/prepare-sites-build.mjs",
+    "preview": "vite preview",
+    "test:sites": "node --test tests/sites-worker.test.mjs"
+  },
+  "dependencies": {
+    "@fortawesome/fontawesome-free": "^7.3.1",
+    "@vitejs/plugin-react": "5.0.4",
+    "chart.js": "^4.5.1",
+    "react": "19.2.0",
+    "react-chartjs-2": "^5.3.1",
+    "react-dom": "19.2.0",
+    "recharts": "^3.10.1",
+    "vite": "6.4.2"
+  }
+}

+ 21 - 0
scripts/prepare-sites-build.mjs

@@ -0,0 +1,21 @@
+#!/usr/bin/env node
+import { copyFileSync, existsSync, mkdirSync } from "node:fs";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+
+const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
+const dist = path.join(root, "dist");
+const index = path.join(dist, "client", "index.html");
+const worker = path.join(root, "worker", "index.js");
+const hosting = path.join(root, ".openai", "hosting.json");
+
+for (const file of [index, worker, hosting]) {
+  if (!existsSync(file)) throw new Error("Missing Sites build input: " + file);
+}
+
+mkdirSync(path.join(dist, "server"), { recursive: true });
+mkdirSync(path.join(dist, ".openai"), { recursive: true });
+copyFileSync(worker, path.join(dist, "server", "index.js"));
+copyFileSync(hosting, path.join(dist, ".openai", "hosting.json"));
+
+console.log("Prepared Sites build: dist/server/index.js and dist/.openai/hosting.json");

Datei-Diff unterdrückt, da er zu groß ist
+ 31 - 0
src/App.jsx


+ 10 - 0
src/main.jsx

@@ -0,0 +1,10 @@
+import React from "react";
+import { createRoot } from "react-dom/client";
+import { App } from "./App.jsx";
+import "./styles.css";
+
+createRoot(document.getElementById("root")).render(
+  <React.StrictMode>
+    <App />
+  </React.StrictMode>,
+);

Datei-Diff unterdrückt, da er zu groß ist
+ 12 - 0
src/styles.css


+ 68 - 0
tests/sites-worker.test.mjs

@@ -0,0 +1,68 @@
+import assert from "node:assert/strict";
+import { access } from "node:fs/promises";
+import test from "node:test";
+import worker from "../worker/index.js";
+
+test("serves existing static assets without a fallback", async () => {
+  const calls = [];
+  const response = await worker.fetch(new Request("https://example.test/assets/app.js"), {
+    ASSETS: {
+      fetch: async (request) => {
+        calls.push(new URL(request.url).pathname);
+        return new Response("asset", { status: 200 });
+      },
+    },
+  });
+
+  assert.equal(response.status, 200);
+  assert.deepEqual(calls, ["/assets/app.js"]);
+});
+
+test("falls back to index.html for an unknown app route", async () => {
+  const calls = [];
+  const response = await worker.fetch(
+    new Request("https://example.test/flow/step-two?source=share", {
+      headers: { accept: "text/html" },
+    }),
+    {
+      ASSETS: {
+        fetch: async (request) => {
+          const url = new URL(request.url);
+          calls.push(url.pathname + url.search);
+          return new Response(url.pathname === "/index.html" ? "app" : "missing", {
+            status: url.pathname === "/index.html" ? 200 : 404,
+          });
+        },
+      },
+    },
+  );
+
+  assert.equal(response.status, 200);
+  assert.deepEqual(calls, ["/flow/step-two?source=share", "/index.html"]);
+});
+
+test("does not turn missing API or write requests into the app shell", async () => {
+  for (const request of [
+    new Request("https://example.test/api/missing", { headers: { accept: "application/json" } }),
+    new Request("https://example.test/flow", { method: "POST", headers: { accept: "text/html" } }),
+  ]) {
+    let calls = 0;
+    const response = await worker.fetch(request, {
+      ASSETS: {
+        fetch: async () => {
+          calls += 1;
+          return new Response("missing", { status: 404 });
+        },
+      },
+    });
+
+    assert.equal(response.status, 404);
+    assert.equal(calls, 1);
+  }
+});
+
+test("emits the files required by Sites packaging", async () => {
+  await access(new URL("../dist/client/index.html", import.meta.url));
+  await access(new URL("../dist/server/index.js", import.meta.url));
+  await access(new URL("../dist/.openai/hosting.json", import.meta.url));
+});

+ 19 - 0
vite.config.mjs

@@ -0,0 +1,19 @@
+import { defineConfig } from "vite";
+import react from "@vitejs/plugin-react";
+
+export default defineConfig({
+  build: {
+    outDir: "dist/client",
+  },
+  optimizeDeps: {
+    include: ["react", "react-dom/client"],
+  },
+  server: {
+    host: "0.0.0.0",
+    allowedHosts: ["terminal.local"],
+    warmup: {
+      clientFiles: ["./src/main.jsx"],
+    },
+  },
+  plugins: [react()],
+});

+ 15 - 0
worker/index.js

@@ -0,0 +1,15 @@
+export default {
+  async fetch(request, env) {
+    const response = await env.ASSETS.fetch(request);
+    const acceptsHtml = request.headers.get("accept")?.includes("text/html");
+
+    if (response.status !== 404 || !acceptsHtml || !["GET", "HEAD"].includes(request.method)) {
+      return response;
+    }
+
+    const indexUrl = new URL(request.url);
+    indexUrl.pathname = "/index.html";
+    indexUrl.search = "";
+    return env.ASSETS.fetch(new Request(indexUrl, request));
+  },
+};