Files
notebook-snapshot-extension/snapshot/tests/test_routes_api.py
T
tao.chenandClaude cd1aba6698 dev: initial implementation of notebook-snapshot extension
Manual-commit notebook snapshot extension (JupyterLab 4):

Backend
- Storage adapter layer (Local + S3 via boto3, pip extra)
  - Dumb put/get/list/delete contract; key validation
  - S3Storage: lazy boto3 import, S3ConnectionError with friendly
    messages + connectivity check at startup
- SnapshotStore
  - Strip code outputs/execution_count, MD5 on cleaned JSON
  - Gzipped envelope (id/timestamp/name/description/hash/size/notebook)
  - Append-only manifest.json (rebuildable from version files)
  - SnapshotUnchangedError when new hash matches most recent version
- REST API (commit / list / content) under /snapshot/ namespace
- traitlets config (storage_type, local_root, s3_* + S3_* env fallback)

Frontend
- snapshot:commit command (toolbar button + command palette)
- Dialog for name + description
- SnapshotPanel (left sidebar timeline) + DiffWidget (main area)
- Cell diff: id-match first, LCS fallback; jsdiff for line diff
- Restore via model.fromJSON() + context.save() (no refresh)
- All AGENTS.md hard conventions enforced

Tests: 33 backend pytest passing (storage + store + routes)

Docs: AGENTS.md, design.md, README.md synced with implementation.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-21 17:33:55 +08:00

153 lines
4.4 KiB
Python

"""Integration tests for the snapshot REST API (commit / list / content)."""
import json
import pytest
SAMPLE_NB = {
"cells": [
{
"cell_type": "code",
"id": "c1",
"source": "print('hi')",
"outputs": [{"output_type": "stream", "text": "hi"}],
"execution_count": 1,
"metadata": {},
},
{
"cell_type": "markdown",
"id": "c2",
"source": "# title",
"metadata": {},
},
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5,
}
def _post_commit(jp_fetch, path: str, name: str, description: str = ""):
body = json.dumps(
{
"path": path,
"content": SAMPLE_NB,
"name": name,
"description": description,
}
).encode()
return jp_fetch(
"snapshot",
"notebook-version",
"commit",
method="POST",
body=body,
)
async def test_commit_creates_entry(jp_fetch):
response = await _post_commit(jp_fetch, "api_a.ipynb", "v1", "first")
assert response.code == 200
payload = json.loads(response.body)
assert payload["entry"]["name"] == "v1"
assert payload["entry"]["description"] == "first"
assert payload["entry"]["id"].startswith("v_")
assert payload["entry"]["size"] > 0
assert payload["entry"]["hash"]
async def test_list_contains_committed_entry(jp_fetch):
await _post_commit(jp_fetch, "api_b.ipynb", "v1", "desc")
response = await jp_fetch(
"snapshot",
"notebook-version",
"list",
params={"path": "api_b.ipynb"},
)
assert response.code == 200
payload = json.loads(response.body)
assert len(payload["versions"]) == 1
assert payload["versions"][0]["name"] == "v1"
assert payload["versions"][0]["description"] == "desc"
async def test_content_returns_stripped_notebook(jp_fetch):
commit_resp = await _post_commit(jp_fetch, "api_c.ipynb", "v1")
entry = json.loads(commit_resp.body)["entry"]
response = await jp_fetch(
"snapshot",
"notebook-version",
"content",
params={"path": "api_c.ipynb", "id": entry["id"]},
)
assert response.code == 200
notebook = json.loads(response.body)
code = next(c for c in notebook["cells"] if c["cell_type"] == "code")
assert code["outputs"] == []
assert code["execution_count"] is None
assert notebook["nbformat"] == 4
async def test_commit_missing_name_returns_400(jp_fetch):
body = json.dumps(
{"path": "api_d.ipynb", "content": SAMPLE_NB}
).encode()
response = await jp_fetch(
"snapshot",
"notebook-version",
"commit",
method="POST",
body=body,
raise_error=False,
)
assert response.code == 400
async def test_list_unknown_path_returns_empty(jp_fetch):
response = await jp_fetch(
"snapshot",
"notebook-version",
"list",
params={"path": "never_committed_unique.ipynb"},
)
assert response.code == 200
payload = json.loads(response.body)
assert payload["versions"] == []
async def test_commit_duplicate_returns_skipped(jp_fetch):
"""Second commit with identical content must NOT save; response is {skipped: True}."""
r1 = await _post_commit(jp_fetch, "api_dup.ipynb", "v1")
assert r1.code == 200
first = json.loads(r1.body)
assert first.get("entry") is not None
assert first.get("skipped") in (None, False)
r2 = await _post_commit(jp_fetch, "api_dup.ipynb", "v2")
assert r2.code == 200 # not an error — just a warning
second = json.loads(r2.body)
assert second.get("skipped") is True
assert second.get("reason") == "unchanged"
assert "未变更" in second.get("message", "") or "unchanged" in second.get("message", "").lower()
assert second.get("entry") is None
# And the manifest still has exactly one entry.
list_resp = await jp_fetch(
"snapshot",
"notebook-version",
"list",
params={"path": "api_dup.ipynb"},
)
assert json.loads(list_resp.body)["versions"].__len__() == 1
async def test_content_unknown_id_returns_404(jp_fetch):
response = await jp_fetch(
"snapshot",
"notebook-version",
"content",
params={"path": "api_e.ipynb", "id": "v_0_doesnotexist"},
raise_error=False,
)
assert response.code == 404