-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_package_gateways.py
More file actions
422 lines (297 loc) · 14.6 KB
/
Copy pathtest_package_gateways.py
File metadata and controls
422 lines (297 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"""Tests for Package Gateway API (groupC).
Verifies REQ-GWAY-001 through REQ-GWAY-008:
001: server/_factory.py make_context() Composition Root
002: recipe.load_and_validate()
003: recipe.validate_from_path()
004: recipe.list_all()
005: migration.check_and_migrate()
006: execution.execute_readonly_query public name
007: Each gateway __all__ is exhaustive
008: config.__all__ completeness (verified, no structural changes)
"""
from __future__ import annotations
from pathlib import Path
import pytest
from tests.arch._helpers import SRC_ROOT, _runtime_import_froms
pytestmark = [pytest.mark.layer("contracts"), pytest.mark.medium]
# ---------------------------------------------------------------------------
# REQ-GWAY-006: execution/__init__.py public surface
# ---------------------------------------------------------------------------
def test_execute_readonly_query_in_execution_all():
import autoskillit.execution as m
assert "execute_readonly_query" in m.__all__
def test_private_names_not_in_execution_all():
import autoskillit.execution as m
assert "_truncate" not in m.__all__
assert "_execute_readonly_query" not in m.__all__
def test_execute_readonly_query_is_callable():
from autoskillit.execution import execute_readonly_query
assert callable(execute_readonly_query)
# ---------------------------------------------------------------------------
# P14-F3: server/__init__.py must declare __all__
# ---------------------------------------------------------------------------
def test_server_all_defined() -> None:
import autoskillit.server as m
assert hasattr(m, "__all__"), "server/__init__.py must declare __all__"
def test_server_all_contains_core_exports() -> None:
import autoskillit.server as m
for name in ("mcp", "version_info", "make_context"):
assert name in m.__all__, f"'{name}' missing from server.__all__"
# ---------------------------------------------------------------------------
# P14-F4: _delete_directory_contents must not appear in workspace.__all__
# ---------------------------------------------------------------------------
def test_private_name_not_in_workspace_all() -> None:
import autoskillit.workspace as m
assert "_delete_directory_contents" not in m.__all__
# ---------------------------------------------------------------------------
# P14-F5: _execute_readonly_query must not be accessible at execution pkg level
# ---------------------------------------------------------------------------
def test_execute_readonly_query_private_not_at_execution_pkg_level() -> None:
import autoskillit.execution as m
assert not hasattr(m, "_execute_readonly_query"), (
"_execute_readonly_query must not be accessible at "
"autoskillit.execution package level after import-as fix"
)
# ---------------------------------------------------------------------------
# REQ-GWAY-002/003/004: recipe/__init__.py facades
# ---------------------------------------------------------------------------
def test_recipe_facades_in_all():
import autoskillit.recipe as m
assert "load_and_validate" in m.__all__
assert "validate_from_path" in m.__all__
assert "list_all" in m.__all__
def test_recipe_load_and_validate_not_found(tmp_path):
from autoskillit.core import RecipeNotFoundError
from autoskillit.recipe import load_and_validate
with pytest.raises(RecipeNotFoundError):
load_and_validate("__nonexistent__", project_dir=tmp_path)
def test_recipe_load_and_validate_found_returns_required_keys(tmp_path):
from autoskillit.recipe import load_and_validate
from autoskillit.recipe.io import list_recipes
recipes = list_recipes(Path("/nonexistent"))
bundled = [r for r in recipes.items if r.source.value == "builtin"]
if not bundled:
pytest.skip("No bundled recipes available")
result = load_and_validate(bundled[0].name)
assert "content" in result
assert "suggestions" in result
assert "valid" in result
assert isinstance(result["suggestions"], list)
assert isinstance(result["valid"], bool)
def test_recipe_validate_from_path_not_found(tmp_path):
from autoskillit.recipe import validate_from_path
result = validate_from_path(tmp_path / "nonexistent.yaml")
assert result["valid"] is False
assert len(result["findings"]) > 0
def test_recipe_validate_from_path_valid_file(tmp_path):
from autoskillit.recipe import validate_from_path
from autoskillit.recipe.io import list_recipes
recipes = list_recipes(Path("/nonexistent"))
bundled = [r for r in recipes.items if r.source.value == "builtin"]
if not bundled:
pytest.skip("No bundled recipes available")
result = validate_from_path(bundled[0].path)
assert "valid" in result
assert "findings" in result
assert isinstance(result["findings"], list)
def test_recipe_list_all_returns_required_keys(tmp_path):
from autoskillit.recipe import list_all
result = list_all(project_dir=tmp_path)
assert "count" in result
assert "recipes" in result
assert isinstance(result["count"], int)
assert isinstance(result["recipes"], list)
assert result["count"] == len(result["recipes"])
def test_recipe_list_all_includes_builtins():
from autoskillit.recipe import list_all
result = list_all()
assert result["count"] >= 4 # at least the 4 bundled recipes
# ---------------------------------------------------------------------------
# REQ-GWAY-005: migration/__init__.py check_and_migrate
# ---------------------------------------------------------------------------
@pytest.mark.anyio
async def test_migration_check_and_migrate_in_all():
import autoskillit.migration as m
assert "check_and_migrate" in m.__all__
@pytest.mark.anyio
async def test_migration_check_and_migrate_not_found(tmp_path):
from autoskillit import __version__
from autoskillit.migration import check_and_migrate
result = await check_and_migrate("__nonexistent__", tmp_path, __version__)
assert "error" in result
@pytest.mark.anyio
async def test_migration_check_and_migrate_up_to_date(tmp_path):
from autoskillit import __version__
from autoskillit.migration import check_and_migrate
from autoskillit.recipe.io import list_recipes
recipes = list_recipes(Path("/nonexistent"))
bundled = [r for r in recipes.items if r.source.value == "builtin"]
if not bundled:
pytest.skip("No bundled recipes available")
# Bundled recipes are current — should report up_to_date
# Use tmp_path so project recipes dir doesn't interfere
result = await check_and_migrate(bundled[0].name, tmp_path, __version__)
assert result.get("status") == "up_to_date"
# ---------------------------------------------------------------------------
# REQ-GWAY-001: server/_factory.py Composition Root
# ---------------------------------------------------------------------------
def test_factory_make_context_returns_toolcontext(monkeypatch, tmp_path):
from autoskillit.config import AutomationConfig
from autoskillit.core.paths import pkg_root
from autoskillit.pipeline.audit import DefaultAuditLog
from autoskillit.pipeline.context import ToolContext
from autoskillit.server._factory import make_context
monkeypatch.setattr("autoskillit.server._factory._check_plugin_installed", lambda: False)
ctx = make_context(AutomationConfig(), project_dir=tmp_path)
assert isinstance(ctx, ToolContext)
assert ctx.gate.enabled is False # starts closed
assert isinstance(ctx.audit, DefaultAuditLog)
assert ctx.token_log is not None
from autoskillit.core.types._type_plugin_source import DirectInstall
assert isinstance(ctx.plugin_source, DirectInstall)
assert ctx.plugin_source.plugin_dir == pkg_root()
def test_factory_make_context_accepts_runner(tmp_path):
from autoskillit.config import AutomationConfig
from autoskillit.server._factory import make_context
ctx = make_context(AutomationConfig(), runner=None, project_dir=tmp_path)
assert ctx.runner is None
def test_factory_make_context_accepts_plugin_dir(tmp_path):
from autoskillit.config import AutomationConfig
from autoskillit.core.types._type_plugin_source import DirectInstall
from autoskillit.server._factory import make_context
ctx = make_context(AutomationConfig(), plugin_dir=str(tmp_path), project_dir=tmp_path)
assert isinstance(ctx.plugin_source, DirectInstall)
assert ctx.plugin_source.plugin_dir == tmp_path
# ---------------------------------------------------------------------------
# REQ-GWAY-007/008: __all__ exhaustiveness
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
"pkg_name",
[
"autoskillit.recipe",
"autoskillit.migration",
"autoskillit.execution",
"autoskillit.config",
],
)
def test_all_entries_importable(pkg_name):
import importlib
mod = importlib.import_module(pkg_name)
for name in mod.__all__:
assert hasattr(mod, name), f"{pkg_name}.__all__ has {name!r} but module has no attribute"
def test_config_all_complete():
from autoskillit.config import __all__ as config_all
assert "AutomationConfig" in config_all
assert "load_config" in config_all
# Spot-check all sub-config classes are present
for cls in ["TestCheckConfig", "SafetyConfig", "CoreRunConfig", "TokenUsageConfig"]:
assert cls in config_all, f"{cls} missing from config.__all__"
# ---------------------------------------------------------------------------
# REQ-IMP-001 gateway completeness — Default* concrete classes
# ---------------------------------------------------------------------------
def test_execution_gateway_exports_default_classes() -> None:
import autoskillit.execution as m
for name in ("DefaultDatabaseReader", "DefaultHeadlessExecutor", "DefaultTestRunner"):
assert name in m.__all__, f"{name} missing from execution.__all__"
def test_migration_gateway_exports_default_migration_service() -> None:
import autoskillit.migration as m
assert "DefaultMigrationService" in m.__all__
def test_recipe_gateway_exports_default_recipe_repository() -> None:
import autoskillit.recipe as m
assert "DefaultRecipeRepository" in m.__all__
def test_workspace_gateway_exports_default_workspace_manager() -> None:
import autoskillit.workspace as m
assert "DefaultWorkspaceManager" in m.__all__
def test_workspace_gateway_exports_public_delete_alias() -> None:
import autoskillit.workspace as m
assert "delete_directory_contents" in m.__all__
# ── REQ-ARCH-004: __all__ completeness ───────────────────────────────────────
def test_package_all_matches_exports() -> None:
"""REQ-ARCH-004: Each package __init__.__all__ must match its exported symbol set.
Two checks:
1. Every name in __all__ is importable from the package (no dead entries).
2. Every public name re-exported via relative or autoskillit.* imports in __init__.py
appears in __all__ (no undeclared exports).
Packages without __all__ (root autoskillit) are skipped.
"""
import importlib
AUTOSKILLIT_ROOT = SRC_ROOT
PACKAGES_WITH_ALL = [
"core",
"config",
"pipeline",
"planner",
"execution",
"workspace",
"recipe",
"migration",
"fleet",
"hooks",
"cli",
"server",
"report",
]
violations: list[str] = []
for pkg_name in PACKAGES_WITH_ALL:
module = importlib.import_module(f"autoskillit.{pkg_name}")
all_list: list[str] = getattr(module, "__all__", None) # type: ignore[assignment]
if all_list is None:
continue # package opted out of __all__ — skip
# Check 1: every __all__ entry is importable
for name in all_list:
if not hasattr(module, name):
violations.append(
f"autoskillit.{pkg_name}: '{name}' in __all__ but not importable"
)
# Check 2: every public name from relative / intra-package imports is in __all__
# Only intra-package absolute imports (autoskillit.{pkg_name}.*) are checked —
# cross-package imports (e.g. `from autoskillit.core import get_logger` in
# recipe/__init__.py) are internal helpers, not re-exports, and must be excluded.
init_path = AUTOSKILLIT_ROOT / pkg_name / "__init__.py"
for node in _runtime_import_froms(init_path):
is_relative = node.level and node.level > 0
is_intra_package = node.module and node.module.startswith(f"autoskillit.{pkg_name}.")
if not (is_relative or is_intra_package):
continue # skip stdlib / third-party / cross-package imports
for alias in node.names:
name = alias.asname if alias.asname else alias.name
if name.startswith("_") or name == "*":
continue
if name not in all_list:
violations.append(
f"autoskillit.{pkg_name}: '{name}' re-exported via import "
f"but not in __all__"
)
assert not violations, "__all__ completeness violations:\n" + "\n".join(violations)
# ── REQ-ARCH-005: root-level module allowlist ──────────────────────────────────
def test_root_module_allowlist() -> None:
"""REQ-ARCH-005: Exactly the expected set of .py files exists at the package root.
Fails when a new root-level .py file is added without updating this allowlist,
forcing deliberate acknowledgement of new root-level additions.
Also fails if an expected file is missing, catching stale allowlist entries.
"""
_ALLOWED_ROOT_MODULES = frozenset(
{
"__init__.py",
"__main__.py",
"_llm_triage.py",
"_probe_canary.py",
"_test_filter.py",
"hook_registry.py",
"version.py",
}
)
actual = frozenset(p.name for p in SRC_ROOT.glob("*.py"))
unexpected = actual - _ALLOWED_ROOT_MODULES
assert not unexpected, (
f"Unauthorized root-level .py file(s) added to src/autoskillit/: "
f"{sorted(unexpected)}. "
"Either move the file to a sub-package or update the allowlist in "
"test_root_module_allowlist()."
)
missing = _ALLOWED_ROOT_MODULES - actual
assert not missing, (
f"Expected root-level .py file(s) no longer found in src/autoskillit/: "
f"{sorted(missing)}. "
"Remove the file from the allowlist in test_root_module_allowlist()."
)