Files
dingtalk-workspace-cli/internal/cli/fortest.go
T
玉澜 225a073d89 fix(cli): close tool search self-review findings
- SearchSubqueries returned a nil candidate slice on the
  decomposed_exact_filtered refusal, so that one strategy encoded
  "candidates":null while every other path encoded []; the refusal now
  carries an empty slice and a marshal assertion pins the wire shape
- the memoized delivery engine was never invalidated: it indexes one
  Catalog generation and caches its source/surface hashes, but
  RegisterSchemaSourceRoot -> resetDeliverySchemaCatalogState left the
  memo installed, so a re-registered root could be served stale hashes.
  Catalog delivery reset now clears it, with a ForTest wrapper and a
  regression test for the rebuild
- dangerous filter Unicode is rejected before the effect vocabulary and
  Catalog product checks, so a control/Bidi character returns
  dangerous_filter_unicode instead of being echoed back through
  invalid_effect; the filter list is an ordered slice, not a map, so the
  reported field name is deterministic
- dws schema help and RFC 5.8 record the path-form boundary: multi-token
  args resolve as a CLI path, a canonical path must stay one argument
- --explain and ToolSearchScoreBreakdown declare that the reported score
  is the raw value while ordering uses the quantized key
- --limit/--candidate-limit help state that 0 means "use the default",
  which was already the behavior but read as an asymmetry against the
  rejected negative values
- bootstrap seeds become named constants and are published in both
  evaluation reports (bootstrap_samples/bootstrap_seed/
  recovery_bootstrap_seed) so an interval is reproducible without
  reading the source
- dingtalk-shared documents the cross-product summary --dry-run flag and
  that its command list is a plan, not business data
2026-08-17 17:21:10 +08:00

98 lines
3.4 KiB
Go

// Copyright 2026 Alibaba Group
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cli
import (
"sync/atomic"
"github.com/spf13/cobra"
)
// Test helpers for this package's lazy delivery state. Production code must
// not call these; the ForTest suffix is the boundary. Cross-package tests use
// the exported ones; in-package tests may use the unexported resetters.
func resetDeliverySchemaCatalogStateForTest() {
resetDeliverySchemaCatalogState()
}
func resetMetaByCLIPathStateForTest() {
resetSchemaDeliveryState()
}
func resetSchemaSourceRootAtomicForTest() {
schemaSourceRoot = atomic.Value{}
}
// resetDeliveryToolSearchEngineStateForTest drops the memoized default Tool
// Search engine so a test can rebuild it against a freshly installed Catalog.
func resetDeliveryToolSearchEngineStateForTest() {
resetDeliveryToolSearchEngineState()
}
// clearDeclaredDryRunCapabilitiesForTest resets the declared index (tests only).
func clearDeclaredDryRunCapabilitiesForTest() {
declaredDryRunCapabilities.Range(func(key, _ any) bool {
declaredDryRunCapabilities.Delete(key)
return true
})
}
func resetReviewedDryRunCapabilitiesLazyForTest() {
resetReviewedDryRunCapabilitiesLazy()
}
func setReviewedDryRunCapabilityGroupsForTest(groups []dryRunCapabilityGroup) func() {
previous := reviewedDryRunCapabilityGroups
reviewedDryRunCapabilityGroups = groups
resetReviewedDryRunCapabilitiesLazy()
return func() {
reviewedDryRunCapabilityGroups = previous
resetReviewedDryRunCapabilitiesLazy()
}
}
// InstallProductionSchemaAssemblyForTest installs the production
// assembleSchemaCatalogFromRoot delivery path with factory and clears lazy
// caches. Tests must Cleanup via RegisterSchemaSourceRoot(nil) or a restore
// helper that reinstalls their prior delivery stub.
func InstallProductionSchemaAssemblyForTest(factory func() *cobra.Command) {
RegisterSchemaSourceRoot(factory)
assembleDeliverySchemaCatalogFn = assembleSchemaCatalogFromRoot
resetDeliverySchemaCatalogStateForTest()
}
// DeliverySchemaAllPayloadForTest returns schema --all through the installed
// delivery loader (catalog_hash comes from Snapshot.SourceHash).
func DeliverySchemaAllPayloadForTest() (map[string]any, error) {
return deliverySchemaAllPayload()
}
// RestorePackageCLISchemaDeliveryForTest reinstalls the package-cli TestMain
// assembled-delivery stub after a production-assembly exercise. Outside package
// cli TestMain it clears the factory and resets lazy delivery state.
func RestorePackageCLISchemaDeliveryForTest() {
if restorePackageCLISchemaDeliveryHook != nil {
restorePackageCLISchemaDeliveryHook()
return
}
storeSchemaSourceRootFn(nil)
assembleDeliverySchemaCatalogFn = assembleSchemaCatalogFromRoot
resetSchemaDeliveryState()
}
// restorePackageCLISchemaDeliveryHook is installed by package-cli TestMain so
// external cli_test helpers can restore the assembled-delivery stub.
var restorePackageCLISchemaDeliveryHook func()