devmode.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="apple-mobile-web-app-capable" content="yes" />
  5. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
  6. <meta charset="UTF-8">
  7. <meta name="theme-color" content="#0078d4">
  8. <script src="../script/jquery.min.js"></script>
  9. <script src="../script/ao_module.js"></script>
  10. <title>AGI Dev Mode Test</title>
  11. <style>
  12. *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  13. body {
  14. font-family: 'Segoe UI Variable', 'Segoe UI', system-ui, -apple-system, sans-serif;
  15. font-size: 14px;
  16. background: #f3f3f3;
  17. color: #201f1e;
  18. min-height: 100vh;
  19. }
  20. /* ── Nav bar ───────────────────────────────────────────── */
  21. .nav-bar { position: sticky; top: 0; z-index: 100; height: 48px; background: rgba(243,243,243,0.9); backdrop-filter: blur(20px) saturate(180%); border-bottom: 1px solid rgba(0,0,0,0.08); display: flex; align-items: center; padding: 0 20px; gap: 4px; }
  22. .nav-brand { font-size: 13px; font-weight: 600; color: #201f1e; margin-right: 12px; }
  23. .nav-tab { display: inline-flex; align-items: center; height: 48px; padding: 0 12px; font-size: 13px; color: #605e5c; text-decoration: none; border-bottom: 2px solid transparent; transition: color 0.1s, border-color 0.1s; white-space: nowrap; }
  24. .nav-tab:hover { color: #201f1e; }
  25. .nav-tab.active { color: #0078d4; border-bottom-color: #0078d4; font-weight: 600; }
  26. /* ── Content ───────────────────────────────────────────── */
  27. .content { max-width: 880px; margin: 24px auto; padding: 0 20px; }
  28. .page-title { font-size: 20px; font-weight: 600; margin-bottom: 4px; }
  29. .page-subtitle { font-size: 13px; color: #797775; margin-bottom: 20px; line-height: 1.5; }
  30. .page-subtitle code { font-family: 'Cascadia Code', 'Consolas', monospace; background: #ebebeb; padding: 1px 5px; border-radius: 3px; font-size: 12px; }
  31. /* ── Toggle row ────────────────────────────────────────── */
  32. .toggle-row {
  33. display: flex;
  34. align-items: center;
  35. gap: 12px;
  36. padding: 12px 16px;
  37. background: #fff;
  38. border: 1px solid #e0e0e0;
  39. border-radius: 8px;
  40. margin-bottom: 14px;
  41. }
  42. .toggle-switch { position: relative; width: 40px; height: 22px; flex-shrink: 0; }
  43. .toggle-switch input { opacity: 0; width: 0; height: 0; }
  44. .toggle-track {
  45. position: absolute; inset: 0;
  46. background: #c0c0c0;
  47. border-radius: 11px;
  48. cursor: pointer;
  49. transition: background 0.2s;
  50. }
  51. .toggle-track::after {
  52. content: '';
  53. position: absolute;
  54. left: 3px; top: 3px;
  55. width: 16px; height: 16px;
  56. background: #fff;
  57. border-radius: 50%;
  58. transition: transform 0.2s;
  59. }
  60. .toggle-switch input:checked + .toggle-track { background: #0078d4; }
  61. .toggle-switch input:checked + .toggle-track::after { transform: translateX(18px); }
  62. .toggle-label { font-size: 13px; font-weight: 500; }
  63. .toggle-hint { font-size: 12px; color: #797775; margin-left: auto; }
  64. /* ── Toolbar ───────────────────────────────────────────── */
  65. .toolbar { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
  66. .btn { display: inline-flex; align-items: center; gap: 6px; padding: 6px 16px; border-radius: 4px; font-family: inherit; font-size: 13px; cursor: pointer; border: 1px solid transparent; transition: background 0.1s; line-height: 1; }
  67. .btn-primary { background: #0078d4; color: #fff; border-color: #0078d4; }
  68. .btn-primary:hover { background: #106ebe; }
  69. .btn-secondary { background: #fff; color: #201f1e; border-color: #d1d1d1; }
  70. .btn-secondary:hover { background: #f5f5f5; }
  71. /* ── Test list card ────────────────────────────────────── */
  72. .test-list {
  73. background: #fff;
  74. border: 1px solid #e0e0e0;
  75. border-radius: 8px;
  76. overflow: hidden;
  77. box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  78. }
  79. .test-list-header {
  80. padding: 10px 16px;
  81. font-size: 11px;
  82. font-weight: 600;
  83. color: #797775;
  84. text-transform: uppercase;
  85. letter-spacing: 0.04em;
  86. border-bottom: 1px solid #f0f0f0;
  87. display: flex;
  88. align-items: center;
  89. gap: 12px;
  90. }
  91. .test-list-header .col-name { flex: 1; }
  92. .test-list-header .col-status { width: 82px; text-align: center; }
  93. .test-list-header .col-action { width: 52px; }
  94. .test-item {
  95. display: flex;
  96. align-items: center;
  97. gap: 12px;
  98. padding: 10px 16px;
  99. border-bottom: 1px solid #f5f5f5;
  100. transition: background 0.1s;
  101. }
  102. .test-item:hover { background: #fafafa; }
  103. .test-idx { font-size: 11px; color: #bbb; width: 22px; text-align: right; flex-shrink: 0; }
  104. .test-info { flex: 1; min-width: 0; }
  105. .test-name { font-size: 13px; font-weight: 500; }
  106. .test-desc { font-size: 11px; color: #797775; margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  107. .test-script { font-size: 11px; color: #bbb; font-family: 'Cascadia Code', 'Consolas', monospace; margin-top: 1px; }
  108. /* Status badges — same tokens as index.html */
  109. .status-badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px; border-radius: 12px; font-size: 11px; font-weight: 600; width: 82px; justify-content: center; flex-shrink: 0; }
  110. .status-dot { width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }
  111. .s-pending { background: #f3f3f3; color: #797775; }
  112. .s-pending .status-dot { background: #c0c0c0; }
  113. .s-running { background: #eff6fc; color: #0078d4; }
  114. .s-running .status-dot { background: #0078d4; animation: pulse 1s ease-in-out infinite; }
  115. .s-pass { background: #dff6dd; color: #107c10; }
  116. .s-pass .status-dot { background: #107c10; }
  117. .s-fail { background: #fde7e9; color: #c50f1f; }
  118. .s-fail .status-dot { background: #c50f1f; }
  119. @keyframes pulse { 0%,100%{opacity:1;transform:scale(1)} 50%{opacity:.4;transform:scale(.7)} }
  120. .col-action { width: 52px; flex-shrink: 0; text-align: right; }
  121. .btn-run { padding: 4px 10px; font-size: 12px; border-radius: 3px; background: transparent; border: 1px solid #d1d1d1; color: #444; cursor: pointer; font-family: inherit; transition: background 0.1s; }
  122. .btn-run:hover { background: #f0f0f0; }
  123. .btn-run:disabled { opacity: 0.45; cursor: default; }
  124. /* ── Result area ───────────────────────────────────────── */
  125. .result-area {
  126. display: none;
  127. padding: 10px 16px 12px 50px;
  128. background: #fafafa;
  129. border-top: 1px solid #f0f0f0;
  130. border-left: 3px solid transparent;
  131. }
  132. .result-area.r-pass { border-left-color: #107c10; }
  133. .result-area.r-fail { border-left-color: #c50f1f; }
  134. .result-area.r-ok { border-left-color: #107c10; }
  135. .result-label { font-size: 11px; font-weight: 600; color: #797775; text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 6px; }
  136. .result-table { width: 100%; border-collapse: collapse; font-size: 12px; font-family: 'Cascadia Code', 'Consolas', monospace; margin-bottom: 8px; }
  137. .result-table td { padding: 2px 0; vertical-align: top; }
  138. .result-table td:first-child { color: #797775; font-weight: 600; width: 80px; padding-right: 12px; white-space: nowrap; }
  139. .stack-block {
  140. background: #f0f0f0;
  141. border-left: 3px solid #c0c0c0;
  142. padding: 7px 10px;
  143. font-family: 'Cascadia Code', 'Consolas', monospace;
  144. font-size: 11px;
  145. white-space: pre-wrap;
  146. word-break: break-all;
  147. line-height: 1.55;
  148. border-radius: 0 3px 3px 0;
  149. color: #323130;
  150. }
  151. .raw-block {
  152. background: #f0f0f0;
  153. border-left: 3px solid #e57373;
  154. padding: 7px 10px;
  155. font-family: 'Cascadia Code', 'Consolas', monospace;
  156. font-size: 11px;
  157. white-space: pre-wrap;
  158. word-break: break-all;
  159. max-height: 180px;
  160. overflow-y: auto;
  161. line-height: 1.55;
  162. border-radius: 0 3px 3px 0;
  163. }
  164. .resp-block {
  165. font-family: 'Cascadia Code', 'Consolas', monospace;
  166. font-size: 12px;
  167. color: #323130;
  168. word-break: break-all;
  169. }
  170. </style>
  171. </head>
  172. <body>
  173. <nav class="nav-bar">
  174. <span class="nav-brand">Unit Tester</span>
  175. <a class="nav-tab" href="index.html">AGI Scripts</a>
  176. <a class="nav-tab" href="backend.html">ao_backend</a>
  177. <a class="nav-tab" href="wstest.html">WebSocket</a>
  178. <a class="nav-tab active" href="devmode.html">Dev Mode</a>
  179. </nav>
  180. <div class="content">
  181. <div class="page-title">AGI Dev Mode Debug Test</div>
  182. <p class="page-subtitle">
  183. Tests the <code>AGI_DEV</code> global flag with <code>ao_module_agirun</code>.
  184. When enabled, the backend returns a JSON payload with the full Otto stack trace
  185. instead of a generic HTTP&nbsp;500.
  186. </p>
  187. <!-- Toggle -->
  188. <div class="toggle-row">
  189. <label class="toggle-switch">
  190. <input type="checkbox" id="devmodeCheckbox" checked onchange="onToggle(this)">
  191. <span class="toggle-track"></span>
  192. </label>
  193. <span class="toggle-label">AGI Dev Mode</span>
  194. <span class="toggle-hint" id="toggleHint">ON &mdash; errors return detailed JSON</span>
  195. </div>
  196. <!-- Toolbar -->
  197. <div class="toolbar">
  198. <button class="btn btn-primary" onclick="runAllTests()">&#9654;&nbsp; Run All</button>
  199. <button class="btn btn-secondary" onclick="clearAll()">Clear</button>
  200. </div>
  201. <!-- Test list -->
  202. <div class="test-list">
  203. <div class="test-list-header">
  204. <span style="width:22px;flex-shrink:0"></span>
  205. <span class="col-name">Test</span>
  206. <span class="col-status">Status</span>
  207. <span class="col-action"></span>
  208. </div>
  209. <div id="testContainer"></div>
  210. </div>
  211. </div>
  212. <script>
  213. var AGI_DEV = true;
  214. const TESTS = [
  215. {
  216. id: "runtime_error",
  217. name: "Runtime Reference Error",
  218. script: "UnitTest/devmode/runtime_error.js",
  219. desc: "Calls an undefined variable — triggers ReferenceError in Otto VM",
  220. expectError: true
  221. },
  222. {
  223. id: "throw_error",
  224. name: "Explicit throw new Error()",
  225. script: "UnitTest/devmode/throw_error.js",
  226. desc: "Throws through a nested call chain — exercises multi-frame stack trace",
  227. expectError: true
  228. },
  229. {
  230. id: "working_script",
  231. name: "Working Script",
  232. script: "UnitTest/devmode/working_script.js",
  233. desc: "Completes successfully — verifies dev mode does not affect normal execution",
  234. expectError: false
  235. }
  236. ];
  237. /* ── Build list ──────────────────────────────────────── */
  238. (function build() {
  239. let html = TESTS.map(function(t, i) {
  240. return `<div class="test-item" id="item_${t.id}">
  241. <span class="test-idx">${i + 1}</span>
  242. <div class="test-info">
  243. <div class="test-name">${escHtml(t.name)}</div>
  244. <div class="test-desc">${escHtml(t.desc)}</div>
  245. <div class="test-script">${escHtml(t.script)}</div>
  246. </div>
  247. <span class="status-badge s-pending" id="badge_${t.id}">
  248. <span class="status-dot"></span>Pending
  249. </span>
  250. <div class="col-action">
  251. <button class="btn-run" id="runbtn_${t.id}" onclick="runTest('${t.id}')">Run</button>
  252. </div>
  253. </div>
  254. <div class="result-area" id="result_${t.id}"></div>`;
  255. }).join("");
  256. document.getElementById("testContainer").innerHTML = html;
  257. })();
  258. /* ── Toggle ──────────────────────────────────────────── */
  259. function onToggle(cb) {
  260. AGI_DEV = cb.checked;
  261. document.getElementById("toggleHint").textContent = AGI_DEV
  262. ? "ON — errors return detailed JSON"
  263. : "OFF — errors return generic HTML 500";
  264. }
  265. /* ── Run ─────────────────────────────────────────────── */
  266. function runAllTests() {
  267. TESTS.forEach(function(t) { runTest(t.id); });
  268. }
  269. function clearAll() {
  270. TESTS.forEach(function(t) {
  271. setStatus(t.id, "pending");
  272. let r = document.getElementById("result_" + t.id);
  273. r.style.display = "none"; r.innerHTML = ""; r.className = "result-area";
  274. });
  275. }
  276. function runTest(id) {
  277. let test = TESTS.find(function(t) { return t.id === id; });
  278. let btn = document.getElementById("runbtn_" + id);
  279. setStatus(id, "running");
  280. if (btn) btn.disabled = true;
  281. ao_module_agirun(test.script, {}, function(data) {
  282. setStatus(id, "pass");
  283. let body = (typeof data === "string") ? data : JSON.stringify(data);
  284. showResult(id, "r-pass", renderSuccess(body));
  285. if (btn) btn.disabled = false;
  286. }, function(xhr) {
  287. setStatus(id, "fail");
  288. let html = AGI_DEV ? renderDevError(xhr) : renderNormalError(xhr);
  289. showResult(id, "r-fail", html);
  290. if (btn) btn.disabled = false;
  291. }, 10000);
  292. }
  293. /* ── Renderers ───────────────────────────────────────── */
  294. function renderSuccess(body) {
  295. return `<div class="result-label">Response (HTTP 200)</div>
  296. <div class="resp-block">${escHtml(body)}</div>`;
  297. }
  298. function renderDevError(xhr) {
  299. let e = {};
  300. try { e = JSON.parse(xhr.responseText); } catch(_) {}
  301. let msg = escHtml(e.message || "(no message)");
  302. let stack = escHtml(e.stacktrace || "(no stack trace)");
  303. let script = escHtml(e.script || "(unknown)");
  304. let user = escHtml(e.user || "(unknown)");
  305. let same = (e.message === e.stacktrace);
  306. return `<div class="result-label">Dev Mode Error (HTTP ${xhr.status || "?"})</div>
  307. <table class="result-table">
  308. <tr><td>Message</td><td>${msg}</td></tr>
  309. <tr><td>Script</td><td>${script}</td></tr>
  310. <tr><td>User</td><td>${user}</td></tr>
  311. </table>
  312. <div class="result-label" style="margin-top:8px">Stack Trace</div>
  313. <div class="stack-block">${same ? msg : stack}</div>`;
  314. }
  315. function renderNormalError(xhr) {
  316. let raw = escHtml(xhr.responseText || "(empty response body)");
  317. return `<div class="result-label">Normal Mode Error (HTTP ${xhr.status || "?"}) &mdash; no debug info</div>
  318. <div class="raw-block">${raw}</div>`;
  319. }
  320. /* ── Helpers ─────────────────────────────────────────── */
  321. function setStatus(id, state) {
  322. let badge = document.getElementById("badge_" + id);
  323. if (!badge) return;
  324. let labels = { pending:"Pending", running:"Running", pass:"Passed", fail:"Failed" };
  325. badge.className = "status-badge s-" + state;
  326. badge.innerHTML = '<span class="status-dot"></span>' + labels[state];
  327. }
  328. function showResult(id, cls, html) {
  329. let div = document.getElementById("result_" + id);
  330. if (!div) return;
  331. div.className = "result-area " + cls;
  332. div.innerHTML = html;
  333. div.style.display = "block";
  334. }
  335. function escHtml(s) {
  336. return String(s).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;");
  337. }
  338. </script>
  339. </body>
  340. </html>