|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from typing import TYPE_CHECKING |
| 3 | +from typing import TYPE_CHECKING, get_type_hints |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
|
9 | 9 | from marimo._islands._island_generator import ( |
10 | 10 | MarimoIslandGenerator, |
11 | 11 | ) |
| 12 | +from marimo._schemas.serialization import ( |
| 13 | + AppInstantiation, |
| 14 | + CellDef, |
| 15 | + NotebookSerialization, |
| 16 | +) |
12 | 17 | from tests.mocks import snapshotter |
13 | 18 |
|
14 | 19 | if TYPE_CHECKING: |
|
17 | 22 | snapshot = snapshotter(__file__) |
18 | 23 |
|
19 | 24 |
|
| 25 | +def _notebook( |
| 26 | + cells: list[CellDef], |
| 27 | + *, |
| 28 | + options: dict[str, object] | None = None, |
| 29 | + filename: str | None = None, |
| 30 | +) -> NotebookSerialization: |
| 31 | + return NotebookSerialization( |
| 32 | + app=AppInstantiation(options=options or {}), |
| 33 | + cells=cells, |
| 34 | + filename=filename, |
| 35 | + ) |
| 36 | + |
| 37 | + |
20 | 38 | def test_add_code(): |
21 | 39 | generator = MarimoIslandGenerator() |
22 | 40 | generator.add_code("print('Hello, World!')") |
@@ -243,6 +261,235 @@ def __(mo): |
243 | 261 | assert str(other_dir / "nb.py") not in data |
244 | 262 |
|
245 | 263 |
|
| 264 | +async def test_from_ir_builds_notebook_serialization() -> None: |
| 265 | + notebook = _notebook( |
| 266 | + [ |
| 267 | + CellDef(code="import marimo as mo"), |
| 268 | + CellDef(code='mo.md("# Hello, IR!")'), |
| 269 | + ], |
| 270 | + ) |
| 271 | + |
| 272 | + generator = MarimoIslandGenerator._from_ir(notebook, app_id="page-a") |
| 273 | + stubs = generator.stubs |
| 274 | + |
| 275 | + assert len(stubs) == 2 |
| 276 | + assert stubs[0].code == "import marimo as mo" |
| 277 | + assert stubs[1].code == 'mo.md("# Hello, IR!")' |
| 278 | + body_html = generator.render_body(include_init_island=False) |
| 279 | + assert 'data-app-id="page-a"' in body_html |
| 280 | + for cell_id in generator._app.cell_manager.cell_ids(): |
| 281 | + assert f'data-cell-id="{cell_id}"' in body_html |
| 282 | + |
| 283 | + await generator.build() |
| 284 | + |
| 285 | + stub = generator.stubs[1] |
| 286 | + assert stub.output is not None |
| 287 | + assert "Hello, IR!" in stub.output.data |
| 288 | + |
| 289 | + |
| 290 | +def test_stubs_returns_read_only_snapshot() -> None: |
| 291 | + generator = MarimoIslandGenerator() |
| 292 | + generator.add_code("x = 1") |
| 293 | + |
| 294 | + stubs = generator.stubs |
| 295 | + assert len(stubs) == 1 |
| 296 | + assert stubs[0].code == "x = 1" |
| 297 | + |
| 298 | + generator.add_code("y = 2") |
| 299 | + assert len(stubs) == 1 |
| 300 | + assert len(generator.stubs) == 2 |
| 301 | + |
| 302 | + |
| 303 | +def test_from_ir_type_hints_resolve() -> None: |
| 304 | + hints = get_type_hints(MarimoIslandGenerator._from_ir) |
| 305 | + |
| 306 | + assert hints["notebook"] is NotebookSerialization |
| 307 | + assert hints["return"] is MarimoIslandGenerator |
| 308 | + |
| 309 | + |
| 310 | +def test_from_ir_applies_display_code_to_stubs() -> None: |
| 311 | + notebook = _notebook([CellDef(code="x = 1")]) |
| 312 | + |
| 313 | + generator = MarimoIslandGenerator._from_ir( |
| 314 | + notebook, |
| 315 | + display_code=True, |
| 316 | + ) |
| 317 | + |
| 318 | + html = generator.stubs[0].render() |
| 319 | + assert "<marimo-cell-code hidden>" not in html |
| 320 | + assert "x = 1" in html |
| 321 | + |
| 322 | + |
| 323 | +def test_from_ir_preserves_app_config() -> None: |
| 324 | + notebook = _notebook( |
| 325 | + [], |
| 326 | + options={"width": "medium", "app_title": "IR App"}, |
| 327 | + ) |
| 328 | + |
| 329 | + generator = MarimoIslandGenerator._from_ir(notebook) |
| 330 | + |
| 331 | + body_html = generator.render_body() |
| 332 | + assert 'style="margin: auto; max-width: 1110px;"' in body_html |
| 333 | + |
| 334 | + html = generator.render_html() |
| 335 | + assert "<title> IR App </title>" in html |
| 336 | + |
| 337 | + |
| 338 | +def test_from_ir_sanitizes_markdown_app_config() -> None: |
| 339 | + notebook = _notebook( |
| 340 | + [], |
| 341 | + options={"width": "medium", "author": "Marimo"}, |
| 342 | + filename="notebook.md", |
| 343 | + ) |
| 344 | + |
| 345 | + generator = MarimoIslandGenerator._from_ir(notebook) |
| 346 | + |
| 347 | + body_html = generator.render_body() |
| 348 | + assert 'style="margin: auto; max-width: 1110px;"' in body_html |
| 349 | + |
| 350 | + |
| 351 | +async def test_from_ir_propagates_ir_filename_to_cells(tmp_path: Path) -> None: |
| 352 | + notebook_file = tmp_path / "nb.py" |
| 353 | + notebook_file.write_text("") |
| 354 | + notebook = _notebook( |
| 355 | + [ |
| 356 | + CellDef(code="import marimo as mo"), |
| 357 | + CellDef( |
| 358 | + code=('mo.md(f"FILE={__file__} | DIR={mo.notebook_dir()}")') |
| 359 | + ), |
| 360 | + ], |
| 361 | + filename=str(notebook_file), |
| 362 | + ) |
| 363 | + |
| 364 | + generator = MarimoIslandGenerator._from_ir(notebook) |
| 365 | + await generator.build() |
| 366 | + |
| 367 | + captured = generator.stubs[1].output |
| 368 | + assert captured is not None |
| 369 | + data = captured.data |
| 370 | + assert str(notebook_file) in data, ( |
| 371 | + f"expected __file__ to resolve to {notebook_file}, got: {data}" |
| 372 | + ) |
| 373 | + assert str(notebook_file.parent) in data, ( |
| 374 | + f"expected notebook_dir() to resolve to {notebook_file.parent}, " |
| 375 | + f"got: {data}" |
| 376 | + ) |
| 377 | + |
| 378 | + |
| 379 | +async def test_from_ir_resolves_relative_path_at_capture_time( |
| 380 | + tmp_path: Path, |
| 381 | +) -> None: |
| 382 | + import os |
| 383 | + |
| 384 | + notebook_dir = tmp_path / "notebooks" |
| 385 | + notebook_dir.mkdir() |
| 386 | + notebook_file = notebook_dir / "nb.py" |
| 387 | + notebook_file.write_text("") |
| 388 | + notebook = _notebook( |
| 389 | + [ |
| 390 | + CellDef(code="import marimo as mo"), |
| 391 | + CellDef(code='mo.md(f"FILE={__file__}")'), |
| 392 | + ], |
| 393 | + ) |
| 394 | + other_dir = tmp_path / "elsewhere" |
| 395 | + other_dir.mkdir() |
| 396 | + |
| 397 | + original_cwd = os.getcwd() |
| 398 | + try: |
| 399 | + os.chdir(notebook_dir) |
| 400 | + generator = MarimoIslandGenerator._from_ir(notebook, filepath="nb.py") |
| 401 | + os.chdir(other_dir) |
| 402 | + await generator.build() |
| 403 | + finally: |
| 404 | + os.chdir(original_cwd) |
| 405 | + |
| 406 | + captured = generator.stubs[1].output |
| 407 | + assert captured is not None |
| 408 | + data = captured.data |
| 409 | + assert str(notebook_file) in data, ( |
| 410 | + f"expected __file__ to resolve to {notebook_file}, got: {data}" |
| 411 | + ) |
| 412 | + assert str(other_dir / "nb.py") not in data |
| 413 | + |
| 414 | + |
| 415 | +async def test_from_ir_filepath_overrides_ir_filename( |
| 416 | + tmp_path: Path, |
| 417 | +) -> None: |
| 418 | + ir_file = tmp_path / "ir.py" |
| 419 | + source_file = tmp_path / "source.ipynb" |
| 420 | + ir_file.write_text("") |
| 421 | + source_file.write_text("") |
| 422 | + notebook = _notebook( |
| 423 | + [ |
| 424 | + CellDef(code="import marimo as mo"), |
| 425 | + CellDef( |
| 426 | + code=('mo.md(f"FILE={__file__} | DIR={mo.notebook_dir()}")') |
| 427 | + ), |
| 428 | + ], |
| 429 | + filename=str(ir_file), |
| 430 | + ) |
| 431 | + |
| 432 | + generator = MarimoIslandGenerator._from_ir( |
| 433 | + notebook, filepath=str(source_file) |
| 434 | + ) |
| 435 | + await generator.build() |
| 436 | + |
| 437 | + captured = generator.stubs[1].output |
| 438 | + assert captured is not None |
| 439 | + data = captured.data |
| 440 | + assert str(source_file) in data, ( |
| 441 | + f"expected __file__ to resolve to {source_file}, got: {data}" |
| 442 | + ) |
| 443 | + assert str(source_file.parent) in data, ( |
| 444 | + f"expected notebook_dir() to resolve to {source_file.parent}, " |
| 445 | + f"got: {data}" |
| 446 | + ) |
| 447 | + assert str(ir_file) not in data |
| 448 | + |
| 449 | + |
| 450 | +async def test_from_ir_preserves_disabled_cell_config() -> None: |
| 451 | + notebook = _notebook( |
| 452 | + [ |
| 453 | + CellDef(code="'disabled output'", options={"disabled": True}), |
| 454 | + CellDef(code="'active output'"), |
| 455 | + ], |
| 456 | + ) |
| 457 | + |
| 458 | + generator = MarimoIslandGenerator._from_ir(notebook) |
| 459 | + |
| 460 | + body_html = generator.render_body(include_init_island=False) |
| 461 | + assert 'data-reactive="false"' in body_html |
| 462 | + assert 'data-reactive="true"' in body_html |
| 463 | + |
| 464 | + await generator.build() |
| 465 | + |
| 466 | + disabled_stub = generator.stubs[0] |
| 467 | + active_stub = generator.stubs[1] |
| 468 | + assert disabled_stub.output is None |
| 469 | + assert active_stub.output is not None |
| 470 | + assert "active output" in active_stub.output.data |
| 471 | + |
| 472 | + |
| 473 | +async def test_from_ir_marks_transitively_disabled_cells_static() -> None: |
| 474 | + notebook = _notebook( |
| 475 | + [ |
| 476 | + CellDef(code="x = 1", options={"disabled": True}), |
| 477 | + CellDef(code="x + 1"), |
| 478 | + ], |
| 479 | + ) |
| 480 | + |
| 481 | + generator = MarimoIslandGenerator._from_ir(notebook) |
| 482 | + |
| 483 | + rendered = [stub.render() for stub in generator.stubs] |
| 484 | + assert all('data-reactive="false"' in html for html in rendered) |
| 485 | + assert "x%20%2B%201" not in rendered[1] |
| 486 | + |
| 487 | + await generator.build() |
| 488 | + |
| 489 | + assert generator.stubs[0].output is None |
| 490 | + assert generator.stubs[1].output is None |
| 491 | + |
| 492 | + |
246 | 493 | def test_render_head(): |
247 | 494 | generator = MarimoIslandGenerator() |
248 | 495 | head_html = generator.render_head() |
|
0 commit comments