130 -> 96. forcetypeassert, predeclared and usetesting are now at zero.
Fanned out across four isolated worktrees partitioned by package so no
two workers could touch the same file: internal/tools (20 assertions),
internal/webui, internal/memory + internal/ratelimit, internal/secret.
Four stragglers outside those scopes fixed inline (internal/pairing,
internal/forge, internal/channels/telegram, internal/tools/mcp).
Unchecked type assertions became comma-ok with a t.Fatalf naming the
actual type. One site in a goroutine uses t.Errorf + return instead,
since t.Fatalf from a non-test goroutine is undefined behaviour.
predeclared renames chose names from meaning rather than adding a
suffix: max -> maxQueryLen, maxMatchLen, maxRequests, alphabetLen, and
nextBackoff's max -> ceiling. `real` -> `base` in the webui tests.
os.Setenv became t.Setenv, which restores on cleanup, so the paired
deferred Unsetenv calls went with it.
Note for future fan-outs: the agent worktrees branched from origin/main
(a7388a0), not from this branch, so every diff arrived based on a tree
without the phase 1 autofix. They were applied with `git apply --3way`
and the phase 1 changes to internal/memory/manager.go were verified to
survive — in particular the repaired `ctx, cancel =` assignment. A plain
`git apply` would have silently reverted it.
76 lines
2.6 KiB
Go
76 lines
2.6 KiB
Go
package telegram
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/go-telegram/bot/models"
|
|
|
|
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
|
)
|
|
|
|
func TestUpdateShowsComponentSectionsAndActions(t *testing.T) {
|
|
g := New("1:test", "", "", []int64{42}, slog.Default())
|
|
g.Updates = &updateStub{snapshot: releaseupdate.Snapshot{Components: []releaseupdate.Component{
|
|
{ID: "gateway", Label: "THE GATEWAY", Installed: "v0.1.0", Available: "v0.1.1", Changelog: "- Clearer help"},
|
|
{ID: "runtime", Label: "THE RUNTIME", Installed: "v0.1.0"},
|
|
}}}
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.updateHandler()(context.Background(), b, &models.Update{Message: &models.Message{
|
|
From: &models.User{ID: 42}, Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate}, Text: "/update",
|
|
}})
|
|
|
|
if len(*requests) != 1 || (*requests)[0].method != "sendMessage" {
|
|
t.Fatalf("requests = %#v, want update message", *requests)
|
|
}
|
|
text := (*requests)[0].form["text"]
|
|
for _, want := range []string{"Archie has an update available:", "--- THE GATEWAY ---", "v0.1.1 available", "- Clearer help", "--- THE RUNTIME ---", "No runtime changes"} {
|
|
if !strings.Contains(text, want) {
|
|
t.Errorf("update text missing %q: %q", want, text)
|
|
}
|
|
}
|
|
if !strings.Contains((*requests)[0].form["reply_markup"], updateApproveCallback) || !strings.Contains((*requests)[0].form["reply_markup"], updateDeferCallback) {
|
|
t.Errorf("update keyboard = %q", (*requests)[0].form["reply_markup"])
|
|
}
|
|
}
|
|
|
|
func TestUpdateCallbackRejectsUnauthorizedSender(t *testing.T) {
|
|
g := New("1:test", "", "", []int64{42}, slog.Default())
|
|
g.Updates = &updateStub{}
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.handleUpdateCallback(context.Background(), b, &models.Update{CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback", From: models.User{ID: 99}, Data: updateApproveCallback,
|
|
}})
|
|
|
|
stub, ok := g.Updates.(*updateStub)
|
|
if !ok {
|
|
t.Fatalf("g.Updates is %T, want *updateStub", g.Updates)
|
|
}
|
|
if stub.installCalls != 0 {
|
|
t.Error("unauthorized callback started an installation")
|
|
}
|
|
if len(*requests) != 1 || (*requests)[0].method != "answerCallbackQuery" {
|
|
t.Fatalf("requests = %#v, want authorization response", *requests)
|
|
}
|
|
}
|
|
|
|
type updateStub struct {
|
|
snapshot releaseupdate.Snapshot
|
|
installCalls int
|
|
}
|
|
|
|
func (s *updateStub) Check(context.Context, int64) (releaseupdate.Snapshot, error) {
|
|
return s.snapshot, nil
|
|
}
|
|
func (s *updateStub) Defer(context.Context, int64, releaseupdate.Snapshot) error { return nil }
|
|
func (s *updateStub) Install(_ context.Context, progress func(string)) error {
|
|
s.installCalls++
|
|
progress("Update started.")
|
|
return nil
|
|
}
|
|
func (*updateStub) CanInstall() bool { return true }
|