Files
dingtalk-workspace-cli/internal/cli/schema_platform_gap_coverage_test.go
T
玉澜 b1cefba808 refactor(schema): retire reviewed registry; collector is the single identity source
Phase 3 of identity-deregistry. BuildEffectiveCommandRegistry now builds the
EffectiveCommandRegistry from CollectIdentitySpecs(root) instead of the
embedded reviewed registry, and the registry source is removed atomically:

- delete internal/cli/schema_command_registry/ (registry.json + products/),
  schema_command_registry.schema.json, the three //go:embed directives, and
  the registry-only loaders/validators (loadReviewedCommandRegistry,
  decodeCommandRegistry, ValidateCommandRegistrySource, shard assemble/merge
  helpers, ReviewedCommandRegistryMergedJSON/SourceHash, ReviewedCommandSpecs)
- keep CommandSpec/CommandRegistry/EffectiveCommandRegistry,
  newEffectiveCommandRegistry/indexCommandSpecs, and SourceHash; the
  collector-built effective hash is byte-identical to the reviewed one, so
  catalog surface_hash/source_hash are unchanged
- convert the Phase 2 dual-run gate into TestCollectedIdentityIsValidSingleSource:
  collected specs non-empty, no missing primaries, effective build succeeds,
  SourceHash stable across repeated collection walks
- generators: catalog -surface and agent-metadata -registry/-surface become
  fail-closed retired valves; outputguard no longer protects the registry
  paths; fetch_mcp_metadata derives interface refs from collected identity
  instead of the merged registry JSON
- retire scripts/policy/check-schema-command-registry.sh and its Makefile
  invocation; generate-schema/check-generated-drift now fail closed if
  schema_command_registry/ reappears
- update AGENTS.md, docs/reference.md, and in-code reviewed-input notes
2026-08-03 16:18:20 +08:00

218 lines
9.2 KiB
Go

