11from __future__ import annotations
22
3+ import os
34from pathlib import Path
45from threading import Event
56from typing import Any
@@ -17,6 +18,64 @@ def __init__(self, *, stdout: str, returncode: int = 0) -> None:
1718 self .returncode = returncode
1819
1920
21+ def test_source_snapshot_observes_each_selected_file_metadata_once (tmp_path , monkeypatch ):
22+ names = ["first.ts" , "nested/data.json" , "....json" , "folder.ts/child.ts" ]
23+ for name in [* names , ".json" , ".ts" , "ignored.py" , "UPPER.TS" ]:
24+ path = tmp_path / name
25+ path .parent .mkdir (parents = True , exist_ok = True )
26+ path .write_text ("{}" , encoding = "utf-8" )
27+ for name in names :
28+ os .utime (tmp_path / name , ns = (1_000_000_000 , 1_000_000_000 ))
29+ selected = {tmp_path / name for name in names }
30+ expected = []
31+ for name in sorted (names ):
32+ metadata = (tmp_path / name ).stat ()
33+ expected .append ((name , metadata .st_mtime_ns , metadata .st_ctime_ns , metadata .st_size ))
34+ observed : list [Path ] = []
35+ original_stat = Path .stat
36+
37+ def observe_stat (path , * args , ** kwargs ):
38+ if path in selected :
39+ observed .append (path )
40+ return original_stat (path , * args , ** kwargs )
41+
42+ monkeypatch .setattr (Path , "stat" , observe_stat )
43+ assert effect_runtime ._runtime_source_snapshot (tmp_path ) == tuple (expected )
44+ assert sorted (observed ) == sorted (selected )
45+
46+
47+ @pytest .mark .skipif (os .name == "nt" , reason = "requires filesystem symlinks" )
48+ def test_source_snapshot_follows_file_links_and_ignores_directory_or_broken_links (tmp_path ):
49+ root = tmp_path / "package"
50+ root .mkdir ()
51+ outside = tmp_path / "outside"
52+ outside .mkdir ()
53+ target = outside / "actual.ts"
54+ target .write_text ("export {};" , encoding = "utf-8" )
55+ (root / "alias.ts" ).symlink_to (target )
56+ (root / "directory" ).symlink_to (outside , target_is_directory = True )
57+ (root / "missing.ts" ).symlink_to (outside / "missing.ts" )
58+ (root / "loop.ts" ).symlink_to (root / "loop.ts" )
59+ metadata = target .stat ()
60+ assert effect_runtime ._runtime_source_snapshot (root ) == (
61+ ("alias.ts" , metadata .st_mtime_ns , metadata .st_ctime_ns , metadata .st_size ),
62+ )
63+
64+
65+ def test_source_snapshot_keeps_pathlib_policy_for_nonencodable_entries (tmp_path , monkeypatch ):
66+ source = tmp_path / "source.ts"
67+ source .write_text ("export {};" , encoding = "utf-8" )
68+ original_stat = Path .stat
69+
70+ def nonencodable_stat (path , * args , ** kwargs ):
71+ if path == source :
72+ raise ValueError ("non-encodable path" )
73+ return original_stat (path , * args , ** kwargs )
74+
75+ monkeypatch .setattr (Path , "stat" , nonencodable_stat )
76+ assert effect_runtime ._runtime_source_snapshot (tmp_path ) == ()
77+
78+
2079def test_runtime_fingerprint_rotates_when_any_owned_source_changes (
2180 tmp_path : Path ,
2281 monkeypatch : pytest .MonkeyPatch ,
@@ -91,17 +150,17 @@ def test_runtime_fingerprint_rescans_when_a_discovered_file_disappears(
91150 removed = tmp_path / "removed.ts"
92151 kept .write_text ("export const kept = true;\n " , encoding = "utf-8" )
93152 removed .write_text ("export const removed = true;\n " , encoding = "utf-8" )
94- original_scan = effect_runtime ._scan_runtime_source_files
153+ original_scan = effect_runtime ._scan_runtime_source_snapshot
95154 scans : list [tuple [str , ...]] = []
96155
97- def scan_then_remove (root : Path ) -> tuple [ str , ...] :
156+ def scan_then_remove (root : Path ) -> effect_runtime . _RuntimeSourceSnapshot :
98157 files = original_scan (root )
99- scans .append (files )
158+ scans .append (tuple ( row [ 0 ] for row in files ) )
100159 if len (scans ) == 1 :
101160 removed .unlink ()
102161 return files
103162
104- monkeypatch .setattr (effect_runtime , "_scan_runtime_source_files " , scan_then_remove )
163+ monkeypatch .setattr (effect_runtime , "_scan_runtime_source_snapshot " , scan_then_remove )
105164
106165 monkeypatch .setattr (effect_runtime , "_control_plane_root" , lambda : tmp_path )
107166
@@ -152,15 +211,15 @@ def _install_persistent_stat_read_churn(
152211 later = tmp_path / "later.ts"
153212 first .write_text ("export const first = true;\n " , encoding = "utf-8" )
154213 later .write_text ("export const later = true;\n " , encoding = "utf-8" )
155- original_scan = effect_runtime ._scan_runtime_source_files
214+ original_scan = effect_runtime ._scan_runtime_source_snapshot
156215 original_read_bytes = Path .read_bytes
157216 scans : list [tuple [str , ...]] = []
158217 first_read = Event ()
159218 later_reads = 0
160219
161- def record_scan (root : Path ) -> tuple [ str , ...] :
220+ def record_scan (root : Path ) -> effect_runtime . _RuntimeSourceSnapshot :
162221 files = original_scan (root )
163- scans .append (files )
222+ scans .append (tuple ( row [ 0 ] for row in files ) )
164223 return files
165224
166225 def churn_after_each_later_read (path : Path ) -> bytes :
@@ -178,7 +237,7 @@ def churn_after_each_later_read(path: Path) -> bytes:
178237 return content
179238
180239 monkeypatch .setattr (effect_runtime , "_control_plane_root" , lambda : tmp_path )
181- monkeypatch .setattr (effect_runtime , "_scan_runtime_source_files " , record_scan )
240+ monkeypatch .setattr (effect_runtime , "_scan_runtime_source_snapshot " , record_scan )
182241 monkeypatch .setattr (Path , "read_bytes" , churn_after_each_later_read )
183242 return scans
184243
0 commit comments