Add local-mode workspace, ZIP import/export, and richer pin/ADC/serial sims

Boot:
- Editor now picks local vs server mode based on URL flag, sign-in
  state, and a stale local-mode flag. Signed-in users are no longer
  bounced to IndexedDB if they had previously clicked "Use locally".

Local mode:
- New LocalWorkspaceClient (src/static/local-workspace.js) with
  pluggable IndexedDB and File System Access backends. Picked folder
  handles persist across reloads with a Reconnect button when the
  permission lapses.
- Static-only host: scripts/serve_static_editor.py serves src/static/
  with COOP/COEP so SharedArrayBuffer-backed sims keep working.
- Bundled MicroPython stubs ship under src/static/bundled-lib/ for
  static hosting; FastAPI also exposes them at /api/public/lib-bundle.

Workspace import / export:
- Zero-dep ZIP encoder + reader (STORE + DEFLATE via
  DecompressionStream). Export/Import buttons in the workspace badge
  work in both local and server modes; imports are confined to code/.

Pin / ADC / Serial simulation:
- machine.py grows ADC, UART, expanded Pin, and PWM mocks, all driven
  by SharedArrayBuffer when cross-origin isolated and falling back to
  postMessage + [pin-out] stdout markers otherwise — pins, ADC slider,
  and serial input now keep working over plain HTTP / LAN-IP origins.
- NeoPixel pins are claimed via a [pin-claim] marker and dropped from
  the Pins panel so the data line doesn't flicker per write().
- New demos: adc_slider_demo.py, pin_demo.py, serial_demo.py.

Lib layout:
- Single source of truth at repo lib/; workspace/lib/ caching layer
  removed and the directory deleted. Filesystem service reads stubs
  directly from PROJECT_ROOT/lib.

UI:
- Home page slimmed to "Sign in" + "Use locally" with optional editor
  / manage-users links. Admin user/invite UI moved to /users.
- Workspace badge gains storage indicator, Folder…/Reconnect, Export,
  Import, and Exit controls.
- Mobile-friendly tweaks: safer-area padding, larger touch targets,
  iOS-zoom-proof serial input, file-tree highlight fix.

Tests:
- test_auth.py patches PROJECT_ROOT for the lib-shared test so the
  repo-root lib refactor stays green. test_api.py asserts the new
  "LED Editor" branding.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-10 06:16:02 +12:00
parent 9f28eabd2d
commit ca0ca6fe7e
26 changed files with 5080 additions and 793 deletions

View File

