|
| 1 | +package skills |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// TestRestartSkill_Triggers_ScoredMatcher verifies the scored matcher |
| 9 | +// correctly triggers the restart-odek-telegram skill for common user |
| 10 | +// inputs like "rebuild and restart", "restart the bot", etc. |
| 11 | +func TestRestartSkill_Triggers_ScoredMatcher(t *testing.T) { |
| 12 | + sm := NewSkillManager("", "/root/.odek/skills") |
| 13 | + sm.Reload() |
| 14 | + |
| 15 | + // The restart skill should be in the Lazy pool (auto_load: false) |
| 16 | + found := false |
| 17 | + for _, s := range sm.Result.Lazy { |
| 18 | + if s.Name == "restart-odek-telegram" { |
| 19 | + found = true |
| 20 | + break |
| 21 | + } |
| 22 | + } |
| 23 | + if !found { |
| 24 | + t.Fatal("restart-odek-telegram not found in Lazy pool — is auto_load: false?") |
| 25 | + } |
| 26 | + |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + input string |
| 30 | + wantHit bool |
| 31 | + }{ |
| 32 | + {"rebuild and restart", "rebuild and restart", true}, |
| 33 | + {"restart the bot", "restart the bot", true}, |
| 34 | + {"bounce odek", "bounce odek", true}, |
| 35 | + {"redeploy telegram", "redeploy telegram", true}, |
| 36 | + {"unrelated task", "explain how DNS works", false}, |
| 37 | + {"partial match — only topic", "odek telegram", true}, // topic words match |
| 38 | + {"partial match — only action", "restart it now", true}, // action word matches |
| 39 | + } |
| 40 | + |
| 41 | + for _, tt := range tests { |
| 42 | + t.Run(tt.name, func(t *testing.T) { |
| 43 | + matched := sm.ScoredMatcher.MatchSkills(tt.input, 5) |
| 44 | + got := false |
| 45 | + for _, m := range matched { |
| 46 | + if m.Name == "restart-odek-telegram" { |
| 47 | + got = true |
| 48 | + break |
| 49 | + } |
| 50 | + } |
| 51 | + if got != tt.wantHit { |
| 52 | + t.Errorf("MatchSkills(%q) hit=%v, want %v. matched: %v", |
| 53 | + tt.input, got, tt.wantHit, skillNames(matched)) |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// TestRestartSkill_BodyContainsScript verifies the loaded skill content |
| 60 | +// actually directs the agent to use the build-and-restart script, |
| 61 | +// not manual build or kill commands. |
| 62 | +func TestRestartSkill_BodyContainsScript(t *testing.T) { |
| 63 | + sm := NewSkillManager("", "/root/.odek/skills") |
| 64 | + sm.Reload() |
| 65 | + |
| 66 | + matched := sm.ScoredMatcher.MatchSkills("rebuild and restart", 1) |
| 67 | + if len(matched) == 0 { |
| 68 | + t.Fatal("no skill matched for 'rebuild and restart'") |
| 69 | + } |
| 70 | + |
| 71 | + body := matched[0].Body |
| 72 | + checks := []struct { |
| 73 | + desc string |
| 74 | + contain string |
| 75 | + }{ |
| 76 | + {"references the script", "build-and-restart-telegram.sh"}, |
| 77 | + {"uses --restart-only flag", "--restart-only"}, |
| 78 | + {"uses nohup to detach", "nohup"}, |
| 79 | + {"warns against go build .", "go build"}, |
| 80 | + } |
| 81 | + |
| 82 | + for _, c := range checks { |
| 83 | + if !strings.Contains(strings.ToLower(body), strings.ToLower(c.contain)) { |
| 84 | + t.Errorf("skill body missing %q: %s", c.contain, c.desc) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TestRestartSkill_NoAndLock regression: the original trie required |
| 90 | +// BOTH topic AND action. "restart" alone (action keyword) should now |
| 91 | +// trigger via scored matcher. |
| 92 | +func TestRestartSkill_NoAndLock(t *testing.T) { |
| 93 | + skill := Skill{ |
| 94 | + Name: "restart-odek-telegram", |
| 95 | + Trigger: SkillTrigger{ |
| 96 | + TopicKeywords: []string{"odek", "telegram", "bot", "restart", "deploy", "rebuild"}, |
| 97 | + ActionKeywords: []string{"restart", "redeploy", "rebuild", "bounce"}, |
| 98 | + }, |
| 99 | + Description: "Restart the odek Telegram bot", |
| 100 | + Body: "Run build-and-restart-telegram.sh", |
| 101 | + } |
| 102 | + |
| 103 | + // Old trie: topic AND action required |
| 104 | + trieIdx := BuildTriggerIndex([]Skill{skill}) |
| 105 | + trieMatch := trieIdx.MatchSkills("restart now", 5) |
| 106 | + t.Logf("Trie match (should fail — no AND-lock fix): %v", skillNames(trieMatch)) |
| 107 | + |
| 108 | + // New scored matcher: OR logic |
| 109 | + scored := NewScoredMatcher([]Skill{skill}, DefaultScoredConfig()) |
| 110 | + scoredMatch := scored.MatchSkills("restart now", 5) |
| 111 | + t.Logf("Scored match (should succeed): %v", skillNames(scoredMatch)) |
| 112 | + |
| 113 | + if len(trieMatch) > 0 { |
| 114 | + t.Log("Note: trie matched — topic OR action might have been fixed") |
| 115 | + } |
| 116 | + if len(scoredMatch) == 0 { |
| 117 | + t.Error("scored matcher should match on action keyword alone ('restart')") |
| 118 | + } |
| 119 | +} |
0 commit comments