sh2/d2ypp2/tests/test_launch_console_build.py
2026-06-11 00:45:55 -05:00

85 lines
3.4 KiB
Python

from __future__ import annotations
import importlib.util
import json
import sys
import tempfile
import time
import unittest
from pathlib import Path
from unittest.mock import patch
ROOT = Path(__file__).resolve().parent.parent
def _load_launch_module():
spec = importlib.util.spec_from_file_location("launch_for_console_build_test", ROOT / "scripts" / "launch.py")
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
class LaunchConsoleBuildTests(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.launch = _load_launch_module()
def test_console_needs_rebuild_skips_hash_when_mtime_unchanged(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
console = Path(tmp) / "web-console"
out = console / "out"
out.mkdir(parents=True)
(out / "index.html").write_text("<html></html>", encoding="utf-8")
(out / ".build-stamp.json").write_text(
json.dumps({"source_mtime": time.time() + 3600, "source_hash": "deadbeef"}),
encoding="utf-8",
)
(console / "package.json").write_text("{}", encoding="utf-8")
app = console / "app"
app.mkdir()
(app / "page.tsx").write_text("export default function Page() { return null }", encoding="utf-8")
with patch.object(self.launch, "CONSOLE", console), patch.object(self.launch, "OUT_INDEX", out / "index.html"), patch.object(
self.launch,
"OUT_STAMP",
out / ".build-stamp.json",
):
with patch.object(self.launch, "_console_sources_hash") as hash_mock:
self.assertFalse(self.launch._console_needs_rebuild())
hash_mock.assert_not_called()
def test_console_deps_need_refresh_when_next_missing(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
console = Path(tmp) / "web-console"
console.mkdir()
(console / "package.json").write_text("{}", encoding="utf-8")
with patch.object(self.launch, "CONSOLE", console):
self.assertTrue(self.launch._console_deps_need_refresh())
def test_console_deps_need_refresh_false_when_stamp_matches(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
console = Path(tmp) / "web-console"
console.mkdir()
pkg = console / "package.json"
pkg.write_text("{}", encoding="utf-8")
lock = console / "package-lock.json"
lock.write_text("{}", encoding="utf-8")
bin_dir = console / "node_modules" / ".bin"
bin_dir.mkdir(parents=True)
next_name = "next.cmd" if self.launch.os.name == "nt" else "next"
(bin_dir / next_name).write_text("", encoding="utf-8")
stamp = {
"package_mtime": pkg.stat().st_mtime,
"lock": "package-lock.json",
"lock_mtime": lock.stat().st_mtime,
}
(console / "node_modules" / ".install-stamp.json").write_text(json.dumps(stamp), encoding="utf-8")
with patch.object(self.launch, "CONSOLE", console):
self.assertFalse(self.launch._console_deps_need_refresh())
if __name__ == "__main__":
unittest.main()