Fix bugs across app + server, optimize UI/UX, add Gitea CI
Some checks failed
CI / Test (Server, optional) (push) Failing after 2m10s
Windows Build / Build Windows (x64) (push) Failing after 29s
CI / Test (Flutter, Linux) (push) Has been cancelled
CI / Analyze (Flutter) (push) Has been cancelled

Bug fixes (Flutter):
- Wrap multi-statement DB writes (insert/update/delete note, deleteDocument,
  deletePageData, OCR FTS merge, migrations) in transactions to prevent data
  loss on interruption and a read-modify-write FTS race.
- Fix PdfDocument leaks on exception (try/finally dispose) and preserve image
  aspect ratio when stamping images onto PDF pages.
- Guard file-picker against empty selection (was .single -> crash).
- Fix eraser ConcurrentModificationError and unmodifiable-list crash on PDF
  pages; capture page synchronously on save to stop wrong-page data loss.
- Fix Riverpod DB-not-ready races, broken pull-to-refresh, settings load race,
  and search N+1; transform stored annotations on PDF page rotation.
- Normalize pen pressure for devices without a pressure range.
- PPT: single source of truth for slide strokes so ink displays and exports.

UI/UX:
- Material 3 typography, theme-aware colors (dark-mode fixes), hover cursors
  and right-click/visible actions on desktop, keyboard shortcuts (undo/redo/
  save/find), toolbar overflow handling, friendlier empty states, semantic OCR
  status badges, relative timestamps, 1-based page indicators, large-deck PPT
  navigation, and a scratchpad-scope label in split view.

Server (optional backend):
- Persist JWT secret (was per-process random), block path traversal in storage,
  fix CORS '*'+credentials, add OCR job ownership checks, last-writer-wins sync
  guard, constant-time login, and split out heavy OCR deps so the API/tests run
  without them.

CI: Gitea workflows for format+analyze+test (Linux, system sqlite) and a
Windows release build; pristine `flutter analyze`, all Flutter and server tests
green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-21 03:18:00 +08:00
commit 72428dc075
210 changed files with 18171 additions and 0 deletions

0
server/tests/__init__.py Normal file
View File

43
server/tests/conftest.py Normal file
View File

@@ -0,0 +1,43 @@
"""Shared test fixtures for BadNote tests."""
import os
import shutil
import sys
import tempfile
import pytest
import pytest_asyncio
from httpx import ASGITransport, AsyncClient
# Ensure the server package is importable
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
os.environ["BADNOTE_STORAGE_PATH"] = "/tmp/badnote_test_storage"
os.environ["BADNOTE_QUEUE_PATH"] = "/tmp/badnote_test_queue"
os.environ["BADNOTE_JWT_SECRET"] = "test-secret-key-for-testing-only"
from badnote_server.config import settings # noqa: E402
from badnote_server.main import app # noqa: E402
from badnote_server.database import get_db, close_db, init_db # noqa: E402
@pytest_asyncio.fixture(autouse=True)
async def setup_db(tmp_path):
"""Fresh DB and clean queue for each test."""
db_path = str(tmp_path / "test.db")
settings.db_path = db_path
# Clean the queue directory before each test
queue_path = settings.queue_path
if os.path.exists(queue_path):
shutil.rmtree(queue_path)
await init_db()
yield
await close_db()
@pytest_asyncio.fixture
async def client():
"""Async test client for the FastAPI app."""
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as ac:
yield ac

18
server/tests/helpers.py Normal file
View File

@@ -0,0 +1,18 @@
"""Shared test helpers for BadNote tests."""
from httpx import AsyncClient
async def register_and_login(client: AsyncClient) -> tuple[str, str]:
"""Helper: register a user and return (token, user_id)."""
resp = await client.post(
"/api/auth/register",
json={"username": "testuser", "password": "testpass123"},
)
data = resp.json()
return data["token"], data["user_id"]
def auth_header(token: str) -> dict:
"""Return Authorization header dict."""
return {"Authorization": f"Bearer {token}"}

82
server/tests/test_auth.py Normal file
View File

