Files
skill-seekers-reference/tests/test_unified.py
yusyus 6d37e43b83 feat: Grand Unification — one command, one interface, direct converters (#346)
* fix: resolve 8 pipeline bugs found during skill quality review

- Fix 0 APIs extracted from documentation by enriching summary.json
  with individual page file content before conflict detection
- Fix all "Unknown" entries in merged_api.md by injecting dict keys
  as API names and falling back to AI merger field names
- Fix frontmatter using raw slugs instead of config name by
  normalizing frontmatter after SKILL.md generation
- Fix leaked absolute filesystem paths in patterns/index.md by
  stripping .skillseeker-cache repo clone prefixes
- Fix ARCHITECTURE.md file count always showing "1 files" by
  counting files per language from code_analysis data
- Fix YAML parse errors on GitHub Actions workflows by converting
  boolean keys (on: true) to strings
- Fix false React/Vue.js framework detection in C# projects by
  filtering web frameworks based on primary language
- Improve how-to guide generation by broadening workflow example
  filter to include setup/config examples with sufficient complexity
- Fix test_git_sources_e2e failures caused by git init default
  branch being 'main' instead of 'master'

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: address 6 review issues in ExecutionContext implementation

Fixes from code review:

1. Mode resolution (#3 critical): _args_to_data no longer unconditionally
   overwrites mode. Only writes mode="api" when --api-key explicitly passed.
   Env-var-based mode detection moved to _default_data() as lowest priority.

2. Re-initialization warning (#4): initialize() now logs debug message
   when called a second time instead of silently returning stale instance.

3. _raw_args preserved in override (#5): temp context now copies _raw_args
   from parent so get_raw() works correctly inside override blocks.

4. test_local_mode_detection env cleanup (#7): test now saves/restores
   API key env vars to prevent failures when ANTHROPIC_API_KEY is set.

5. _load_config_file error handling (#8): wraps FileNotFoundError and
   JSONDecodeError with user-friendly ValueError messages.

6. Lint fixes: added logging import, fixed Generator import from
   collections.abc, fixed AgentClient return type annotation.

Remaining P2/P3 items (documented, not blocking):
- Lock TOCTOU in override() — safe on CPython, needs fix for no-GIL
- get() reads _instance without lock — same CPython caveat
- config_path not stored on instance
- AnalysisSettings.depth not Literal constrained

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: address all remaining P2/P3 review issues in ExecutionContext

1. Thread safety: get() now acquires _lock before reading _instance (#2)
2. Thread safety: override() saves/restores _initialized flag to prevent
   re-init during override blocks (#10)
3. Config path stored: _config_path PrivateAttr + config_path property (#6)
4. Literal validation: AnalysisSettings.depth now uses
   Literal["surface", "deep", "full"] — rejects invalid values (#9)
5. Test updated: test_analysis_depth_choices now expects ValidationError
   for invalid depth, added test_analysis_depth_valid_choices
6. Lint cleanup: removed unused imports, fixed whitespace in tests

All 10 previously reported issues now resolved.
26 tests pass, lint clean.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: restore 5 truncated scrapers, migrate unified_scraper, fix context init

5 scrapers had main() truncated with "# Original main continues here..."
after Kimi's migration — business logic was never connected:
- html_scraper.py — restored HtmlToSkillConverter extraction + build
- pptx_scraper.py — restored PptxToSkillConverter extraction + build
- confluence_scraper.py — restored ConfluenceToSkillConverter with 3 modes
- notion_scraper.py — restored NotionToSkillConverter with 4 sources
- chat_scraper.py — restored ChatToSkillConverter extraction + build

unified_scraper.py — migrated main() to context-first pattern with argv fallback

Fixed context initialization chain:
- main.py no longer initializes ExecutionContext (was stealing init from commands)
- create_command.py now passes config_path from source_info.parsed
- execution_context.py handles SourceInfo.raw_input (not raw_source)

All 18 scrapers now genuinely migrated. 26 tests pass, lint clean.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: resolve 7 data flow conflicts between ExecutionContext and legacy paths

Critical fixes (CLI args silently lost):
- unified_scraper Phase 6: reads ctx.enhancement.level instead of raw JSON
  when args=None (#3, #4)
- unified_scraper Phase 6 agent: reads ctx.enhancement.agent instead of
  3 independent env var lookups (#5)
- doc_scraper._run_enhancement: uses agent_client.api_key instead of raw
  os.environ.get() — respects config file api_key (#1)

Important fixes:
- main._handle_analyze_command: populates _fake_args from ExecutionContext
  so --agent and --api-key aren't lost in analyze→enhance path (#6)
- doc_scraper type annotations: replaced forward refs with Any to avoid
  F821 undefined name errors

All changes include RuntimeError fallback for backward compatibility when
ExecutionContext isn't initialized.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: 3 crashes + 1 stub in migrated scrapers found by deep scan

1. github_scraper.py: args.scrape_only and args.enhance_level crash when
   args=None (context path). Guarded with if args and getattr(). Also
   fixed agent fallback to read ctx.enhancement.agent.

2. codebase_scraper.py: args.output and args.skip_api_reference crash in
   summary block when args=None. Replaced with output_dir local var and
   ctx.analysis.skip_api_reference.

3. epub_scraper.py: main() was still a stub ending with "# Rest of main()
   continues..." — restored full extraction + build + enhancement logic
   using ctx values exclusively.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: complete ExecutionContext migration for remaining scrapers

Kimi's Phase 4 scraper migrations + Claude's review fixes.
All 18 scrapers now use context-first pattern with argv fallback.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: Phase 1 — ExecutionContext.get() always returns context (no RuntimeError)

get() now returns a default context instead of raising RuntimeError when
not explicitly initialized. This eliminates the need for try/except
RuntimeError blocks in all 18 scrapers.

Components can always call ExecutionContext.get() safely — it returns
defaults if not initialized, or the explicitly initialized instance.

Updated tests: test_get_returns_defaults_when_not_initialized,
test_reset_clears_instance (no longer expects RuntimeError).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: Phase 2a-c — remove 16 individual scraper CLI commands

Removed individual scraper commands from:
- COMMAND_MODULES in main.py (16 entries: scrape, github, pdf, word,
  epub, video, jupyter, html, openapi, asciidoc, pptx, rss, manpage,
  confluence, notion, chat)
- pyproject.toml entry points (16 skill-seekers-<type> binaries)
- parsers/__init__.py (16 parser registrations)

All source types now accessed via: skill-seekers create <source>
Kept: create, unified, analyze, enhance, package, upload, install,
      install-agent, config, doctor, and utility commands.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: create SkillConverter base class + converter registry

New base interface that all 17 converters will inherit:
- SkillConverter.run() — extract + build (same call for all types)
- SkillConverter.extract() — override in subclass
- SkillConverter.build_skill() — override in subclass
- get_converter(source_type, config) — factory from registry
- CONVERTER_REGISTRY — maps source type → (module, class)

create_command will use get_converter() instead of _call_module().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: Grand Unification — one command, one interface, direct converters

Complete the Grand Unification refactor: `skill-seekers create` is now
the single entry point for all 18 source types. Individual scraper CLI
commands (scrape, github, pdf, analyze, unified, etc.) are removed.

## Architecture changes

- **18 SkillConverter subclasses**: Every scraper now inherits SkillConverter
  with extract() + build_skill() + SOURCE_TYPE. Factory via get_converter().
- **create_command.py rewritten**: _build_config() constructs config dicts
  from ExecutionContext for each source type. Direct converter.run() calls
  replace the old _build_argv() + sys.argv swap + _call_module() machinery.
- **main.py simplified**: create command bypasses _reconstruct_argv entirely,
  calls CreateCommand(args).execute() directly. analyze/unified commands
  removed (create handles both via auto-detection).
- **CreateParser mode="all"**: Top-level parser now accepts all 120+ flags
  (--browser, --max-pages, --depth, etc.) since create is the only entry.
- **Centralized enhancement**: Runs once in create_command after converter,
  not duplicated in each scraper.
- **MCP tools use converters**: 5 scraping tools call get_converter()
  directly instead of subprocess. Config type auto-detected from keys.
- **ConfigValidator → UniSkillConfigValidator**: Renamed with backward-
  compat alias.
- **Data flow**: AgentClient + LocalSkillEnhancer read ExecutionContext
  first, env vars as fallback.

## What was removed

- main() from all 18 scraper files (~3400 lines)
- 18 CLI commands from COMMAND_MODULES + pyproject.toml entry points
- analyze + unified parsers from parser registry
- _build_argv, _call_module, _SKIP_ARGS, _DEST_TO_FLAG, all _route_*()
- setup_argument_parser, get_configuration, _check_deprecated_flags
- Tests referencing removed commands/functions

## Net impact

51 files changed, ~6000 lines removed. 2996 tests pass, 0 failures.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: review fixes for Grand Unification PR

- Add autouse conftest fixture to reset ExecutionContext singleton between tests
- Replace hardcoded defaults in _is_explicitly_set() with parser-derived defaults
- Upgrade ExecutionContext double-init log from debug to info
- Use logger.exception() in SkillConverter.run() to preserve tracebacks
- Fix docstring "17 types" → "18 types" in skill_converter.py
- DRY up 10 copy-paste help handlers into dict + loop (~100 lines removed)
- Fix 2 CI workflows still referencing removed `skill-seekers scrape` command
- Remove broken pyproject.toml entry point for codebase_scraper:main

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: resolve 12 logic/flow issues found in deep review

Critical fixes:
- UnifiedScraper.run(): replace sys.exit(1) with return 1, add return 0
- doc_scraper: use ExecutionContext.get() when already initialized instead
  of re-calling initialize() which silently discards new config
- unified_scraper: define enhancement_config before try/except to prevent
  UnboundLocalError in LOCAL enhancement timeout read

Important fixes:
- override(): cleaner tuple save/restore for singleton swap
- --agent without --api-key now sets mode="local" so env API key doesn't
  override explicit agent choice
- Remove DeprecationWarning from _reconstruct_argv (fires on every
  non-create command in production)
- Rewrite scrape_generic_tool to use get_converter() instead of subprocess
  calls to removed main() functions
- SkillConverter.run() checks build_skill() return value, returns 1 if False
- estimate_pages_tool uses -m module invocation instead of .py file path

Low-priority fixes:
- get_converter() raises descriptive ValueError on class name typo
- test_default_values: save/clear API key env vars before asserting mode
- test_get_converter_pdf: fix config key "path" → "pdf_path"

3056 passed, 4 failed (pre-existing dep version issues), 32 skipped.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: update MCP server tests to mock converter instead of subprocess

scrape_docs_tool now uses get_converter() + _run_converter() in-process
instead of run_subprocess_with_streaming. Update 4 TestScrapeDocsTool
tests to mock the converter layer instead of the removed subprocess path.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: YusufKaraaslanSpyke <yusuf@spykegames.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 23:00:52 +03:00

727 lines
22 KiB
Python

#!/usr/bin/env python3
"""
Tests for Unified Multi-Source Scraper
Covers:
- Config validation (unified vs legacy)
- Conflict detection
- Rule-based merging
- Skill building
"""
import json
import os
import tempfile
from pathlib import Path
import pytest
from skill_seekers.cli.config_validator import ConfigValidator, validate_config
from skill_seekers.cli.conflict_detector import Conflict, ConflictDetector
from skill_seekers.cli.merge_sources import RuleBasedMerger
from skill_seekers.cli.unified_skill_builder import UnifiedSkillBuilder
# ===========================
# Config Validation Tests
# ===========================
def test_detect_unified_format():
"""Test unified format detection and legacy rejection"""
import json
import tempfile
unified_config = {
"name": "test",
"description": "Test skill",
"sources": [{"type": "documentation", "base_url": "https://example.com"}],
}
legacy_config = {"name": "test", "description": "Test skill", "base_url": "https://example.com"}
# Test unified detection
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(unified_config, f)
config_path = f.name
try:
validator = ConfigValidator(config_path)
assert validator.is_unified
validator.validate() # Should pass
finally:
os.unlink(config_path)
# Test legacy rejection (legacy format removed in v2.11.0)
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(legacy_config, f)
config_path = f.name
try:
validator = ConfigValidator(config_path)
assert validator.is_unified # Always True now
# Validation should fail for legacy format
with pytest.raises(ValueError, match="LEGACY CONFIG FORMAT DETECTED"):
validator.validate()
finally:
os.unlink(config_path)
def test_validate_unified_sources():
"""Test source type validation"""
config = {
"name": "test",
"description": "Test",
"sources": [
{"type": "documentation", "base_url": "https://example.com"},
{"type": "github", "repo": "user/repo"},
{"type": "pdf", "path": "/path/to.pdf"},
],
}
validator = ConfigValidator(config)
validator.validate()
assert len(validator.config["sources"]) == 3
def test_validate_invalid_source_type():
"""Test invalid source type raises error"""
config = {
"name": "test",
"description": "Test",
"sources": [{"type": "invalid_type", "url": "https://example.com"}],
}
validator = ConfigValidator(config)
with pytest.raises(ValueError, match="Invalid type"):
validator.validate()
def test_needs_api_merge():
"""Test API merge detection"""
# Config with both docs and GitHub code
config_needs_merge = {
"name": "test",
"description": "Test",
"sources": [
{"type": "documentation", "base_url": "https://example.com", "extract_api": True},
{"type": "github", "repo": "user/repo", "include_code": True},
],
}
validator = ConfigValidator(config_needs_merge)
assert validator.needs_api_merge()
# Config with only docs
config_no_merge = {
"name": "test",
"description": "Test",
"sources": [{"type": "documentation", "base_url": "https://example.com"}],
}
validator = ConfigValidator(config_no_merge)
assert not validator.needs_api_merge()
def test_backward_compatibility():
"""Test legacy config rejection (removed in v2.11.0)"""
legacy_config = {
"name": "test",
"description": "Test skill",
"base_url": "https://example.com",
"selectors": {"main_content": "article"},
"max_pages": 100,
}
# Legacy format should be rejected with clear error message
validator = ConfigValidator(legacy_config)
with pytest.raises(ValueError) as exc_info:
validator.validate()
# Check error message provides migration guidance
error_msg = str(exc_info.value)
assert "LEGACY CONFIG FORMAT DETECTED" in error_msg
assert "removed in v2.11.0" in error_msg
assert "sources" in error_msg # Shows new format requires sources array
# ===========================
# Conflict Detection Tests
# ===========================
def test_detect_missing_in_docs():
"""Test detection of APIs missing in documentation"""
docs_data = {
"pages": [
{
"url": "https://example.com/api",
"apis": [
{
"name": "documented_func",
"parameters": [{"name": "x", "type": "int"}],
"return_type": "str",
}
],
}
]
}
github_data = {
"code_analysis": {
"analyzed_files": [
{
"functions": [
{
"name": "undocumented_func",
"parameters": [{"name": "y", "type_hint": "float"}],
"return_type": "bool",
}
]
}
]
}
}
detector = ConflictDetector(docs_data, github_data)
conflicts = detector._find_missing_in_docs()
assert len(conflicts) > 0
assert any(c.type == "missing_in_docs" for c in conflicts)
assert any(c.api_name == "undocumented_func" for c in conflicts)
def test_detect_missing_in_code():
"""Test detection of APIs missing in code"""
docs_data = {
"pages": [
{
"url": "https://example.com/api",
"apis": [
{
"name": "obsolete_func",
"parameters": [{"name": "x", "type": "int"}],
"return_type": "str",
}
],
}
]
}
github_data = {"code_analysis": {"analyzed_files": []}}
detector = ConflictDetector(docs_data, github_data)
conflicts = detector._find_missing_in_code()
assert len(conflicts) > 0
assert any(c.type == "missing_in_code" for c in conflicts)
assert any(c.api_name == "obsolete_func" for c in conflicts)
def test_detect_signature_mismatch():
"""Test detection of signature mismatches"""
docs_data = {
"pages": [
{
"url": "https://example.com/api",
"apis": [
{
"name": "func",
"parameters": [{"name": "x", "type": "int"}],
"return_type": "str",
}
],
}
]
}
github_data = {
"code_analysis": {
"analyzed_files": [
{
"functions": [
{
"name": "func",
"parameters": [
{"name": "x", "type_hint": "int"},
{"name": "y", "type_hint": "bool", "default": "False"},
],
"return_type": "str",
}
]
}
]
}
}
detector = ConflictDetector(docs_data, github_data)
conflicts = detector._find_signature_mismatches()
assert len(conflicts) > 0
assert any(c.type == "signature_mismatch" for c in conflicts)
assert any(c.api_name == "func" for c in conflicts)
def test_conflict_severity():
"""Test conflict severity assignment"""
# High severity: missing_in_code
conflict_high = Conflict(
type="missing_in_code",
severity="high",
api_name="test",
docs_info={"name": "test"},
code_info=None,
difference="API documented but not in code",
)
assert conflict_high.severity == "high"
# Medium severity: missing_in_docs
conflict_medium = Conflict(
type="missing_in_docs",
severity="medium",
api_name="test",
docs_info=None,
code_info={"name": "test"},
difference="API in code but not documented",
)
assert conflict_medium.severity == "medium"
# ===========================
# Merge Tests
# ===========================
def test_rule_based_merge_docs_only():
"""Test rule-based merge for docs-only APIs"""
docs_data = {
"pages": [
{
"url": "https://example.com/api",
"apis": [
{
"name": "docs_only_api",
"parameters": [{"name": "x", "type": "int"}],
"return_type": "str",
}
],
}
]
}
github_data = {"code_analysis": {"analyzed_files": []}}
detector = ConflictDetector(docs_data, github_data)
conflicts = detector.detect_all_conflicts()
merger = RuleBasedMerger(docs_data, github_data, conflicts)
merged = merger.merge_all()
assert "apis" in merged
assert "docs_only_api" in merged["apis"]
assert merged["apis"]["docs_only_api"]["status"] == "docs_only"
def test_rule_based_merge_code_only():
"""Test rule-based merge for code-only APIs"""
docs_data = {"pages": []}
github_data = {
"code_analysis": {
"analyzed_files": [
{
"functions": [
{
"name": "code_only_api",
"parameters": [{"name": "y", "type_hint": "float"}],
"return_type": "bool",
}
]
}
]
}
}
detector = ConflictDetector(docs_data, github_data)
conflicts = detector.detect_all_conflicts()
merger = RuleBasedMerger(docs_data, github_data, conflicts)
merged = merger.merge_all()
assert "apis" in merged
assert "code_only_api" in merged["apis"]
assert merged["apis"]["code_only_api"]["status"] == "code_only"
def test_rule_based_merge_matched():
"""Test rule-based merge for matched APIs"""
docs_data = {
"pages": [
{
"url": "https://example.com/api",
"apis": [
{
"name": "matched_api",
"parameters": [{"name": "x", "type": "int"}],
"return_type": "str",
}
],
}
]
}
github_data = {
"code_analysis": {
"analyzed_files": [
{
"functions": [
{
"name": "matched_api",
"parameters": [{"name": "x", "type_hint": "int"}],
"return_type": "str",
}
]
}
]
}
}
detector = ConflictDetector(docs_data, github_data)
conflicts = detector.detect_all_conflicts()
merger = RuleBasedMerger(docs_data, github_data, conflicts)
merged = merger.merge_all()
assert "apis" in merged
assert "matched_api" in merged["apis"]
assert merged["apis"]["matched_api"]["status"] == "matched"
def test_merge_summary():
"""Test merge summary statistics"""
docs_data = {
"pages": [
{
"url": "https://example.com/api",
"apis": [
{"name": "api1", "parameters": [], "return_type": "str"},
{"name": "api2", "parameters": [], "return_type": "int"},
],
}
]
}
github_data = {
"code_analysis": {
"analyzed_files": [
{"functions": [{"name": "api3", "parameters": [], "return_type": "bool"}]}
]
}
}
detector = ConflictDetector(docs_data, github_data)
conflicts = detector.detect_all_conflicts()
merger = RuleBasedMerger(docs_data, github_data, conflicts)
merged = merger.merge_all()
assert "summary" in merged
assert merged["summary"]["total_apis"] == 3
assert merged["summary"]["docs_only"] == 2
assert merged["summary"]["code_only"] == 1
# ===========================
# Skill Builder Tests
# ===========================
def test_skill_builder_basic():
"""Test basic skill building"""
config = {
"name": "test_skill",
"description": "Test skill description",
"sources": [{"type": "documentation", "base_url": "https://example.com"}],
}
scraped_data = {"documentation": {"pages": [], "data_file": "/tmp/test.json"}}
with tempfile.TemporaryDirectory() as tmpdir:
# Override output directory
builder = UnifiedSkillBuilder(config, scraped_data)
builder.skill_dir = tmpdir
builder._generate_skill_md()
# Check SKILL.md was created
skill_md = Path(tmpdir) / "SKILL.md"
assert skill_md.exists()
content = skill_md.read_text()
assert "test_skill" in content.lower()
assert "Test skill description" in content
def test_skill_builder_with_conflicts():
"""Test skill building with conflicts"""
config = {
"name": "test_skill",
"description": "Test",
"sources": [
{"type": "documentation", "base_url": "https://example.com"},
{"type": "github", "repo": "user/repo"},
],
}
scraped_data = {}
conflicts = [
Conflict(
type="missing_in_code",
severity="high",
api_name="test_api",
docs_info={"name": "test_api"},
code_info=None,
difference="Test difference",
)
]
with tempfile.TemporaryDirectory() as tmpdir:
builder = UnifiedSkillBuilder(config, scraped_data, conflicts=conflicts)
builder.skill_dir = tmpdir
builder._generate_skill_md()
skill_md = Path(tmpdir) / "SKILL.md"
content = skill_md.read_text()
assert "1 conflicts detected" in content
assert "missing_in_code" in content
def test_skill_builder_merged_apis():
"""Test skill building with merged APIs"""
config = {"name": "test", "description": "Test", "sources": []}
scraped_data = {}
merged_data = {
"apis": {
"test_api": {
"name": "test_api",
"status": "matched",
"merged_signature": "test_api(x: int) -> str",
"merged_description": "Test API",
"source": "both",
}
}
}
with tempfile.TemporaryDirectory() as tmpdir:
builder = UnifiedSkillBuilder(config, scraped_data, merged_data=merged_data)
builder.skill_dir = tmpdir
content = builder._format_merged_apis()
assert "✅ Verified APIs" in content
assert "test_api" in content
# ===========================
# Integration Tests
# ===========================
def test_full_workflow_unified_config():
"""Test complete workflow with unified config"""
# Create test config
config = {
"name": "test_unified",
"description": "Test unified workflow",
"merge_mode": "rule-based",
"sources": [
{"type": "documentation", "base_url": "https://example.com", "extract_api": True},
{
"type": "github",
"repo": "user/repo",
"include_code": True,
"code_analysis_depth": "surface",
},
],
}
# Validate config
validator = ConfigValidator(config)
validator.validate()
assert validator.is_unified
assert validator.needs_api_merge()
def test_config_file_validation():
"""Test validation from config file"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
config = {
"name": "test",
"description": "Test",
"sources": [{"type": "documentation", "base_url": "https://example.com"}],
}
json.dump(config, f)
config_path = f.name
try:
validator = validate_config(config_path)
assert validator.is_unified
finally:
os.unlink(config_path)
# ===========================
# Workflow JSON Config Tests
# ===========================
class TestWorkflowJsonConfig:
"""Test that UnifiedScraper.run() merges JSON workflow fields into effective_args."""
def _make_scraper(self, tmp_path, extra_config=None):
"""Build a minimal UnifiedScraper backed by a temp config file."""
from skill_seekers.cli.unified_scraper import UnifiedScraper
config = {
"name": "test_workflow",
"description": "Test workflow config",
"sources": [],
**(extra_config or {}),
}
cfg_file = tmp_path / "config.json"
cfg_file.write_text(json.dumps(config))
scraper = UnifiedScraper.__new__(UnifiedScraper)
scraper.config = config
scraper.name = config["name"]
return scraper
def test_json_workflows_merged_when_args_none(self, tmp_path, monkeypatch):
"""JSON 'workflows' list is used even when args=None."""
captured = {}
def fake_run_workflows(args, context=None): # noqa: ARG001
captured["enhance_workflow"] = getattr(args, "enhance_workflow", None)
monkeypatch.setattr(
"skill_seekers.cli.workflow_runner.run_workflows", fake_run_workflows, raising=False
)
import skill_seekers.cli.unified_scraper as us_module
monkeypatch.setattr(us_module, "run_workflows", fake_run_workflows, raising=False)
scraper = self._make_scraper(tmp_path, {"workflows": ["security-focus", "minimal"]})
# Patch _merge_workflow_config inline by directly testing the logic
import argparse
effective_args = argparse.Namespace(
enhance_workflow=None, enhance_stage=None, var=None, workflow_dry_run=False
)
json_workflows = scraper.config.get("workflows", [])
if json_workflows:
effective_args.enhance_workflow = (
list(effective_args.enhance_workflow or []) + json_workflows
)
assert effective_args.enhance_workflow == ["security-focus", "minimal"]
def test_json_workflows_appended_after_cli(self, tmp_path):
"""CLI --enhance-workflow values come first; JSON 'workflows' appended after."""
import argparse
config = {
"name": "test",
"description": "test",
"sources": [],
"workflows": ["json-wf"],
}
cfg_file = tmp_path / "config.json"
cfg_file.write_text(json.dumps(config))
cli_args = argparse.Namespace(
enhance_workflow=["cli-wf"],
enhance_stage=None,
var=None,
workflow_dry_run=False,
)
json_workflows = config.get("workflows", [])
effective = argparse.Namespace(
enhance_workflow=list(cli_args.enhance_workflow or []) + json_workflows,
enhance_stage=None,
var=None,
workflow_dry_run=False,
)
assert effective.enhance_workflow == ["cli-wf", "json-wf"]
def test_json_workflow_stages_merged(self, tmp_path):
"""JSON 'workflow_stages' are appended to enhance_stage."""
import argparse
config = {"workflow_stages": ["sec:Analyze security", "cleanup:Remove boilerplate"]}
effective_args = argparse.Namespace(
enhance_workflow=None, enhance_stage=None, var=None, workflow_dry_run=False
)
json_stages = config.get("workflow_stages", [])
if json_stages:
effective_args.enhance_stage = list(effective_args.enhance_stage or []) + json_stages
assert effective_args.enhance_stage == [
"sec:Analyze security",
"cleanup:Remove boilerplate",
]
def test_json_workflow_vars_converted_to_kv_strings(self, tmp_path):
"""JSON 'workflow_vars' dict is converted to 'key=value' strings."""
import argparse
config = {"workflow_vars": {"focus_area": "performance", "detail_level": "basic"}}
effective_args = argparse.Namespace(
enhance_workflow=None, enhance_stage=None, var=None, workflow_dry_run=False
)
json_vars = config.get("workflow_vars", {})
if json_vars:
effective_args.var = list(effective_args.var or []) + [
f"{k}={v}" for k, v in json_vars.items()
]
assert "focus_area=performance" in effective_args.var
assert "detail_level=basic" in effective_args.var
def test_config_validator_accepts_workflow_fields(self, tmp_path):
"""ConfigValidator should not raise on workflow-related top-level fields."""
from skill_seekers.cli.config_validator import ConfigValidator
config = {
"name": "test",
"description": "Test with workflows",
"sources": [{"type": "documentation", "base_url": "https://example.com"}],
"workflows": ["security-focus"],
"workflow_stages": ["custom:Do something"],
"workflow_vars": {"key": "value"},
}
validator = ConfigValidator(config)
# Should not raise
assert validator.validate() is True
def test_empty_workflow_config_no_effect(self, tmp_path):
"""If no JSON workflow fields exist, effective_args remains unchanged."""
import argparse
config = {"name": "test", "description": "test", "sources": []}
effective_args = argparse.Namespace(
enhance_workflow=None, enhance_stage=None, var=None, workflow_dry_run=False
)
json_workflows = config.get("workflows", [])
json_stages = config.get("workflow_stages", [])
json_vars = config.get("workflow_vars", {})
has_json = bool(json_workflows or json_stages or json_vars)
assert not has_json
assert effective_args.enhance_workflow is None
assert effective_args.enhance_stage is None
assert effective_args.var is None
# Run tests
if __name__ == "__main__":
pytest.main([__file__, "-v"])