summaryrefslogtreecommitdiff
path: root/internal/hexaiaction/action_handler_test.go
blob: 92df3b137891f778fbb9175c92891f53129e64d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package hexaiaction

import (
	"context"
	"strings"
	"testing"

	"codeberg.org/snonux/hexai/internal/appconfig"
)

func TestActionHandlers_AreSelfRegistered(t *testing.T) {
	handlers := codeActionHandlers()
	for _, kind := range []ActionKind{
		ActionSkip,
		ActionRewrite,
		ActionDiagnostics,
		ActionDocument,
		ActionGoTest,
		ActionSimplify,
		ActionFixTypos,
		ActionCustom,
		ActionCustomPrompt,
	} {
		if _, ok := handlers[kind]; !ok {
			t.Fatalf("expected handler for %q", kind)
		}
	}
}

func TestActionHandlers_SnapshotDoesNotMutateRegistry(t *testing.T) {
	handlers := codeActionHandlers()
	delete(handlers, ActionSkip)
	if _, ok := lookupActionHandler(ActionSkip); !ok {
		t.Fatal("mutating handler snapshot changed registry")
	}
}

func TestExecuteAction_UnknownFallsBackToSelection(t *testing.T) {
	cfg := appconfig.App{}
	parts := InputParts{Selection: "original"}
	out, err := executeAction(context.Background(), ActionKind("missing"), parts, &cfg, fakeDoer{"ignored"}, nil, nil)
	if err != nil {
		t.Fatalf("executeAction: %v", err)
	}
	if out != "original" {
		t.Fatalf("expected fallback selection, got %q", out)
	}
}

func TestActionHandlerRegistryRejectsInvalidRegistrations(t *testing.T) {
	tests := map[string]func(*actionHandlerRegistry){
		"empty kind": func(registry *actionHandlerRegistry) {
			registry.register("", actionHandlerFunc(handleSkipAction))
		},
		"nil handler": func(registry *actionHandlerRegistry) {
			registry.register(ActionKind("nil"), nil)
		},
		"typed nil handler": func(registry *actionHandlerRegistry) {
			var handler actionHandlerFunc
			registry.register(ActionKind("typed-nil"), handler)
		},
		"duplicate kind": func(registry *actionHandlerRegistry) {
			registry.register(ActionKind("dup"), actionHandlerFunc(handleSkipAction))
			registry.register(ActionKind("dup"), actionHandlerFunc(handleSkipAction))
		},
	}

	for name, run := range tests {
		t.Run(name, func(t *testing.T) {
			defer func() {
				if recovered := recover(); recovered == nil {
					t.Fatal("expected panic")
				}
			}()
			run(newActionHandlerRegistry())
		})
	}
}

func TestActionHandlerRegistryNilPanicNamesKind(t *testing.T) {
	defer func() {
		recovered := recover()
		if recovered == nil {
			t.Fatal("expected panic")
		}
		if !strings.Contains(recovered.(string), "custom") {
			t.Fatalf("expected panic to name kind, got %v", recovered)
		}
	}()
	newActionHandlerRegistry().register(ActionCustom, nil)
}

func TestActionHandlerFuncNilExecuteReturnsError(t *testing.T) {
	var handler actionHandlerFunc
	_, err := handler.Execute(context.Background(), actionRequest{})
	if err == nil {
		t.Fatal("expected error")
	}
	if !strings.Contains(err.Error(), "nil action handler") {
		t.Fatalf("expected nil handler error, got %v", err)
	}
}