@@ -0,0 +1,82 @@
"""Tests for auth endpoints."""
import sys, os
sys.path.insert(0, os.path.dirname(__file__))
import pytest
import pytest_asyncio
from httpx import AsyncClient
from helpers import auth_header, register_and_login
@pytest.mark.asyncio
async def test_register(client: AsyncClient):
resp = await client.post(
"/api/auth/register",
json={"username": "newuser", "password": "password123"},
)
assert resp.status_code == 201
data = resp.json()
assert "token" in data
assert "user_id" in data
@pytest.mark.asyncio
async def test_register_duplicate(client: AsyncClient):
await client.post(
"/api/auth/register",
json={"username": "dupuser", "password": "password123"},
)
resp = await client.post(
"/api/auth/register",
json={"username": "dupuser", "password": "password123"},
)
assert resp.status_code == 409
@pytest.mark.asyncio
async def test_login(client: AsyncClient):
await client.post(
"/api/auth/register",
json={"username": "loginuser", "password": "mypassword"},
)
resp = await client.post(
"/api/auth/login",
json={"username": "loginuser", "password": "mypassword"},
)
assert resp.status_code == 200
data = resp.json()
assert "token" in data
assert "user_id" in data
@pytest.mark.asyncio
async def test_login_wrong_password(client: AsyncClient):
await client.post(
"/api/auth/register",
json={"username": "wrongpw", "password": "correct"},
)
resp = await client.post(
"/api/auth/login",
json={"username": "wrongpw", "password": "incorrect"},
)
assert resp.status_code == 401
@pytest.mark.asyncio
async def test_refresh(client: AsyncClient):
token, user_id = await register_and_login(client)
resp = await client.post(
"/api/auth/refresh",
headers=auth_header(token),
)
assert resp.status_code == 200
data = resp.json()
assert "token" in data
assert data["user_id"] == user_id
@pytest.mark.asyncio
async def test_refresh_no_token(client: AsyncClient):
resp = await client.post("/api/auth/refresh")
assert resp.status_code in (401, 403)

146
server/tests/test_notes.py Normal file
View File

@@ -0,0 +1,146 @@
"""Tests for notes CRUD and sync endpoints."""
import sys, os
sys.path.insert(0, os.path.dirname(__file__))
import pytest
import pytest_asyncio
from httpx import AsyncClient
from helpers import auth_header, register_and_login
@pytest.mark.asyncio
async def test_create_note(client: AsyncClient):
token, _ = await register_and_login(client)
resp = await client.post(
"/api/notes",
json={
"id": "note-001",
"title": "Test Note",
"tags": ["tag1", "tag2"],
"strokes_json": "[{\"x\":1,\"y\":2}]",
},
headers=auth_header(token),
)
assert resp.status_code == 201
data = resp.json()
assert data["id"] == "note-001"
assert data["title"] == "Test Note"
assert data["tags"] == ["tag1", "tag2"]
@pytest.mark.asyncio
async def test_get_note(client: AsyncClient):
token, _ = await register_and_login(client)
await client.post(
"/api/notes",
json={"id": "note-002", "title": "Fetch Me", "tags": [], "strokes_json": "[]"},
headers=auth_header(token),
)
resp = await client.get("/api/notes/note-002", headers=auth_header(token))
assert resp.status_code == 200
assert resp.json()["title"] == "Fetch Me"
@pytest.mark.asyncio
async def test_get_note_not_found(client: AsyncClient):
token, _ = await register_and_login(client)
resp = await client.get("/api/notes/nonexistent", headers=auth_header(token))
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_update_note(client: AsyncClient):
token, _ = await register_and_login(client)
await client.post(
"/api/notes",
json={"id": "note-003", "title": "Original", "tags": [], "strokes_json": "[]"},
headers=auth_header(token),
)
resp = await client.post(
"/api/notes",
json={"id": "note-003", "title": "Updated", "tags": ["new"], "strokes_json": "[1]"},
headers=auth_header(token),
)
assert resp.status_code == 201
assert resp.json()["title"] == "Updated"
assert resp.json()["tags"] == ["new"]
@pytest.mark.asyncio
async def test_delete_note(client: AsyncClient):
token, _ = await register_and_login(client)
await client.post(
"/api/notes",
json={"id": "note-004", "title": "Delete Me", "tags": [], "strokes_json": "[]"},
headers=auth_header(token),
)
resp = await client.delete("/api/notes/note-004", headers=auth_header(token))
assert resp.status_code == 200
resp = await client.get("/api/notes/note-004", headers=auth_header(token))
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_list_notes_with_since(client: AsyncClient):
token, _ = await register_and_login(client)
await client.post(
"/api/notes",
json={"id": "note-005", "title": "Old", "tags": [], "strokes_json": "[]"},
headers=auth_header(token),
)
resp = await client.get("/api/notes", headers=auth_header(token))
assert resp.status_code == 200
assert len(resp.json()) >= 1
resp = await client.get(
"/api/notes?since=2099-01-01T00:00:00+00:00",
headers=auth_header(token),
)
assert resp.status_code == 200
assert len(resp.json()) == 0
@pytest.mark.asyncio
async def test_notes_require_auth(client: AsyncClient):
resp = await client.get("/api/notes")
assert resp.status_code in (401, 403)
# ── Sync ────────────────────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_sync_push(client: AsyncClient):
token, _ = await register_and_login(client)
resp = await client.post(
"/api/sync/push",
json={
"notes": [
{"id": "sync-1", "title": "Synced", "tags": [], "strokes_json": "[]", "updated_at": "2024-01-01T00:00:00+00:00"},
{"id": "sync-2", "title": "Also", "tags": ["t"], "strokes_json": "[]", "updated_at": "2024-01-02T00:00:00+00:00"},
]
},
headers=auth_header(token),
)
assert resp.status_code == 200
assert resp.json()["synced_count"] == 2
@pytest.mark.asyncio
async def test_sync_pull(client: AsyncClient):
token, _ = await register_and_login(client)
await client.post(
"/api/sync/push",
json={"notes": [{"id": "pull-1", "title": "Pull Me", "tags": [], "strokes_json": "[]", "updated_at": "2024-06-01T00:00:00+00:00"}]},
headers=auth_header(token),
)
resp = await client.post(
"/api/sync/pull",
json={"since": "2024-01-01T00:00:00+00:00"},
headers=auth_header(token),
)
assert resp.status_code == 200
notes = resp.json()["notes"]
assert len(notes) >= 1
assert any(n["id"] == "pull-1" for n in notes)

