# Marketplace Installation Tests with OTEL Validation
# Converts marketplace_validation.rs installation tests to CLNRM format
# Proves execution via 7-layer fake-green detection

[meta]
name = "marketplace_install_with_otel_proof"
version = "1.0.0"
description = "Comprehensive marketplace installation tests with OTEL span validation"

[otel]
exporter = "otlp"
endpoint = "http://localhost:4318"
service_name = "ggen-marketplace-install-tests"

[otel.resource]
"service.namespace" = "ggen-marketplace"
"test.suite" = "install"

[service.ggen]
plugin = "generic_container"
image = "rust:1.75"
workdir = "/app"
volumes = [{ host_path = "./", container_path = "/app" }]
env.RUST_LOG = "info,ggen=debug"
env.OTEL_EXPORTER_OTLP_ENDPOINT = "http://localhost:4318"

# ============================================================================
# PACKAGE RESOLUTION TESTS
# ============================================================================

[[scenario]]
name = "test_marketplace_package_resolve"
description = "Test basic package resolution"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_package_resolve \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "ggen.registry.resolve"
attrs.all = {
  "package.id" = "test-package",
  "package.version" = "1.0.0",
  "git.url" = "https://github.com/test/test-package.git",
  "git.rev" = "v1.0.0"
}

[[scenario.expect.span]]
name = "registry.fetch_index"
attrs.all = { "status" = "success" }

[scenario.expect.status]
all = "OK"

[scenario.expect.graph]
must_have_parent = [
  { child = "ggen.registry.resolve", parent = "test_marketplace_package_resolve" }
]

[scenario.expect.temporal]
must_follow = [
  { later = "ggen.registry.resolve", earlier = "registry.fetch_index" }
]

[[scenario]]
name = "test_marketplace_package_resolve_specific_version"
description = "Test resolving specific package versions"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_package_resolve_specific_version \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "ggen.registry.resolve"
attrs.all = {
  "package.id" = "multi-version-pkg",
  "package.version" = "1.0.0",
  "git.rev" = "v1.0.0"
}

[[scenario.expect.span]]
name = "ggen.registry.resolve"
attrs.all = {
  "package.id" = "multi-version-pkg",
  "package.version" = "2.0.0",
  "git.rev" = "v2.0.0"
}

[[scenario.expect.span]]
name = "ggen.registry.resolve"
attrs.all = {
  "package.id" = "multi-version-pkg",
  "package.version" = "2.0.0",  # Latest version
  "version.latest" = "true"
}

[scenario.expect.status]
all = "OK"

[scenario.expect.counts]
# Should have exactly 3 resolve operations
"ggen.registry.resolve" = { exact = 3 }

[[scenario]]
name = "test_marketplace_package_metadata_validation"
description = "Validate package metadata completeness"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_package_metadata_validation \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "registry.fetch_index"
attrs.all = { "status" = "success", "packages.count.gte" = 1 }

[[scenario.expect.span]]
name = "ggen.registry.validate_metadata"
attrs.all = {
  "package.id" = "validated-package",
  "metadata.complete" = "true",
  "has.name" = "true",
  "has.description" = "true",
  "has.tags" = "true",
  "has.license" = "true",
  "has.author" = "true"
}

[scenario.expect.status]
all = "OK"

# ============================================================================
# UPDATE AND VERSION MANAGEMENT TESTS
# ============================================================================

[[scenario]]
name = "test_marketplace_check_updates"
description = "Test checking for package updates"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_check_updates \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "ggen.registry.check_updates"
attrs.all = {
  "package.id" = "update-pkg",
  "current.version" = "1.0.0",
  "latest.version" = "2.0.0",
  "update.available" = "true"
}

[[scenario.expect.span]]
name = "ggen.registry.check_updates"
attrs.all = {
  "package.id" = "update-pkg",
  "current.version" = "1.1.0",
  "latest.version" = "2.0.0",
  "update.available" = "true"
}

[[scenario.expect.span]]
name = "ggen.registry.check_updates"
attrs.all = {
  "package.id" = "update-pkg",
  "current.version" = "2.0.0",
  "latest.version" = "2.0.0",
  "update.available" = "false"
}

[scenario.expect.status]
all = "OK"

[scenario.expect.counts]
# Should have exactly 3 update checks
"ggen.registry.check_updates" = { exact = 3 }

# ============================================================================
# PACKAGE LIST TESTS
# ============================================================================

[[scenario]]
name = "test_marketplace_package_list_performance"
description = "Test listing 200 packages performance"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_package_list_performance \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "ggen.registry.list_packages"
attrs.all = { "packages.count" = 200 }
# Listing must complete within 1 second
duration_ms.max = 1000

