Your Name commited on
Commit ยท
109c542
1
Parent(s): f14ef69
feat: implement class management functionality and related tests
Browse files- static/math.css +5 -0
- static/math.js +48 -2
- tests/test_phone_browser.py +63 -0
static/math.css
CHANGED
|
@@ -152,3 +152,8 @@ html.math-on, html.math-on body{overflow:hidden}
|
|
| 152 |
.mx-scan-overlay{position:fixed;inset:0;z-index:1000;background:#000c;overflow:auto;padding:20px;display:flex;align-items:flex-start;justify-content:center}
|
| 153 |
.mx-scan-card{background:#141719;color:#f4f2ec;border:1px solid #485050;border-radius:18px;padding:24px;width:min(520px,100%);display:flex;flex-direction:column;gap:14px;line-height:1.6}
|
| 154 |
.mx-scan-card p{margin:0}.mx-scan-card [hidden]{display:none}.mx-scan-card small{color:#adb7b1}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
.mx-scan-overlay{position:fixed;inset:0;z-index:1000;background:#000c;overflow:auto;padding:20px;display:flex;align-items:flex-start;justify-content:center}
|
| 153 |
.mx-scan-card{background:#141719;color:#f4f2ec;border:1px solid #485050;border-radius:18px;padding:24px;width:min(520px,100%);display:flex;flex-direction:column;gap:14px;line-height:1.6}
|
| 154 |
.mx-scan-card p{margin:0}.mx-scan-card [hidden]{display:none}.mx-scan-card small{color:#adb7b1}
|
| 155 |
+
.mx-management,.mx-class-editor form{display:flex;flex-direction:column;gap:16px}
|
| 156 |
+
.mx-management select{display:block;width:100%;margin-top:8px;padding:10px;border:1px solid var(--line);border-radius:8px;background:var(--bg);color:var(--ink);font:inherit}
|
| 157 |
+
.mx-management fieldset{border:1px solid var(--line);border-radius:12px;padding:14px;display:grid;gap:12px}
|
| 158 |
+
.mx-management input{accent-color:var(--mx-accent)}
|
| 159 |
+
.mx-management button:disabled{opacity:.6;cursor:wait}
|
static/math.js
CHANGED
|
@@ -10,7 +10,7 @@ document.head.append(css);
|
|
| 10 |
|
| 11 |
const CFG=window.MATHLY||{}, AUTH_URL=CFG.supabaseUrl||"";
|
| 12 |
let ME_EMAIL="", MY_CLASSES=[], MY_CLASS=null, client=null;
|
| 13 |
-
const can=permission=>CFG.permissions.includes("*")||CFG.permissions.includes(permission);
|
| 14 |
const api=(url,options={})=>fetch(url,{...options,headers:{...(options.headers||{}),...(CFG.token?{Authorization:`Bearer ${CFG.token}`}:{})}});
|
| 15 |
|
| 16 |
const esc=s=>String(s??"").replace(/[&<>"']/g,c=>({"&":"&","<":"<",">":">",'"':""","'":"'"}[c]));
|
|
@@ -79,7 +79,7 @@ function draw(){
|
|
| 79 |
const UNITS=G.units.filter(u=>SHOWN.has(u.id)), mine=G.skills.filter(s=>SHOWN.has(s.unit));
|
| 80 |
const total=mine.length, doneN=mine.filter(s=>P.done[s.id]).length, nxt=next();
|
| 81 |
const cls=typeof MY_CLASS!=="undefined"&&MY_CLASS?` ยท ืืืชืช ${esc(MY_CLASS.name)}`:"";
|
| 82 |
-
const manage=
|
| 83 |
const units=UNITS.map(u=>{
|
| 84 |
const ids=ORDER.filter(id=>BY[id].unit===u.id), d=ids.filter(id=>P.done[id]).length;
|
| 85 |
return `<div class="mx-su u-${u.id}"><b>${esc(u.name)}</b><span class="mx-bar"><i style="width:${Math.round(100*d/ids.length)}%"></i></span><small>${d} ืืชืื ${ids.length}</small></div>`;
|
|
@@ -173,6 +173,7 @@ function onClick(e){
|
|
| 173 |
if(t.dataset.choice!=null) return answer(t);
|
| 174 |
if(t.dataset.id) return open(t.dataset.id);
|
| 175 |
switch(t.dataset.act){
|
|
|
|
| 176 |
case "phone": return pairPhone();
|
| 177 |
case "copy-phone": return copyPhone(t);
|
| 178 |
case "photo": case "retake": return picker.click();
|
|
@@ -184,6 +185,51 @@ function onClick(e){
|
|
| 184 |
}
|
| 185 |
}
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
/* A photographed problem (/api/math/photo, app/math_photo.py): hints first. The sheet shows the problem as it was
|
| 188 |
read (a wrong reading is the student's cue to photograph again), then one hint; each further hint, each step and
|
| 189 |
the whole solution with its answer come only when the student asks for them. */
|
|
|
|
| 10 |
|
| 11 |
const CFG=window.MATHLY||{}, AUTH_URL=CFG.supabaseUrl||"";
|
| 12 |
let ME_EMAIL="", MY_CLASSES=[], MY_CLASS=null, client=null;
|
| 13 |
+
const can=permission=>(CFG.permissions||[]).includes("*")||(CFG.permissions||[]).includes(permission);
|
| 14 |
const api=(url,options={})=>fetch(url,{...options,headers:{...(options.headers||{}),...(CFG.token?{Authorization:`Bearer ${CFG.token}`}:{})}});
|
| 15 |
|
| 16 |
const esc=s=>String(s??"").replace(/[&<>"']/g,c=>({"&":"&","<":"<",">":">",'"':""","'":"'"}[c]));
|
|
|
|
| 79 |
const UNITS=G.units.filter(u=>SHOWN.has(u.id)), mine=G.skills.filter(s=>SHOWN.has(s.unit));
|
| 80 |
const total=mine.length, doneN=mine.filter(s=>P.done[s.id]).length, nxt=next();
|
| 81 |
const cls=typeof MY_CLASS!=="undefined"&&MY_CLASS?` ยท ืืืชืช ${esc(MY_CLASS.name)}`:"";
|
| 82 |
+
const manage=can("management.access"), pct=Math.round(100*doneN/total);
|
| 83 |
const units=UNITS.map(u=>{
|
| 84 |
const ids=ORDER.filter(id=>BY[id].unit===u.id), d=ids.filter(id=>P.done[id]).length;
|
| 85 |
return `<div class="mx-su u-${u.id}"><b>${esc(u.name)}</b><span class="mx-bar"><i style="width:${Math.round(100*d/ids.length)}%"></i></span><small>${d} ืืชืื ${ids.length}</small></div>`;
|
|
|
|
| 173 |
if(t.dataset.choice!=null) return answer(t);
|
| 174 |
if(t.dataset.id) return open(t.dataset.id);
|
| 175 |
switch(t.dataset.act){
|
| 176 |
+
case "manage": return manageClasses();
|
| 177 |
case "phone": return pairPhone();
|
| 178 |
case "copy-phone": return copyPhone(t);
|
| 179 |
case "photo": case "retake": return picker.click();
|
|
|
|
| 185 |
}
|
| 186 |
}
|
| 187 |
|
| 188 |
+
async function manageClasses(){
|
| 189 |
+
if(!can("management.access")) return;
|
| 190 |
+
closeSheet(); back=document.activeElement; cur="management";
|
| 191 |
+
sheet.innerHTML=`<div class="mx-scrim" data-act="close"></div>
|
| 192 |
+
<div class="mx-sheet" role="dialog" aria-modal="true" aria-labelledby="mxTitle">
|
| 193 |
+
<div class="mx-shtop"><h3 id="mxTitle">ื ืืืื ืืืชืืช</h3><button class="mx-x" data-act="close" aria-label="ืกืืืจื">โ</button></div>
|
| 194 |
+
<div class="mx-management"><p role="status">ืืืขื ืื ืืช ืืืืชืืชโฆ</p></div></div>`;
|
| 195 |
+
sheet.hidden=false; sheet.querySelector(".mx-x").focus();
|
| 196 |
+
const box=sheet.querySelector(".mx-management");
|
| 197 |
+
try{
|
| 198 |
+
const response=await api("/api/classes");
|
| 199 |
+
if(!response.ok) throw new Error("ืื ืืฆืืื ื ืืืขืื ืืช ืืืืชืืช. ื ืกื ืฉืื.");
|
| 200 |
+
const {classes}=await response.json();
|
| 201 |
+
if(!box.isConnected) return;
|
| 202 |
+
if(!classes.length){box.innerHTML='<p>ืืื ืืืชืืช ืื ืืืื.</p>';return;}
|
| 203 |
+
box.innerHTML=`<label>ืืืชื<select class="mx-class-picker">${classes.map(c=>`<option value="${esc(c.id)}">${esc(c.name)}</option>`).join("")}</select></label><div class="mx-class-editor"></div>`;
|
| 204 |
+
const select=box.querySelector("select"), editor=box.querySelector(".mx-class-editor");
|
| 205 |
+
if(MY_CLASS&&classes.some(c=>c.id===MY_CLASS.id)) select.value=String(MY_CLASS.id);
|
| 206 |
+
function edit(){
|
| 207 |
+
const row=classes.find(c=>String(c.id)===select.value);
|
| 208 |
+
editor.innerHTML=`<form><fieldset><legend>ืืืืืืช ืืืืืื ืืืืชื</legend>${G.units.map(u=>`<label><input type="checkbox" name="unit" value="${esc(u.id)}"${row.lessons.includes(u.id)?" checked":""}> ${esc(u.name)}</label>`).join("")}</fieldset>
|
| 209 |
+
<button class="mx-btn solid" type="submit">ืฉืืืจืช ืืฉืื ืืืื</button><p role="status" aria-live="polite"></p></form>`;
|
| 210 |
+
const form=editor.querySelector("form"), message=form.querySelector('[role="status"]'), submit=form.querySelector('button');
|
| 211 |
+
form.onsubmit=async event=>{
|
| 212 |
+
event.preventDefault();
|
| 213 |
+
const lessons=[...form.querySelectorAll('input:checked')].map(input=>input.value);
|
| 214 |
+
if(!lessons.length){message.textContent="ืืืจื ืืคืืืช ืืืืืช ืืืืื ืืืช.";return;}
|
| 215 |
+
submit.disabled=true; select.disabled=true; message.textContent="ืฉืืืจืืโฆ";
|
| 216 |
+
try{
|
| 217 |
+
const saved=await api(`/api/classes/${encodeURIComponent(row.id)}`,{method:"PUT",headers:{"Content-Type":"application/json"},
|
| 218 |
+
body:JSON.stringify({name:row.name,emails:row.emails||[],teachers:row.teachers||[],lessons})});
|
| 219 |
+
if(!saved.ok) throw new Error("ืื ืืฆืืื ื ืืฉืืืจ ืืช ืืฉืื ืืืื. ื ืกื ืฉืื.");
|
| 220 |
+
row.lessons=lessons;
|
| 221 |
+
MY_CLASSES.forEach(c=>{if(c.id===row.id)c.lessons=lessons;});
|
| 222 |
+
if(MY_CLASS?.id===row.id) MY_CLASS.lessons=lessons;
|
| 223 |
+
SHOWN=new Set(shownUnits().map(u=>u.id)); draw();
|
| 224 |
+
message.textContent="ืืฉืื ืืืื ื ืฉืืจื. ืืคืฉืจ ืืืืืจ ืืืกืืื.";
|
| 225 |
+
}catch(error){message.textContent=error.message;}
|
| 226 |
+
finally{submit.disabled=false;select.disabled=false;}
|
| 227 |
+
};
|
| 228 |
+
}
|
| 229 |
+
select.onchange=edit; edit();
|
| 230 |
+
}catch(error){box.textContent=error.message;}
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
/* A photographed problem (/api/math/photo, app/math_photo.py): hints first. The sheet shows the problem as it was
|
| 234 |
read (a wrong reading is the student's cue to photograph again), then one hint; each further hint, each step and
|
| 235 |
the whole solution with its answer come only when the student asks for them. */
|
tests/test_phone_browser.py
CHANGED
|
@@ -96,3 +96,66 @@ def test_phone_refresh_upload_and_desktop_network_recovery(phone_server):
|
|
| 96 |
assert len(polls) >= 2
|
| 97 |
finally:
|
| 98 |
browser.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
assert len(polls) >= 2
|
| 97 |
finally:
|
| 98 |
browser.close()
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
@pytest.mark.parametrize("manager", [True, False])
|
| 102 |
+
def test_class_management_geometry_and_permissions(phone_server, manager):
|
| 103 |
+
import json
|
| 104 |
+
|
| 105 |
+
row = {"id": 7, "name": "ืืืชืช ืชืจืืื", "lessons": ["f"],
|
| 106 |
+
"emails": ["student@example.com"], "teachers": ["teacher@example.com"]}
|
| 107 |
+
writes = []
|
| 108 |
+
with playwright.sync_playwright() as p:
|
| 109 |
+
browser = p.chromium.launch()
|
| 110 |
+
try:
|
| 111 |
+
page = browser.new_page()
|
| 112 |
+
page.route("https://cdn.jsdelivr.net/**", lambda route: route.abort())
|
| 113 |
+
page.add_init_script('''window.supabase={createClient:()=>({auth:{
|
| 114 |
+
getSession:async()=>({data:{session:{access_token:"test",user:{id:"test"}}}}),
|
| 115 |
+
onAuthStateChange:()=>{},signOut:async()=>{}
|
| 116 |
+
}})};''')
|
| 117 |
+
|
| 118 |
+
def index(route):
|
| 119 |
+
response = route.fetch()
|
| 120 |
+
route.fulfill(response=response, body=response.text().replace(
|
| 121 |
+
'supabaseUrl:""', 'supabaseUrl:"https://auth.invalid"'))
|
| 122 |
+
|
| 123 |
+
page.route(phone_server + "/", index)
|
| 124 |
+
page.route("**/api/me", lambda route: route.fulfill(json={
|
| 125 |
+
"email": "student@example.com", "user_id": "test", "answer_scans": False,
|
| 126 |
+
"permissions": ["management.access", "classes.manage"] if manager else ["path.use"],
|
| 127 |
+
"classes": [row]}))
|
| 128 |
+
page.route("**/api/classes", lambda route: route.fulfill(json={"classes": [row]}))
|
| 129 |
+
|
| 130 |
+
def save(route):
|
| 131 |
+
body = json.loads(route.request.post_data)
|
| 132 |
+
writes.append(body)
|
| 133 |
+
if len(writes) == 1:
|
| 134 |
+
route.fulfill(status=503, json={"detail": "temporary failure"})
|
| 135 |
+
else:
|
| 136 |
+
row.update(body)
|
| 137 |
+
route.fulfill(json={"ok": True})
|
| 138 |
+
|
| 139 |
+
page.route("**/api/classes/7", save)
|
| 140 |
+
page.goto(phone_server)
|
| 141 |
+
button = page.locator('[data-act="manage"]')
|
| 142 |
+
if not manager:
|
| 143 |
+
playwright.expect(page.locator('.mx-top')).to_be_visible()
|
| 144 |
+
playwright.expect(button).to_have_count(0)
|
| 145 |
+
return
|
| 146 |
+
button.click()
|
| 147 |
+
geometry = page.locator('input[name="unit"][value="g"]')
|
| 148 |
+
geometry.check()
|
| 149 |
+
page.get_by_role("button", name="ืฉืืืจืช ืืฉืื ืืืื").click()
|
| 150 |
+
playwright.expect(page.get_by_role("status")).to_contain_text("ืื ืืฆืืื ื ืืฉืืืจ")
|
| 151 |
+
playwright.expect(geometry).to_be_checked()
|
| 152 |
+
page.get_by_role("button", name="ืฉืืืจืช ืืฉืื ืืืื").click()
|
| 153 |
+
playwright.expect(page.get_by_role("status")).to_contain_text("ืืฉืื ืืืื ื ืฉืืจื")
|
| 154 |
+
assert writes[-1] == {"name": row["name"], "emails": row["emails"],
|
| 155 |
+
"teachers": row["teachers"], "lessons": ["f", "g"]}
|
| 156 |
+
page.locator('.mx-x').click()
|
| 157 |
+
playwright.expect(page.locator('[data-id="g_triangle_area"]')).to_be_visible()
|
| 158 |
+
page.reload()
|
| 159 |
+
playwright.expect(page.locator('[data-id="g_triangle_area"]')).to_be_visible()
|
| 160 |
+
finally:
|
| 161 |
+
browser.close()
|