fix(qwen): align Go hooks with Qwen Code 0.20

This commit is contained in:
ZacharyZhang-NY committed 2026-07-20 16:06:52 -04:00
1 parent 4521cbe32f
commit dd72793cdb
18 files changed
+2921 -741

No files matched your search

+195 -58
View File
@@ -8,27 +8,70 @@ import (
"testing"
)
func TestQwenInstallRejectsMalformedSettingsBeforeWrites(t *testing.T) {
func TestQwenInstallRejectsMalformedKnownSettingsBeforeWrites(t *testing.T) {
tests := []struct {
name, source, pattern string
}{
{"malformed", "{ malformed", "parse Qwen Code settings"},
{"malformed", "{ malformed", "parse Qwen Code user settings"},
{"non-object", "[]", "JSON object"},
{"trailing comma", `{ "owner": true, }`, "trailing comma"},
{"duplicate root", `{ "hooks": {}, "hooks": {} }`, "duplicate"},
{"nested duplicate", `{ "hooks": { "PreToolUse": [{ "hooks": [], "hooks": [] }] } }`, "duplicate"},
{
"nested duplicate",
`{ "hooks": { "PreToolUse": [{ "hooks": [], "hooks": [] }] } }`,
"duplicate",
},
{"invalid disable flag", `{ "disableAllHooks": "yes" }`, "must be a boolean"},
{"invalid security", `{ "security": [] }`, "security"},
{
"invalid folder trust",
`{ "security": { "folderTrust": { "enabled": "yes" } } }`,
"must be a boolean",
},
{"hooks array", `{ "hooks": [] }`, "JSON object"},
{"unsupported event", `{ "hooks": { "UnknownEvent": [] } }`, "unsupported field"},
{"null hooks", `{ "hooks": null }`, "JSON object"},
{"null event", `{ "hooks": { "PreToolUse": null } }`, "must be an array"},
{"null group", `{ "hooks": { "PreToolUse": [null] } }`, "must be an object"},
{"invalid matcher", `{ "hooks": { "PreToolUse": [{ "matcher": "[", "hooks": [] }] } }`, "regular expression"},
{"invalid sequential", `{ "hooks": { "PreToolUse": [{ "sequential": "yes", "hooks": [] }] } }`, "must be a boolean"},
{"null handlers", `{ "hooks": { "PreToolUse": [{ "hooks": null }] } }`, "hooks array"},
{"missing command", `{ "hooks": { "PreToolUse": [{ "hooks": [{ "type": "command" }] }] } }`, "non-empty string"},
{"missing url", `{ "hooks": { "PreToolUse": [{ "hooks": [{ "type": "http" }] }] } }`, "non-empty string"},
{"invalid handler type", `{ "hooks": { "PreToolUse": [{ "hooks": [{ "type": "function", "command": "x" }] }] } }`, "command"},
{"invalid timeout", `{ "hooks": { "PreToolUse": [{ "hooks": [{ "type": "command", "command": "x", "timeout": "ten" }] }] } }`, "finite number"},
{
"invalid matcher type",
`{ "hooks": { "PreToolUse": [{ "matcher": 1, "hooks": [] }] } }`,
"matcher must be a string",
},
{
"invalid matcher regex",
`{ "hooks": { "PreToolUse": [{ "matcher": "[", "hooks": [] }] } }`,
"regular expression",
},
{
"invalid sequential",
`{ "hooks": { "PreToolUse": [{ "sequential": "yes", "hooks": [] }] } }`,
"must be a boolean",
},
{
"null handlers",
`{ "hooks": { "PreToolUse": [{ "hooks": null }] } }`,
"hooks array",
},
{
"missing command",
`{ "hooks": { "PreToolUse": [{ "hooks": [{ "type": "command" }] }] } }`,
"non-empty command",
},
{
"missing url",
`{ "hooks": { "PreToolUse": [{ "hooks": [{ "type": "http" }] }] } }`,
"non-empty url",
},
{
"settings function",
`{ "hooks": { "PreToolUse": [{ "hooks": [{ "type": "function" }] }] } }`,
"unsupported type",
},
{
"invalid timeout",
`{ "hooks": { "PreToolUse": [{ "hooks": [{ "type": "command", "command": "x", "timeout": "ten" }] }] } }`,
"finite number",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
@@ -40,68 +83,156 @@ func TestQwenInstallRejectsMalformedSettingsBeforeWrites(t *testing.T) {
if readQwenTestFile(t, fixture.configPath) != test.source {
t.Fatal("failed install changed Qwen Code settings")
}
for _, path := range []string{fixture.hookPath, fixture.runtimeConfig, fixture.privateKey} {
for _, path := range []string{
fixture.guardPath,
fixture.hookPath,
fixture.runtimeConfig,
fixture.privateKey,
} {
requireMissingQwenFile(t, path)
}
})
}
}
func TestQwenInstallValidatesRoutingAndManagedPathsBeforeWrites(t *testing.T) {
t.Run("unreadable home env", func(t *testing.T) {
fixture := prepareQwenFixture(t, qwenFixtureOptions{})
envPath := filepath.Join(fixture.qwenDir, ".env")
if err := os.MkdirAll(envPath, 0755); err != nil {
t.Fatalf("create unreadable env path: %v", err)
func TestQwenPreservesFutureEventsAndLimitsRegexSemantics(t *testing.T) {
settings := `{
"hooks": {
"FutureEvent": { "shape": "future" },
"UserPromptSubmit": [{ "matcher": "[", "hooks": [] }]
}
}`
fixture := prepareQwenFixture(t, qwenFixtureOptions{settings: qwenString(settings)})
installQwenFixture(t, fixture)
raw := readQwenTestFile(t, fixture.configPath)
for _, marker := range []string{"FutureEvent", `"matcher": "["`} {
if !strings.Contains(raw, marker) {
t.Fatalf("future-compatible setting %q was lost: %s", marker, raw)
}
err := fixture.plugin.Install(fixture.config)
if err == nil || !strings.Contains(err.Error(), "Qwen home environment") {
t.Fatalf("install error = %v", err)
}
requireMissingQwenFile(t, fixture.configPath)
requireMissingQwenFile(t, fixture.runtimeConfig)
})
}
}
func TestQwenInstallRejectsMalformedReadOnlySourceBeforeWrites(t *testing.T) {
tests := []struct {
name string
mutate func(*qwenFixture)
pattern string
name string
path func(*qwenFixture) string
}{
{
name: "missing guard",
mutate: func(fixture *qwenFixture) {
if err := os.Remove(fixture.guardPath); err != nil {
t.Fatalf("remove guard: %v", err)
}
},
pattern: "guard runtime is missing",
},
{
name: "invalid agent id",
mutate: func(fixture *qwenFixture) {
fixture.config.AgentID = "../agent"
},
pattern: "single non-empty path segment",
},
{
name: "guard outside managed directory",
mutate: func(fixture *qwenFixture) {
fixture.config.GuardScriptPath = filepath.Join(fixture.homeDir, "guard.js")
},
pattern: "managed agent directory",
},
{"system defaults", func(f *qwenFixture) string { return f.systemDefaults }},
{"workspace", func(f *qwenFixture) string { return f.workspaceConfig }},
{"system override", func(f *qwenFixture) string { return f.systemConfig }},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fixture := prepareQwenFixture(t, qwenFixtureOptions{})
test.mutate(fixture)
filePath := test.path(fixture)
writeOptionalQwenFile(t, filePath, qwenString("{ malformed"))
err := fixture.plugin.Install(fixture.config)
if err == nil || !strings.Contains(err.Error(), test.pattern) {
t.Fatalf("install error = %v, want substring %q", err, test.pattern)
if err == nil || !strings.Contains(err.Error(), "parse Qwen Code") {
t.Fatalf("install error = %v", err)
}
for _, path := range []string{fixture.configPath, fixture.hookPath, fixture.runtimeConfig, fixture.privateKey} {
if readQwenTestFile(t, filePath) != "{ malformed" {
t.Fatal("failed install changed the read-only source")
}
requireMissingQwenFile(t, fixture.guardPath)
})
}
}
func TestQwenInstallRejectsEffectiveDisableBeforeWrites(t *testing.T) {
fixture := prepareQwenFixture(t, qwenFixtureOptions{})
writeOptionalQwenFile(
t,
fixture.systemConfig,
qwenJSON(map[string]any{"disableAllHooks": true}),
)
err := fixture.plugin.Install(fixture.config)
if err == nil || !strings.Contains(err.Error(), "disableAllHooks") ||
!strings.Contains(err.Error(), fixture.systemConfig) {
t.Fatalf("install error = %v", err)
}
requireMissingQwenFile(t, fixture.guardPath)
requireMissingQwenFile(t, fixture.configPath)
}
func TestQwenTransactionProtectsEveryReadOnlySource(t *testing.T) {
tests := []struct {
name string
configure func(*testing.T, *qwenFixture)
path func(*qwenFixture) string
content string
}{
{
"home environment",
func(*testing.T, *qwenFixture) {},
func(f *qwenFixture) string { return filepath.Join(f.qwenDir, ".env") },
"QWEN_HOME=external\n",
},
{
"system defaults",
func(*testing.T, *qwenFixture) {},
func(f *qwenFixture) string { return f.systemDefaults },
"{}\n",
},
{
"workspace",
func(*testing.T, *qwenFixture) {},
func(f *qwenFixture) string { return f.workspaceConfig },
"{}\n",
},
{
"system override",
func(*testing.T, *qwenFixture) {},
func(f *qwenFixture) string { return f.systemConfig },
"{}\n",
},
{
"trusted folders",
func(t *testing.T, f *qwenFixture) {
writeOptionalQwenFile(t, f.configPath, qwenJSON(map[string]any{
"security": map[string]any{
"folderTrust": map[string]any{"enabled": true},
},
}))
},
func(f *qwenFixture) string { return f.trustedFolders },
"{}\n",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fixture := prepareQwenFixture(t, qwenFixtureOptions{
settings: qwenJSON(map[string]any{"owner": "user"}),
})
test.configure(t, fixture)
original := readQwenTestFile(t, fixture.configPath)
sourcePath := test.path(fixture)
mutated := false
fixture.plugin.rename = func(source, destination string) error {
if !mutated {
mutated = true
writeOptionalQwenFile(t, sourcePath, qwenString(test.content))
}
return os.Rename(source, destination)
}
err := fixture.plugin.Install(fixture.config)
if err == nil || !strings.Contains(err.Error(), "changed during") || !mutated {
t.Fatalf("transaction error = %v, mutated = %v", err, mutated)
}
if readQwenTestFile(t, fixture.configPath) != original {
t.Fatal("transaction rollback changed user settings")
}
if readQwenTestFile(t, sourcePath) != test.content {
t.Fatal("transaction rollback overwrote the external mutation")
}
for _, path := range []string{
fixture.guardPath,
fixture.hookPath,
fixture.runtimeConfig,
fixture.privateKey,
} {
requireMissingQwenFile(t, path)
}
requireNoQwenStagingFiles(t, fixture.homeDir)
})
}
}
@@ -120,13 +251,18 @@ func TestQwenTransactionRollsBackAllFilesAndCleansStaging(t *testing.T) {
return os.Rename(source, destination)
}
err := fixture.plugin.Install(fixture.config)
if err == nil || !strings.Contains(err.Error(), "Write Qwen Code installation") || !failed {
if err == nil || !strings.Contains(err.Error(), "Install Qwen Code hooks") || !failed {
t.Fatalf("transaction error = %v, failed = %v", err, failed)
}
if readQwenTestFile(t, fixture.configPath) != original {
t.Fatal("transaction rollback did not restore Qwen Code settings")
}
for _, path := range []string{fixture.hookPath, fixture.runtimeConfig, fixture.privateKey} {
for _, path := range []string{
fixture.guardPath,
fixture.hookPath,
fixture.runtimeConfig,
fixture.privateKey,
} {
requireMissingQwenFile(t, path)
}
requireNoQwenStagingFiles(t, fixture.homeDir)
@@ -152,7 +288,8 @@ func TestQwenStatusSurfacesMalformedRuntimeConfig(t *testing.T) {
if err := os.WriteFile(fixture.runtimeConfig, []byte("{ malformed"), 0600); err != nil {
t.Fatalf("corrupt runtime config: %v", err)
}
if _, err := fixture.plugin.Status(); err == nil || !strings.Contains(err.Error(), "parse Elydora runtime config") {
if _, err := fixture.plugin.Status(); err == nil ||
!strings.Contains(err.Error(), "parse Elydora runtime config") {
t.Fatalf("status error = %v", err)
}
}