@@ -1,5 +1,4 @@
import importlib
import shutil
from pathlib import Path
import pytest
@@ -10,33 +9,24 @@ def test_root_serves_html(client):
response = client.get("/")
assert response.status_code == 200
assert "text/html" in response.headers["content-type"]
assert "Python Editor" in response.text
assert "LED Editor" in response.text
def test_editor_route_serves_editor_html(client):
response = client.get("/editor")
assert response.status_code == 200
assert "text/html" in response.headers["content-type"]
assert "Python Editor" in response.text
assert "LED Editor" in response.text
def test_workspace_lib_seeded_from_bundle_on_startup(client, tmp_path):
"""Populate WORKSPACE_ROOT/lib with shipped stubs when absent (empty volume / fresh tmp)."""
lib_dir = tmp_path / "lib"
assert (lib_dir / "machine.py").is_file()
assert (lib_dir / "neopixel.py").is_file()
assert len((lib_dir / "machine.py").read_text(encoding="utf-8")) > 0
def test_workspace_lib_repops_when_removed_after_startup(client, tmp_path):
"""Touching lib via the API restores bundled stubs after the workspace lib dir is wiped."""
lib_dir = tmp_path / "lib"
assert (lib_dir / "machine.py").is_file()
shutil.rmtree(lib_dir)
def test_lib_served_directly_from_repo_bundle(client, tmp_path):
"""`lib/` reads come straight from repo `lib/`; no workspace cache is created."""
resp = client.get("/api/file/lib/machine.py")
assert resp.status_code == 200
assert (lib_dir / "machine.py").is_file()
body = resp.json().get("content", "")
assert "class Pin" in body and len(body) > 0
assert not (tmp_path / "lib").exists()
def test_list_files_hides_dotfiles_and_reports_sizes(client, tmp_path):
(tmp_path / ".hidden.txt").write_text("secret", encoding="utf-8")
@@ -76,9 +66,6 @@ def test_save_file_collapses_duplicate_scoped_prefix(client, tmp_path):
def test_lib_folder_is_read_only_for_mutations(client, tmp_path):
lib_dir = tmp_path / "lib"
lib_dir.mkdir(exist_ok=True)
(lib_dir / "helper.py").write_text("x = 1\n", encoding="utf-8")
code_dir = tmp_path / "code"
code_dir.mkdir()
(code_dir / "main.py").write_text("print('ok')\n", encoding="utf-8")
@@ -86,7 +73,7 @@ def test_lib_folder_is_read_only_for_mutations(client, tmp_path):
save_blocked = client.post("/api/file/lib/new.txt", json={"content": "nope"})
assert save_blocked.status_code == 403
delete_blocked = client.delete("/api/file/lib/helper.py")
delete_blocked = client.delete("/api/file/lib/machine.py")
assert delete_blocked.status_code == 403
move_blocked = client.post(
@@ -252,14 +239,14 @@ def test_folder_delete_errors(client, tmp_path):
def test_workspace_py_sources_returns_python_files(client, tmp_path):
(tmp_path / "code").mkdir()
(tmp_path / "code" / "app.py").write_text("x = 1\n", encoding="utf-8")
(tmp_path / "lib").mkdir(exist_ok=True)
(tmp_path / "lib" / "util.py").write_text("def f():\n pass\n", encoding="utf-8")
response = client.get("/api/workspace/py-sources")
assert response.status_code == 200
files = response.json()["files"]
assert files["code/app.py"] == "x = 1\n"
assert "lib/util.py" in files
# Bundled stubs from repo `lib/` are merged in automatically.
assert "lib/machine.py" in files
assert "lib/neopixel.py" in files
def test_api_requires_bearer_when_editor_api_key_set(tmp_path, monkeypatch):
@@ -284,7 +271,9 @@ def test_api_requires_bearer_when_editor_api_key_set(tmp_path, monkeypatch):
assert ok.status_code == 200
def test_create_app_startup_creates_lib(tmp_path, monkeypatch):
def test_create_app_startup_does_not_seed_workspace_lib(tmp_path, monkeypatch):
"""Bundle stubs are read directly from repo `lib/`, so startup must not create
a `WORKSPACE_ROOT/lib` cache."""
import editor_app.config as config
import editor_app.db.session as db_sess
import editor_app.main as main
@@ -302,4 +291,4 @@ def test_create_app_startup_creates_lib(tmp_path, monkeypatch):
assert not (tmp_path / "lib").exists()
with TestClient(main.app) as _client:
_client.get("/api/auth/status")
assert (tmp_path / "lib").is_dir()
assert not (tmp_path / "lib").exists()

View File

@@ -18,7 +18,7 @@ def _reload_app(tmp_path, monkeypatch, **env):
monkeypatch.delenv("BOOTSTRAP_ADMIN_PASSWORD", raising=False)
for k, v in env.items():
monkeypatch.setenv(k, v)
config.WORKSPACE_ROOT = tmp_path
monkeypatch.setattr(config, "WORKSPACE_ROOT", tmp_path)
db_sess.reset_engine()
importlib.reload(main)
return main.app
@@ -304,9 +304,23 @@ def test_users_have_isolated_workspaces(tmp_path, monkeypatch):
def test_lib_is_shared_read_only_across_users(tmp_path, monkeypatch):
"""`lib/` lives at the repo root (single source of truth), not under WORKSPACE_ROOT,
so we patch `config.PROJECT_ROOT` to give the test its own isolated lib bundle."""
import editor_app.config as config
shared_lib = tmp_path / "lib"
shared_lib.mkdir(parents=True, exist_ok=True)
(shared_lib / "shared.py").write_text("VALUE = 42\n", encoding="utf-8")
# Mirror canonical demo files so `_seed_canonical_demos_into_code` still works.
real_demos = config.PROJECT_ROOT / "workspace" / "code"
fake_demos = tmp_path / "workspace" / "code"
fake_demos.mkdir(parents=True, exist_ok=True)
for fname in ("pattern_rainbow_demo.py", "pattern_twinkle_demo.py", "pattern_chase_demo.py"):
src = real_demos / fname
if src.is_file():
(fake_demos / fname).write_text(src.read_text(encoding="utf-8"), encoding="utf-8")
monkeypatch.setattr(config, "PROJECT_ROOT", tmp_path)
with TestClient(
_reload_app(tmp_path, monkeypatch, AUTH_ENABLED="true", AUTH_REGISTER_OPEN="true")