// Copyright 2026 Alibaba Group
// Licensed under the Apache License, Version 2.0 (the "License");
package cli
import (
"fmt"
"strings"
"testing"
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/corecmd/contract"
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/corecmd/contractfinal"
"github.com/spf13/cobra"
)
func TestCrossPlatformCoveragePinnedMCPTextAndLegacyMetadataSource(t *testing.T) {
mcp := embeddedMCPMetadata{Tools: map[string]embeddedMCPToolMetadata{
"sample.run": {
Title: "MCP Title",
Description: "MCP Description",
Parameters: map[string]embeddedMCPParamMeta{"name": {Type: "string"}},
},
}}
entry := runtimeSchemaEntry{
ProductID: "sample",
ToolName: "run",
PrimaryCLIPath: "sample run",
CLIPath: "sample run",
Title: "Cobra Title",
Description: "Cobra Description",
}
title, description, source, _, err := runtimeToolTextMetadataFromMetadata(entry, runtimeSchemaMetadataSources{MCP: mcp})
if err != nil {
t.Fatalf("runtimeToolTextMetadataFromMetadata() error = %v", err)
}
if title == "" || description == "" {
t.Fatalf("pinned MCP text candidates must participate: title=%q description=%q source=%q", title, description, source)
}
legacyRoot := buildRuntimeSchemaTestRoot()
legacyBound, err := boundTestCommandRegistry(legacyRoot)
if err != nil {
t.Fatal(err)
}
for _, command := range legacyBound.Commands {
contractfinal.ClearRuntimeContractFinalForTest(command.PrimaryCommand)
}
entries, err := collectRuntimeSchemaEntriesFromBound(legacyBound)
if err != nil || len(entries) == 0 {
t.Fatalf("entries = %#v, %v", entries, err)
}
legacyEntry := entries[0]
legacyEntry.Title = "Legacy Title"
legacyEntry.Description = "Legacy Description"
tool, err := runtimeToolSpecFromLegacyMetadata(legacyEntry, runtimeSchemaMetadataSources{
MCP: embeddedMCPMetadata{Tools: map[string]embeddedMCPToolMetadata{
legacyEntry.ProductID + "." + legacyEntry.ToolName: {
Title: "Pinned",
Description: "Pinned desc",
Parameters: map[string]embeddedMCPParamMeta{},
},
}},
})
if err != nil {
t.Fatalf("runtimeToolSpecFromLegacyMetadata() error = %v", err)
}
if tool.MetadataSource != ProvenanceEmbeddedMCPMetadata {
t.Fatalf("legacy pinned MCP metadata_source = %q, want %q", tool.MetadataSource, ProvenanceEmbeddedMCPMetadata)
}
}
func TestCrossPlatformCoverageCompletenessAndDeliveryErrorBranches(t *testing.T) {
prevGroups := reviewedRuntimeSchemaExclusionGroups
t.Cleanup(func() { reviewedRuntimeSchemaExclusionGroups = prevGroups })
reviewedRuntimeSchemaExclusionGroups = []runtimeSchemaExclusionGroup{{
ID: "", Reason: "x", Reviewed: true, Commands: []string{"x"},
}}
if _, err := ReviewedRuntimeSchemaExclusions(); err == nil || !strings.Contains(err.Error(), "not reviewed") {
t.Fatalf("blank group id error = %v", err)
}
reviewedRuntimeSchemaExclusionGroups = []runtimeSchemaExclusionGroup{{
ID: "g", Reason: "reason", Reviewed: true, Commands: []string{" ", "ok"},
}}
if _, err := ReviewedRuntimeSchemaExclusions(); err == nil || !strings.Contains(err.Error(), "empty command") {
t.Fatalf("empty command error = %v", err)
}
reviewedRuntimeSchemaExclusionGroups = []runtimeSchemaExclusionGroup{{
ID: "g", Reason: "reason", Reviewed: true, Commands: []string{"dup", "dup"},
}}
if _, err := ReviewedRuntimeSchemaExclusions(); err == nil || !strings.Contains(err.Error(), "duplicate") {
t.Fatalf("duplicate exclusion error = %v", err)
}
reviewedRuntimeSchemaExclusionGroups = prevGroups
prevBuild := completenessBuildEffective
prevBind := completenessBindEffective
prevLoad := completenessLoadExclusions
prevRuntime := completenessRuntimeReport
prevDelivery := completenessDeliveryReport
t.Cleanup(func() {
completenessBuildEffective = prevBuild
completenessBindEffective = prevBind
completenessLoadExclusions = prevLoad
completenessRuntimeReport = prevRuntime
completenessDeliveryReport = prevDelivery
})
root := &cobra.Command{Use: "dws"}
completenessBuildEffective = func(*cobra.Command) (EffectiveCommandRegistry, error) {
return EffectiveCommandRegistry{}, fmt.Errorf("build boom")
}
if err := ValidateRuntimeSchemaCompleteness(root); err == nil || !strings.Contains(err.Error(), "build boom") {
t.Fatalf("build error = %v", err)
}
completenessBuildEffective = func(*cobra.Command) (EffectiveCommandRegistry, error) {
return EffectiveCommandRegistry{}, nil
}
completenessBindEffective = func(*cobra.Command, EffectiveCommandRegistry) (BoundCommandRegistry, error) {
return BoundCommandRegistry{}, fmt.Errorf("bind boom")
}
if err := ValidateRuntimeSchemaCompleteness(root); err == nil || !strings.Contains(err.Error(), "bind boom") {
t.Fatalf("bind error = %v", err)
}
completenessBindEffective = func(*cobra.Command, EffectiveCommandRegistry) (BoundCommandRegistry, error) {
return BoundCommandRegistry{}, nil
}
completenessLoadExclusions = func() ([]RuntimeSchemaExclusion, error) {
return nil, fmt.Errorf("excl boom")
}
if err := validateResolvedRuntimeSchemaCompleteness(root, BoundCommandRegistry{}); err == nil || !strings.Contains(err.Error(), "excl boom") {
t.Fatalf("excl error = %v", err)
}
completenessLoadExclusions = func() ([]RuntimeSchemaExclusion, error) { return nil, nil }
completenessRuntimeReport = func(*cobra.Command, []RuntimeSchemaExclusion, BoundCommandRegistry) RuntimeSchemaCompletenessReport {
return RuntimeSchemaCompletenessReport{DeliveryErrors: []string{"delivery"}}
}
if err := validateResolvedRuntimeSchemaCompleteness(root, BoundCommandRegistry{}); err == nil || !strings.Contains(err.Error(), "delivery") {
t.Fatalf("delivery report error = %v", err)
}
completenessRuntimeReport = func(*cobra.Command, []RuntimeSchemaExclusion, BoundCommandRegistry) RuntimeSchemaCompletenessReport {
return RuntimeSchemaCompletenessReport{Missing: []string{"m"}}
}
if err := validateResolvedRuntimeSchemaCompleteness(root, BoundCommandRegistry{}); err == nil || !strings.Contains(err.Error(), "missing") {
t.Fatalf("missing report error = %v", err)
}
completenessRuntimeReport = func(*cobra.Command, []RuntimeSchemaExclusion, BoundCommandRegistry) RuntimeSchemaCompletenessReport {
return RuntimeSchemaCompletenessReport{InvalidExclusions: []string{"i"}}
}
if err := validateResolvedRuntimeSchemaCompleteness(root, BoundCommandRegistry{}); err == nil || !strings.Contains(err.Error(), "invalid") {
t.Fatalf("invalid report error = %v", err)
}
completenessRuntimeReport = func(*cobra.Command, []RuntimeSchemaExclusion, BoundCommandRegistry) RuntimeSchemaCompletenessReport {
return RuntimeSchemaCompletenessReport{StaleExclusions: []string{"s"}}
}
if err := validateResolvedRuntimeSchemaCompleteness(root, BoundCommandRegistry{}); err == nil || !strings.Contains(err.Error(), "stale") {
t.Fatalf("stale report error = %v", err)
}
completenessLoadExclusions = func() ([]RuntimeSchemaExclusion, error) {
return nil, fmt.Errorf("delivery excl boom")
}
if err := validateResolvedSchemaCatalogDeliveryCompleteness(root, BoundCommandRegistry{}, SchemaCatalogSnapshot{}); err == nil || !strings.Contains(err.Error(), "delivery excl boom") {
t.Fatalf("delivery excl error = %v", err)
}
completenessLoadExclusions = func() ([]RuntimeSchemaExclusion, error) { return nil, nil }
prevIface := loadCatalogValidateInterfaces
prevProv := loadCatalogValidateProvenance
t.Cleanup(func() {
loadCatalogValidateInterfaces = prevIface
loadCatalogValidateProvenance = prevProv
})
loadCatalogValidateInterfaces = func(SchemaRegistry) error { return nil }
loadCatalogValidateProvenance = func(SchemaRegistry) error { return nil }
registry := SchemaRegistry{
Source: SchemaSourceRuntimeAssembled,
Products: []ProductSpec{{
ID: "sample",
Tools: []ToolSpec{{
Identity: contract.ToolIdentitySpec{
CLIPath: "sample run", CanonicalPath: "sample.run", ProductID: "sample",
Name: "run", Path: "sample.run", PrimaryCLIPath: "sample run",
},
Safety: contract.SafetySpec{Effect: "read", Risk: "low", Confirmation: "not_required", Idempotency: "yes"},
Selection: contract.SelectionSpec{AgentSummary: "s"},
}},
}},
}
payload, err := registry.ToSnapshotPayload()
if err != nil {
t.Fatal(err)
}
snapshot := SchemaCatalogSnapshot{
Version: SchemaCatalogSnapshotVersion,
Catalog: payload.Catalog,
Tools: payload.Tools,
}
snapshot.SourceHash = schemaCatalogSnapshotHash(snapshot)
for _, tc := range []struct {
name string
report RuntimeSchemaCompletenessReport
want string
}{
{"snap", RuntimeSchemaCompletenessReport{DeliveryErrors: []string{"snap"}}, "snap"},
{"missing", RuntimeSchemaCompletenessReport{Missing: []string{"m"}}, "missing"},
{"invalid", RuntimeSchemaCompletenessReport{InvalidExclusions: []string{"i"}}, "invalid"},
{"stale", RuntimeSchemaCompletenessReport{StaleExclusions: []string{"s"}}, "stale"},
} {
completenessDeliveryReport = func(*cobra.Command, loadedSchemaCatalog, []RuntimeSchemaExclusion, BoundCommandRegistry) RuntimeSchemaCompletenessReport {
return tc.report
}
if err := validateSchemaCatalogDeliveryCompletenessFromBound(root, BoundCommandRegistry{}, snapshot, nil); err == nil || !strings.Contains(err.Error(), tc.want) {
t.Fatalf("%s error = %v", tc.name, err)
}
}
}