Files
larksuite-cli/shortcuts/apps/plugin_instance_delete_test.go
anguohui e9f2da086f feat: add plugin package and instance management commands for apps domain
Add 8 new shortcut commands under `lark-cli apps`:

Plugin package management (aligned with fullstack-cli):
- +plugin-install: download tgz, extract to node_modules, update package.json
- +plugin-uninstall: remove from node_modules and package.json actionPlugins
- +plugin-list: list declared plugins with installation status

Plugin instance CRUD (aligned with feida-ai):
- +plugin-instance-create: validate + write capability JSON with formValue validation
- +plugin-instance-update: merge mutable fields, re-validate formValue
- +plugin-instance-delete: idempotent file removal
- +plugin-instance-get: read capability JSON
- +plugin-instance-list: scan capabilities directory

Shared infrastructure (plugin_common.go):
- 4-level capabilities dir resolution (flag → env → .env.local MIAODA_APP_TYPE → detection)
- formValue validation ported from feida-ai (5 rules: forbidden Handlebars, paramsSchema
  type constraints, input ref existence, unconsumed params, array double-wrap auto-fix)
- tgz extraction with path traversal protection
- package.json actionPlugins management
- Install version check with mismatch warnings
2026-06-18 16:10:47 +08:00

82 lines
2.2 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package apps
import (
"encoding/json"
"os"
"path/filepath"
"testing"
)
func TestPluginInstanceDelete_Basic(t *testing.T) {
dir := setupPluginTestProject(t, "server")
capDir := filepath.Join(dir, "server", "capabilities")
writeTestCapJSON(t, capDir, "my-inst.json", map[string]interface{}{"id": "my-inst"})
factory, stdout, _ := newAppsExecuteFactory(t)
err := runAppsShortcut(t, AppsPluginInstanceDelete, []string{
"+plugin-instance-delete",
"--id", "my-inst",
"--project-path", dir,
"--format", "json",
"--as", "user",
}, factory, stdout)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var env map[string]interface{}
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
data, _ := env["data"].(map[string]interface{})
if data["deleted"] != true {
t.Errorf("deleted = %v, want true", data["deleted"])
}
if _, err := os.Stat(filepath.Join(capDir, "my-inst.json")); !os.IsNotExist(err) { //nolint:forbidigo
t.Error("capability file should have been deleted")
}
}
func TestPluginInstanceDelete_Idempotent(t *testing.T) {
dir := setupPluginTestProject(t, "server")
factory, stdout, _ := newAppsExecuteFactory(t)
err := runAppsShortcut(t, AppsPluginInstanceDelete, []string{
"+plugin-instance-delete",
"--id", "nonexistent",
"--project-path", dir,
"--format", "json",
"--as", "user",
}, factory, stdout)
if err != nil {
t.Fatalf("delete of nonexistent instance should be idempotent, got: %v", err)
}
var env map[string]interface{}
if err := json.Unmarshal(stdout.Bytes(), &env); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
data, _ := env["data"].(map[string]interface{})
if data["deleted"] != true {
t.Errorf("deleted = %v, want true", data["deleted"])
}
}
func TestPluginInstanceDelete_MissingID(t *testing.T) {
dir := setupPluginTestProject(t, "server")
factory, stdout, _ := newAppsExecuteFactory(t)
err := runAppsShortcut(t, AppsPluginInstanceDelete, []string{
"+plugin-instance-delete",
"--project-path", dir,
"--as", "user",
}, factory, stdout)
if err == nil {
t.Fatal("expected error when --id is missing")
}
}