82
server/tests/test_ocr.py Normal file
View File

@@ -0,0 +1,82 @@
"""Tests for OCR endpoints."""
import sys, os
sys.path.insert(0, os.path.dirname(__file__))
import pytest
import pytest_asyncio
from httpx import AsyncClient
from helpers import auth_header, register_and_login
@pytest.mark.asyncio
async def test_submit_ocr_job(client: AsyncClient):
token, _ = await register_and_login(client)
resp = await client.post(
"/api/ocr/process",
json={"note_id": "note-ocr-1", "document_id": None, "page_number": None},
headers=auth_header(token),
)
assert resp.status_code == 201
data = resp.json()
assert "job_id" in data
@pytest.mark.asyncio
async def test_get_job_status(client: AsyncClient):
token, _ = await register_and_login(client)
resp = await client.post(
"/api/ocr/process",
json={"note_id": "note-ocr-2", "document_id": None, "page_number": None},
headers=auth_header(token),
)
job_id = resp.json()["job_id"]
resp = await client.get(f"/api/ocr/status/{job_id}", headers=auth_header(token))
assert resp.status_code == 200
data = resp.json()
assert data["id"] == job_id
assert data["status"] == "pending"
@pytest.mark.asyncio
async def test_get_job_status_not_found(client: AsyncClient):
token, _ = await register_and_login(client)
resp = await client.get("/api/ocr/status/nonexistent", headers=auth_header(token))
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_get_ocr_results_empty(client: AsyncClient):
token, _ = await register_and_login(client)
resp = await client.get("/api/ocr/results/note-no-jobs", headers=auth_header(token))
assert resp.status_code == 200
assert resp.json() == []
@pytest.mark.asyncio
async def test_get_ocr_results_with_jobs(client: AsyncClient):
token, _ = await register_and_login(client)
await client.post(
"/api/ocr/process",
json={"note_id": "note-ocr-3", "document_id": None, "page_number": None},
headers=auth_header(token),
)
await client.post(
"/api/ocr/process",
json={"note_id": "note-ocr-3", "document_id": None, "page_number": None},
headers=auth_header(token),
)
resp = await client.get("/api/ocr/results/note-ocr-3", headers=auth_header(token))
assert resp.status_code == 200
assert len(resp.json()) == 2
@pytest.mark.asyncio
async def test_ocr_requires_auth(client: AsyncClient):
resp = await client.post(
"/api/ocr/process",
json={"note_id": "x", "document_id": None, "page_number": None},
)
assert resp.status_code in (401, 403)