|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | + |
| 6 | +SCRIPT_PATH = Path(__file__).parent.parent / "scripts" / "nmem-hook-read.sh" |
| 7 | + |
| 8 | + |
| 9 | +def _write_fake_nmem(bin_dir: Path, body: str) -> Path: |
| 10 | + fake_nmem = bin_dir / "nmem" |
| 11 | + fake_nmem.write_text("#!/bin/sh\n" + body, encoding="utf-8") |
| 12 | + fake_nmem.chmod(0o755) |
| 13 | + return fake_nmem |
| 14 | + |
| 15 | + |
| 16 | +def _run_hook(tmp_path: Path, *, cwd: Path, env: dict[str, str]) -> subprocess.CompletedProcess[str]: |
| 17 | + hook_env = os.environ.copy() |
| 18 | + hook_env.update(env) |
| 19 | + hook_env["HOME"] = str(tmp_path / "home") |
| 20 | + (Path(hook_env["HOME"]) / "ai-now").mkdir(parents=True, exist_ok=True) |
| 21 | + return subprocess.run( |
| 22 | + ["/bin/sh", str(SCRIPT_PATH)], |
| 23 | + cwd=str(cwd), |
| 24 | + env=hook_env, |
| 25 | + text=True, |
| 26 | + capture_output=True, |
| 27 | + timeout=15, |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +def test_read_hook_prefers_git_common_dir_space(tmp_path): |
| 32 | + bin_dir = tmp_path / "bin" |
| 33 | + bin_dir.mkdir() |
| 34 | + calls = tmp_path / "calls.log" |
| 35 | + _write_fake_nmem( |
| 36 | + bin_dir, |
| 37 | + f""" |
| 38 | +printf '%s\\n' "$*" >> "{calls}" |
| 39 | +case "$*" in |
| 40 | + *"--space examplerepo"*) printf '%s\\n' '{{"exists": true, "content": "space briefing"}}' ;; |
| 41 | + *) printf '%s\\n' '{{"exists": true, "content": "default briefing"}}' ;; |
| 42 | +esac |
| 43 | +""", |
| 44 | + ) |
| 45 | + project = tmp_path / "ExampleRepo" |
| 46 | + subdir = project / "subdir" |
| 47 | + subdir.mkdir(parents=True) |
| 48 | + subprocess.run(["git", "init", "-q"], cwd=str(project), check=True) |
| 49 | + |
| 50 | + result = _run_hook( |
| 51 | + tmp_path, |
| 52 | + cwd=subdir, |
| 53 | + env={"PATH": f"{bin_dir}:{os.environ['PATH']}", "NMEM_SPACE": ""}, |
| 54 | + ) |
| 55 | + |
| 56 | + assert result.returncode == 0 |
| 57 | + assert result.stdout.strip() == "space briefing" |
| 58 | + assert "--space examplerepo" in calls.read_text(encoding="utf-8") |
| 59 | + |
| 60 | + |
| 61 | +def test_read_hook_honors_nmem_space_override(tmp_path): |
| 62 | + bin_dir = tmp_path / "bin" |
| 63 | + bin_dir.mkdir() |
| 64 | + calls = tmp_path / "calls.log" |
| 65 | + _write_fake_nmem( |
| 66 | + bin_dir, |
| 67 | + f""" |
| 68 | +printf '%s\\n' "$*" >> "{calls}" |
| 69 | +case "$*" in |
| 70 | + *"--space Research Lane"*) printf '%s\\n' '{{"exists": true, "content": "env briefing"}}' ;; |
| 71 | + *) printf '%s\\n' '{{"exists": true, "content": "default briefing"}}' ;; |
| 72 | +esac |
| 73 | +""", |
| 74 | + ) |
| 75 | + |
| 76 | + result = _run_hook( |
| 77 | + tmp_path, |
| 78 | + cwd=tmp_path, |
| 79 | + env={"PATH": f"{bin_dir}:{os.environ['PATH']}", "NMEM_SPACE": "Research Lane"}, |
| 80 | + ) |
| 81 | + |
| 82 | + assert result.returncode == 0 |
| 83 | + assert result.stdout.strip() == "env briefing" |
| 84 | + assert "--space Research Lane" in calls.read_text(encoding="utf-8") |
| 85 | + |
| 86 | + |
| 87 | +def test_read_hook_falls_back_to_default_space_when_project_space_empty(tmp_path): |
| 88 | + bin_dir = tmp_path / "bin" |
| 89 | + bin_dir.mkdir() |
| 90 | + _write_fake_nmem( |
| 91 | + bin_dir, |
| 92 | + """ |
| 93 | +case "$*" in |
| 94 | + *"--space "*) printf '%s\\n' '{"exists": false, "content": ""}' ;; |
| 95 | + *) printf '%s\\n' '{"exists": true, "content": "default briefing"}' ;; |
| 96 | +esac |
| 97 | +""", |
| 98 | + ) |
| 99 | + project = tmp_path / "repo" |
| 100 | + project.mkdir() |
| 101 | + subprocess.run(["git", "init", "-q"], cwd=str(project), check=True) |
| 102 | + |
| 103 | + result = _run_hook( |
| 104 | + tmp_path, |
| 105 | + cwd=project, |
| 106 | + env={"PATH": f"{bin_dir}:{os.environ['PATH']}", "NMEM_SPACE": ""}, |
| 107 | + ) |
| 108 | + |
| 109 | + assert result.returncode == 0 |
| 110 | + assert result.stdout.strip() == "default briefing" |
| 111 | + |
| 112 | + |
| 113 | +def test_read_hook_falls_back_to_local_memory_file_without_nmem(tmp_path): |
| 114 | + memory_file = tmp_path / "home" / "ai-now" / "memory.md" |
| 115 | + memory_file.parent.mkdir(parents=True) |
| 116 | + memory_file.write_text("file briefing\n", encoding="utf-8") |
| 117 | + bin_dir = tmp_path / "no-nmem-bin" |
| 118 | + bin_dir.mkdir() |
| 119 | + (bin_dir / "cat").symlink_to("/bin/cat") |
| 120 | + |
| 121 | + result = _run_hook( |
| 122 | + tmp_path, |
| 123 | + cwd=tmp_path, |
| 124 | + env={"PATH": str(bin_dir), "NMEM_SPACE": ""}, |
| 125 | + ) |
| 126 | + |
| 127 | + assert result.returncode == 0 |
| 128 | + assert result.stdout.strip() == "file briefing" |
| 129 | + |
| 130 | + |
| 131 | +def test_read_hook_invokes_windows_nmem_cmd_by_command_name(tmp_path): |
| 132 | + bin_dir = tmp_path / "bin" |
| 133 | + bin_dir.mkdir() |
| 134 | + calls = tmp_path / "cmd.log" |
| 135 | + |
| 136 | + nmem_cmd = bin_dir / "nmem.cmd" |
| 137 | + nmem_cmd.write_text("", encoding="utf-8") |
| 138 | + nmem_cmd.chmod(0o755) |
| 139 | + |
| 140 | + cmd_exe = bin_dir / "cmd.exe" |
| 141 | + cmd_exe.write_text( |
| 142 | + f"""#!/bin/sh |
| 143 | +printf '%s\\n' "$*" > "{calls}" |
| 144 | +case "$*" in |
| 145 | + *"nmem.cmd"*) printf '%s\\n' '{{"exists": true, "content": "cmd briefing"}}' ;; |
| 146 | + *) exit 1 ;; |
| 147 | +esac |
| 148 | +""", |
| 149 | + encoding="utf-8", |
| 150 | + ) |
| 151 | + cmd_exe.chmod(0o755) |
| 152 | + |
| 153 | + result = _run_hook( |
| 154 | + tmp_path, |
| 155 | + cwd=tmp_path, |
| 156 | + env={"PATH": f"{bin_dir}:/bin:/usr/bin", "NMEM_SPACE": 'project"2024'}, |
| 157 | + ) |
| 158 | + |
| 159 | + assert result.returncode == 0 |
| 160 | + assert result.stdout.strip() == "cmd briefing" |
| 161 | + command = calls.read_text(encoding="utf-8") |
| 162 | + assert '"nmem.cmd" "--json" "wm" "read"' in command |
| 163 | + assert '"project\\"2024"' in command |
0 commit comments