[scenario.expect.status]
all = "OK"

# ============================================================================
# CATEGORY AND STATISTICS TESTS
# ============================================================================

[[scenario]]
name = "test_marketplace_registry_categories"
description = "Test listing package categories"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_registry_categories \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "ggen.registry.list_categories"
attrs.all = { "categories.count" = 2 }

[[scenario.expect.span]]
name = "ggen.registry.list_categories"
attrs.any = { "categories.contains" = ["web", "cli"] }

[scenario.expect.status]
all = "OK"

[[scenario]]
name = "test_marketplace_package_statistics"
description = "Test package download statistics"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_package_statistics \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "registry.fetch_index"
attrs.all = { "packages.count" = 5 }

[scenario.expect.status]
all = "OK"

# Verify download counts for all packages
[[scenario.expect.span]]
name = "ggen.registry.validate_statistics"
attrs.all = { "downloads.validated" = "true" }

# ============================================================================
# ERROR HANDLING TESTS
# ============================================================================

[[scenario]]
name = "test_marketplace_nonexistent_package"
description = "Test error handling for non-existent package"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_nonexistent_package \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "ggen.registry.resolve"
attrs.all = {
  "package.id" = "nonexistent-package",
  "error" = "PackageNotFound"
}
# This span should have error status
status = "ERROR"

# But test should pass (error is expected)
[scenario.expect.status]
test = "OK"

[[scenario]]
name = "test_marketplace_invalid_version"
description = "Test error handling for invalid version"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_invalid_version \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "ggen.registry.resolve"
attrs.all = {
  "package.id" = "version-test",
  "package.version" = "99.99.99",
  "error" = "VersionNotFound"
}
status = "ERROR"

[scenario.expect.status]
test = "OK"

[[scenario]]
name = "test_marketplace_empty_registry"
description = "Test operations on empty registry"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_empty_registry \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "ggen.market.search"
attrs.all = { "query" = "anything", "results.count" = 0 }

[[scenario.expect.span]]
name = "ggen.registry.list_packages"
attrs.all = { "packages.count" = 0 }

[scenario.expect.status]
all = "OK"

[[scenario]]
name = "test_marketplace_malformed_index_handling"
description = "Test error handling for malformed index"
service = "ggen"
run = """
cargo test --test marketplace_tests_main \
  integration::marketplace_validation::test_marketplace_malformed_index_handling \
  -- --nocapture
"""

[[scenario.expect.span]]
name = "registry.fetch_index"
attrs.all = { "error" = "ParseError" }
status = "ERROR"

[scenario.expect.status]
test = "OK"

# ============================================================================
# REPORT GENERATION
# ============================================================================

[report]
output = "tests/clnrm/reports/marketplace_install_report.json"
format = "json"
include_spans = true
include_metrics = true

[report.sha256]
enabled = true
algorithm = "SHA-256"
digest_file = "tests/clnrm/reports/marketplace_install_digest.txt"

[report.metrics]
total_resolves = { path = "$.scenarios[*].spans[?(@.name=='ggen.registry.resolve')].length()" }
avg_resolve_duration_ms = { path = "$.scenarios[*].spans[?(@.name=='ggen.registry.resolve')].duration_ms.avg()" }
success_rate = { path = "$.scenarios[*].status[?(@=='OK')].length() / $.scenarios.length()" }
error_handling_tests = { path = "$.scenarios[*].spans[?(@.status=='ERROR')].length()" }

# ============================================================================
# 7-LAYER FAKE-GREEN DETECTION SUMMARY
# ============================================================================

[validation]
layers = [
  "lifecycle_events",    # 1. Container start/exec events
  "span_graphs",         # 2. Parent-child relationships
  "span_counts",         # 3. Operation count verification
  "temporal_ordering",   # 4. must_precede/must_follow
  "window_containment",  # 5. Child spans within parent duration
  "status_validation",   # 6. All spans = "OK" (or expected ERROR)
  "hermeticity"          # 7. No external services
]

[validation.lifecycle_events]
required = ["container.start", "container.exec", "test.run"]

[validation.span_graphs]
verify_parent_child = true

[validation.span_counts]
verify_exact_counts = true

[validation.temporal_ordering]
verify_must_follow = true

[validation.window_containment]
verify_child_within_parent = true

[validation.status_validation]
all_must_be_ok = true
allow_expected_errors = true

[validation.hermeticity]
no_external_services = true
allowed_localhost = ["4318"]
