* feat(workflow): post line-anchored review comments with suggested fixes Bead archie-core-q9au (docs/prds/inline-review.md). A review's findings reached the operator only as a flat list in the PR body, so the author had to map every finding back to its line by hand and apply mechanical fixes by hand. Now the line-anchored findings are also posted as inline review comments, carrying a one-click suggestion when the reviewer could state the fix. Domain (internal/domain/workflow): - ReviewFinding gains Suggestion; Validate rejects one on anything but a confirmed finding, so the agent's record_finding call is refused with something it can act on. Validate splits into validateAnchor / validateClassification / validateDisposition, which also brings it back under the cyclop limit the gate enforces. - StagePostReviewComments runs after StageOpenPR in the implement workflow. It posts only line-anchored findings (Line > 0); a whole-file finding has no line to attach to and stays in the body list, which remains the complete record. It is best-effort: the PR is open and the body already lists every finding, so a failed comment is logged, never a park -- the same reasoning as OpenPR's existing best-effort LinkBranch call. - The suggestion fence is gated on Verdict == confirmed where the fence is written, because it is a one-click apply: a wrong suggestion breaks the code and the author has to notice. Forge (internal/forge): ReviewCommentWriter + GitHub/Gitea implementations, type-asserted like PullRequestReader so a forge that cannot is skipped rather than faked (NoopForge is pinned as not satisfying it). GitHub loops one CreateComment per comment and joins the failures, so one refused anchor cannot drop the rest; Gitea carries them on a single COMMENT-state review. Two deliberate departures from the PRD's sketch, both recorded in it: - The interface is batch (CreateReviewComments), not one call per comment: Gitea's only inline shape is one submitted review, and the set then crosses NATS once instead of once per finding. - The head SHA is resolved inside each forge implementation rather than by the caller. The stage runs in archie-agent, which holds no forge credentials and has no PullRequestReader; the daemon-side forgerpc handler is the only place that does. Transport (internal/forgerpc): workflow.Forger gains the method, so the agent reaches the daemon over the new create_review_comments subject; the server type-asserts the writer and answers an incapable forge with an error the stage logs. This is the one proxied method whose absence on an image skew does not park a task. The reviewer prompt and record_finding schema ask for a suggestion on a confirmed, mechanically fixable defect only. Regenerating the wfextract symbol table -- the documented way a new exported workflow symbol reaches interpreted stage plugins -- also picked up symbols that were missing from the committed table (the Status* and Source* constants, ReviewCheck, Store, Task, and the _Store wrapper). Nil-guards were re-applied to every wrapper afterwards, as that file's header requires. Evidence: task check exit 0 (incl. 229 UI tests). Three mutations of the new stage logic each fail a test that names the behaviour -- dropping the confirmed-only gate, dropping the Line > 0 filter, and letting a posting failure escape -- so the tests discriminate rather than passing vacuously. * fix(build): drop the Dockerfile pin assertion The guard fetched golangci-lint's go.mod at image build time to fail the build when GOLANGCI_LINT_VERSION and GOFUMPT_VERSION disagreed. It only ever tripped on self-inflicted pin drift, but it added a network fetch and a new failure mode to the release build, which is not where that belongs. The pins stay correct (golangci-lint 2.13.2, whose vendored gofumpt v0.11.0 equals GOFUMPT_VERSION) and the comment above them still states the invariant, so the reasoning survives without the build-time machinery. The pin change itself reaches the agent image through the normal release path: CI rebuilds and pushes archie-agent on an archie/vX.Y.Z release, and a gateway-only release deliberately does not. * feat(workflow): add the remediate workflow for PR review remediation Implements step 5 of docs/prds/pr-review-remediation.md: a remediate workflow that resumes the task's existing worktree onto its PR branch (worktree.Manager.Resume, not Prepare's refresh), runs a builder agent against the review's actionable comments, commits and pushes to the same branch, and replies via the forge's ReplyToReview. Task.RetryCount doubles as the remediation round counter, capped by the repo's max_retries; the round-cap stage parks the task and posts one comment when it's hit. Task gains a ReviewPayload field (JSON-encoded review unit) with full store/proto/staterpc plumbing. workflow.Forger gains Comment and ReplyToReview, proxied to archie-agent via a new forgerpc RPC. Resume and its Dir lookup are added as an unexported optional Trees capability rather than widening the exported interface, which is already at golangci-lint's interfacebloat cap and is projected into wfextract's Yaegi symbol table for repository-authored stage plugins. The daemon-side reaction consumer that dispatches into this workflow (subscribing ARCHIE_REACTIONS, grouping comments by review, enforcing per-task serialization) is tracked separately in archie-core-8li9.4. * feat(messaging): extract standalone messaging service binary and process composition * feat(messaging): adapt channel frontends to consume Gateway ChatContract * feat(messaging): remove in-process channels and Router from daemon bootstrap Telegram, email and webhook now run only in the Messaging Service, which reaches this process through the Gateway's ChatContract. The daemon builds no channel and no gateway.Router, so the wiring that existed only to serve them goes with it: setupGateways, setupEmailGateway, setupWebhookGateway, setupTelegramGateway, buildTelegramRouter, makeTelegramReload and the release-announcement path helper. The seams those paths owned move to where they can still be honoured: - Channel ValidateConfig runs in archiemessaging.compose, which now returns an error, so a configured-but-invalid front-end fails the service rather than being dropped silently. - The inbound rate limiter moves to the Gateway process, which owns the only Router and therefore the only place a per-sender budget applies to every channel's turns. - The daemon's gateway readiness probe becomes a ContractProbe against the Gateway. It cannot observe a channel lifecycle it no longer runs, and a channel-count probe would have reported every daemon degraded. - telegramSetup is now chatSetup (in chat_setup.go), trimmed to the fields a chat turn runner actually reads. Telegram operator features that were wired only in the daemon (/update, release announcements, version reporting, SIGHUP reload) are not yet wired in the Messaging Service: archie-core-8cda.6.6. Channel lifecycle reporting: archie-core-8cda.6.8. The Gateway process builds no worktree manager, so review_pr is unadvertised on every channel: archie-core-8cda.6.7. * feat(messaging): re-wire the Telegram operator surface in the Messaging Service setupTelegramGateway satisfied eight telegram.Gateway seams from the daemon's own process. Each is resolved by where the fact it needs is owned, not by where it used to be built; the decision table is in docs/architecture/migration-decisions.md. Wired here: /update (built from this service's own [chat.telegram] check and install commands), the update-report relay (same sha256(bot_user)[:8] path the daemon wrote, so a pending report is found rather than written afresh), show_tool_calls, and /restart's config reload, which re-resolves the token and allowlist from this service's own file and refuses a reload that resolves to an empty token. /version is sourced from the Gateway: ChatSnapshot gains a Version field carrying the Gateway's rendered build block, read per call and bounded by the dependency timeout. cmd/archie-messaging deliberately takes no version ldflags, so it cannot report a partial upgrade as a matched one. RunningVersions and ReleaseAnnouncements stay nil. Both need a component's real running version, and the only versions this process can obtain are a different binary's stamps -- reporting them would manufacture exactly the false success the update check exists to catch, and the announcer without parseable versions is a no-op that reads as configured. archie-core-8cda.6.9 carries the contract that unblocks them. Dangerous stays nil: no composition has ever supplied a command authority. Enrich is dropped from the update service: componentInstallTypeEnricher read [nats], which this service must not decode, so the check command's own reported install type stands. * fix(workflow): guard inline review comments against head drift Two findings from the review of q9au. A comment's line numbers describe exactly one revision of the branch, but the forge resolved the pull request's head at posting time -- after the review had already read the worktree. A push in that window (a collaborator, a retry) meant the findings were anchored to whatever now occupied those lines, mislabelling instead of failing. The revision the review measured now travels with the comment set: OpenPR records it from the same worktree read it already performs (TaskContext.ReviewedHeadSHA), StagePostReviewComments threads it through Forger to forgerpc, and the forge refuses the whole set once the head it reads has moved off it -- which the best-effort stage logs, the PR body remaining the record. An unmeasured revision posts unverified rather than dropping every finding, so a worktree that cannot report one degrades instead of regressing. Suggestion was documented single-line only, because GitHub applies a suggestion fence up to its first line and a multi-line replacement would silently corrupt the file. Only the verdict was validated, so a multi-line suggestion reached the renderer and posted as that truncated fence. Validate now rejects an embedded newline, which makes record_finding refuse it with feedback the model can act on instead of the reviewer discovering the corruption later.
1041 lines
32 KiB
Go
1041 lines
32 KiB
Go
package telegram
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-telegram/bot/models"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
)
|
|
|
|
// ── DangerousCommandAuthority stub ─────────────────────────────
|
|
|
|
type dangerousStub struct {
|
|
mu sync.Mutex
|
|
stopCalls []string
|
|
rollbackCalls []int
|
|
checkpoints []CheckpointInfo
|
|
stopErr error
|
|
rollbackErr error
|
|
rollbackResult string
|
|
checkpointsErr error
|
|
}
|
|
|
|
func (s *dangerousStub) StopProcess(_ context.Context, spec string) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.stopCalls = append(s.stopCalls, spec)
|
|
return s.stopErr
|
|
}
|
|
|
|
func (s *dangerousStub) Rollback(_ context.Context, num int) (string, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.rollbackCalls = append(s.rollbackCalls, num)
|
|
if s.rollbackErr != nil {
|
|
return "", s.rollbackErr
|
|
}
|
|
return s.rollbackResult, nil
|
|
}
|
|
|
|
func (s *dangerousStub) ListCheckpoints(_ context.Context) ([]CheckpointInfo, error) {
|
|
if s.checkpointsErr != nil {
|
|
return nil, s.checkpointsErr
|
|
}
|
|
return s.checkpoints, nil
|
|
}
|
|
|
|
// messageText returns the text content from a Telegram request,
|
|
// checking both regular text messages and rich messages.
|
|
func messageText(requests []telegramRequest, index int) string {
|
|
if index >= len(requests) {
|
|
return ""
|
|
}
|
|
req := requests[index]
|
|
if text := req.form["text"]; text != "" {
|
|
return text
|
|
}
|
|
return req.form["rich_message"]
|
|
}
|
|
|
|
// ── Tests ──────────────────────────────────────────────────────
|
|
|
|
func TestRollbackListsCheckpoints(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
now := time.Date(2026, 1, 15, 10, 30, 0, 0, time.UTC)
|
|
stub := &dangerousStub{
|
|
checkpoints: []CheckpointInfo{
|
|
{Number: 3, Timestamp: now, Label: "After gate fix", Size: "12 MB"},
|
|
{Number: 2, Timestamp: now.Add(-1 * time.Hour), Label: "Baseline", Size: "10 MB"},
|
|
{Number: 1, Timestamp: now.Add(-2 * time.Hour), Label: "Initial checkout", Size: "8 MB"},
|
|
},
|
|
}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/rollback",
|
|
},
|
|
})
|
|
|
|
if len(*requests) == 0 {
|
|
t.Fatal("rollback list sent no response")
|
|
}
|
|
text := messageText(*requests, 0)
|
|
for _, want := range []string{"Available checkpoints", "3 — 2026-01-15 10:30", "12 MB", "After gate fix"} {
|
|
if !strings.Contains(text, want) {
|
|
t.Errorf("checkpoint list missing %q: %s", want, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRollbackEmptyCheckpoints(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{} // no checkpoints
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/rollback",
|
|
},
|
|
})
|
|
|
|
text := messageText(*requests, 0)
|
|
if !strings.Contains(text, "No checkpoints") {
|
|
t.Errorf("expected 'No checkpoints' message, got: %s", text)
|
|
}
|
|
}
|
|
|
|
func TestRollbackCheckpointError(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{checkpointsErr: fmt.Errorf("storage offline")}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/rollback",
|
|
},
|
|
})
|
|
|
|
text := messageText(*requests, 0)
|
|
if !strings.Contains(text, "Could not list") {
|
|
t.Errorf("expected error message, got: %s", text)
|
|
}
|
|
}
|
|
|
|
func TestRollbackWithNumberRequiresApproval(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{
|
|
rollbackResult: "Restored to checkpoint 2: 15 files changed, 3 removed.",
|
|
}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/rollback 2",
|
|
},
|
|
})
|
|
|
|
if len(*requests) == 0 {
|
|
t.Fatal("rollback approval prompt sent no response")
|
|
}
|
|
text := (*requests)[0].form["text"]
|
|
if !strings.Contains(text, "Dangerous command") {
|
|
t.Errorf("expected dangerous command prompt, got: %s", text)
|
|
}
|
|
if !strings.Contains(text, "/rollback 2") {
|
|
t.Errorf("expected command text in prompt, got: %s", text)
|
|
}
|
|
if !strings.Contains(text, "expires in 10 minutes") {
|
|
t.Errorf("expected expiry notice, got: %s", text)
|
|
}
|
|
|
|
// Verify inline keyboard has three buttons.
|
|
var markup models.InlineKeyboardMarkup
|
|
if err := json.Unmarshal([]byte((*requests)[0].form["reply_markup"]), &markup); err != nil {
|
|
t.Fatalf("decode reply markup: %v", err)
|
|
}
|
|
if len(markup.InlineKeyboard) != 3 {
|
|
t.Fatalf("keyboard rows = %d, want 3 (Approve/Approve Permanently/Deny)", len(markup.InlineKeyboard))
|
|
}
|
|
|
|
buttons := []string{
|
|
markup.InlineKeyboard[0][0].Text,
|
|
markup.InlineKeyboard[1][0].Text,
|
|
markup.InlineKeyboard[2][0].Text,
|
|
}
|
|
if buttons[0] != "Approve this time" || buttons[1] != "Approve Permanently" || buttons[2] != "Deny" {
|
|
t.Errorf("button labels = %v, want [Approve this time, Approve Permanently, Deny]", buttons)
|
|
}
|
|
|
|
// Verify callback data prefix.
|
|
if !strings.HasPrefix(markup.InlineKeyboard[0][0].CallbackData, dangerousCmdPrefix+"approve:") {
|
|
t.Errorf("approve callback = %q, want prefix %s", markup.InlineKeyboard[0][0].CallbackData, dangerousCmdPrefix+"approve:")
|
|
}
|
|
if !strings.HasPrefix(markup.InlineKeyboard[1][0].CallbackData, dangerousCmdPrefix+"permanent:") {
|
|
t.Errorf("permanent callback = %q", markup.InlineKeyboard[1][0].CallbackData)
|
|
}
|
|
if !strings.HasPrefix(markup.InlineKeyboard[2][0].CallbackData, dangerousCmdPrefix+"deny:") {
|
|
t.Errorf("deny callback = %q", markup.InlineKeyboard[2][0].CallbackData)
|
|
}
|
|
|
|
// Verify no rollback was executed yet (still pending approval).
|
|
if len(stub.rollbackCalls) != 0 {
|
|
t.Errorf("rollback was executed before approval: %v", stub.rollbackCalls)
|
|
}
|
|
}
|
|
|
|
func TestRollbackApprovalExecutes(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{
|
|
rollbackResult: "Restored to checkpoint 2: 15 files changed.",
|
|
}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
// Step 1: Trigger the approval prompt.
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/rollback 2",
|
|
},
|
|
})
|
|
|
|
// Extract the approve callback token.
|
|
var markup models.InlineKeyboardMarkup
|
|
if err := json.Unmarshal([]byte((*requests)[0].form["reply_markup"]), &markup); err != nil {
|
|
t.Fatalf("decode markup: %v", err)
|
|
}
|
|
approveCallback := markup.InlineKeyboard[0][0].CallbackData
|
|
|
|
*requests = nil
|
|
|
|
// Step 2: Send the approval callback.
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-id",
|
|
From: models.User{ID: allowedUserID},
|
|
Data: approveCallback,
|
|
Message: models.MaybeInaccessibleMessage{
|
|
Type: models.MaybeInaccessibleMessageTypeMessage,
|
|
Message: &models.Message{ID: 9, Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate}},
|
|
},
|
|
},
|
|
})
|
|
|
|
// Verify rollback was executed.
|
|
if len(stub.rollbackCalls) != 1 || stub.rollbackCalls[0] != 2 {
|
|
t.Errorf("rollbackCalls = %v, want [2]", stub.rollbackCalls)
|
|
}
|
|
|
|
// Verify acknowledgement and edit.
|
|
var answered, edited bool
|
|
for _, req := range *requests {
|
|
if req.method == "answerCallbackQuery" {
|
|
answered = true
|
|
if !strings.Contains(req.form["text"], "Approved") {
|
|
t.Errorf("acknowledgement = %q", req.form["text"])
|
|
}
|
|
}
|
|
if req.method == "editMessageText" {
|
|
edited = true
|
|
if !strings.Contains(req.form["text"], "Restored to checkpoint 2") {
|
|
t.Errorf("edit text = %q", req.form["text"])
|
|
}
|
|
}
|
|
}
|
|
if !answered || !edited {
|
|
t.Fatalf("answered=%t edited=%t, requests: %#v", answered, edited, *requests)
|
|
}
|
|
}
|
|
|
|
func TestRollbackWithBadNumber(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/rollback abc",
|
|
},
|
|
})
|
|
|
|
text := messageText(*requests, 0)
|
|
if !strings.Contains(text, "Usage: /rollback") {
|
|
t.Errorf("expected usage message, got: %s", text)
|
|
}
|
|
if len(stub.rollbackCalls) != 0 {
|
|
t.Error("rollback was called with bad argument")
|
|
}
|
|
}
|
|
|
|
func TestStopRequiresApproval(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.stopHandler(nil)(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/stop npm-dev-server",
|
|
},
|
|
})
|
|
|
|
text := (*requests)[0].form["text"]
|
|
if !strings.Contains(text, "Dangerous command") {
|
|
t.Errorf("expected dangerous command prompt, got: %s", text)
|
|
}
|
|
if !strings.Contains(text, "/stop npm-dev-server") {
|
|
t.Errorf("expected command text in prompt, got: %s", text)
|
|
}
|
|
|
|
// Verify no process was stopped yet.
|
|
if len(stub.stopCalls) != 0 {
|
|
t.Errorf("stop was executed before approval: %v", stub.stopCalls)
|
|
}
|
|
}
|
|
|
|
func TestStopApprovalExecutes(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
// Step 1: Trigger the approval prompt.
|
|
g.stopHandler(nil)(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/stop my-process",
|
|
},
|
|
})
|
|
|
|
var markup models.InlineKeyboardMarkup
|
|
if err := json.Unmarshal([]byte((*requests)[0].form["reply_markup"]), &markup); err != nil {
|
|
t.Fatalf("decode markup: %v", err)
|
|
}
|
|
approveCallback := markup.InlineKeyboard[0][0].CallbackData
|
|
|
|
*requests = nil
|
|
|
|
// Step 2: Approve.
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-id",
|
|
From: models.User{ID: allowedUserID},
|
|
Data: approveCallback,
|
|
Message: models.MaybeInaccessibleMessage{
|
|
Type: models.MaybeInaccessibleMessageTypeMessage,
|
|
Message: &models.Message{ID: 9, Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate}},
|
|
},
|
|
},
|
|
})
|
|
|
|
if len(stub.stopCalls) != 1 || stub.stopCalls[0] != "my-process" {
|
|
t.Errorf("stopCalls = %v, want [my-process]", stub.stopCalls)
|
|
}
|
|
}
|
|
|
|
// TestStopWithNoArgumentDoesNotRequireApproval is the emergency-brake
|
|
// contract. Bare /stop targets the running chat turn, so it must act
|
|
// immediately -- if it ever renders an approval prompt again, the brake has
|
|
// been put behind a confirmation round-trip and no longer works.
|
|
func TestStopWithNoArgumentDoesNotRequireApproval(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.stopHandler(nil)(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/stop",
|
|
},
|
|
})
|
|
|
|
text := messageText(*requests, 0)
|
|
if strings.Contains(text, "Dangerous command") {
|
|
t.Errorf("bare /stop asked for approval: %s", text)
|
|
}
|
|
if !strings.Contains(text, "Nothing is running") {
|
|
t.Errorf("expected an idle report, got: %s", text)
|
|
}
|
|
if len(stub.stopCalls) != 0 {
|
|
t.Errorf("bare /stop terminated a named process: %v", stub.stopCalls)
|
|
}
|
|
}
|
|
|
|
// TestStopCancelsRunningChatTurn covers the whole point of the command:
|
|
// a turn in flight is cancelled, and the queue behind it is discarded.
|
|
func TestStopCancelsRunningChatTurn(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
|
|
ctx := t.Context()
|
|
g.turns = messaging.NewTurns(slog.Default())
|
|
|
|
// stopCurrentTurn resolves the session through the router; with none
|
|
// wired, drive the lane directly and assert on the lane's state.
|
|
const session = "chat:7"
|
|
started := make(chan struct{})
|
|
stopped := make(chan struct{})
|
|
g.turns.Submit(ctx, session, func(turnCtx context.Context) {
|
|
close(started)
|
|
<-turnCtx.Done()
|
|
close(stopped)
|
|
})
|
|
<-started
|
|
g.turns.Submit(ctx, session, func(context.Context) {})
|
|
|
|
cancelled, dropped := g.turns.Stop(session)
|
|
if !cancelled {
|
|
t.Error("the running turn was not cancelled")
|
|
}
|
|
if dropped != 1 {
|
|
t.Errorf("dropped = %d, want 1", dropped)
|
|
}
|
|
select {
|
|
case <-stopped:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("the turn did not observe cancellation")
|
|
}
|
|
}
|
|
|
|
func TestDangerousCallbackRejectsUnauthorizedSender(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{
|
|
rollbackResult: "ok",
|
|
}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
// Register a fake action for user 42.
|
|
_ = g.registerDangerousAction("/rollback 1", "rollback test", 42, func(ctx context.Context) (string, error) {
|
|
return "ok", nil
|
|
})
|
|
// Find the token.
|
|
g.dangerousMu.Lock()
|
|
var token string
|
|
for t := range g.dangerousActions {
|
|
token = t
|
|
}
|
|
g.dangerousMu.Unlock()
|
|
|
|
// Attempt approval from user 99 (not allowed).
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-id",
|
|
From: models.User{ID: 99},
|
|
Data: dangerousCmdPrefix + "approve:" + token,
|
|
},
|
|
})
|
|
|
|
var answer *telegramRequest
|
|
for i := range *requests {
|
|
if (*requests)[i].method == "answerCallbackQuery" {
|
|
answer = &(*requests)[i]
|
|
}
|
|
}
|
|
if answer == nil || answer.form["show_alert"] != "true" {
|
|
t.Fatalf("unauthorized rejection did not alert; requests: %#v", *requests)
|
|
}
|
|
if len(stub.rollbackCalls) != 0 {
|
|
t.Error("unauthorized sender executed rollback")
|
|
}
|
|
}
|
|
|
|
func TestDangerousCallbackExpiredToken(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{rollbackResult: "ok"}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
// Register and immediately expire.
|
|
token := g.registerDangerousAction("/rollback 1", "expired", allowedUserID, func(ctx context.Context) (string, error) {
|
|
return "ok", nil
|
|
})
|
|
|
|
g.dangerousMu.Lock()
|
|
action := g.dangerousActions[token]
|
|
action.expiresAt = time.Now().Add(-1 * time.Minute) // force expiry
|
|
g.dangerousActions[token] = action
|
|
g.dangerousMu.Unlock()
|
|
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-id",
|
|
From: models.User{ID: allowedUserID},
|
|
Data: dangerousCmdPrefix + "approve:" + token,
|
|
},
|
|
})
|
|
|
|
if len(stub.rollbackCalls) != 0 {
|
|
t.Error("expired token executed rollback")
|
|
}
|
|
// Find the rejection answer.
|
|
for _, req := range *requests {
|
|
if req.method == "answerCallbackQuery" && req.form["text"] == "That action is no longer valid." {
|
|
return // pass
|
|
}
|
|
}
|
|
t.Errorf("no 'no longer valid' answer; requests: %#v", *requests)
|
|
}
|
|
|
|
func TestDangerousCallbackSingleUse(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{rollbackResult: "ok"}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
token := g.registerDangerousAction("/rollback 1", "single-use test", allowedUserID, func(ctx context.Context) (string, error) {
|
|
return g.Dangerous.Rollback(ctx, 1)
|
|
})
|
|
|
|
// First approval should work.
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-1",
|
|
From: models.User{ID: allowedUserID},
|
|
Data: dangerousCmdPrefix + "approve:" + token,
|
|
Message: models.MaybeInaccessibleMessage{
|
|
Type: models.MaybeInaccessibleMessageTypeMessage,
|
|
Message: &models.Message{ID: 9, Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate}},
|
|
},
|
|
},
|
|
})
|
|
|
|
if len(stub.rollbackCalls) != 1 {
|
|
t.Fatalf("first approval did not execute: calls=%d", len(stub.rollbackCalls))
|
|
}
|
|
|
|
*requests = nil
|
|
|
|
// Second approval (replay) should be rejected.
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-2",
|
|
From: models.User{ID: allowedUserID},
|
|
Data: dangerousCmdPrefix + "approve:" + token,
|
|
},
|
|
})
|
|
|
|
if len(stub.rollbackCalls) != 1 {
|
|
t.Errorf("replay executed rollback again: calls=%d", len(stub.rollbackCalls))
|
|
}
|
|
|
|
for _, req := range *requests {
|
|
if req.method == "answerCallbackQuery" && req.form["text"] == "That action is no longer valid." {
|
|
return
|
|
}
|
|
}
|
|
t.Errorf("replay was not rejected; requests: %#v", *requests)
|
|
}
|
|
|
|
func TestDangerousCallbackDeny(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{rollbackResult: "ok"}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
token := g.registerDangerousAction("/rollback 1", "deny test", allowedUserID, func(ctx context.Context) (string, error) {
|
|
return "ok", nil
|
|
})
|
|
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-id",
|
|
From: models.User{ID: allowedUserID},
|
|
Data: dangerousCmdPrefix + "deny:" + token,
|
|
Message: models.MaybeInaccessibleMessage{
|
|
Type: models.MaybeInaccessibleMessageTypeMessage,
|
|
Message: &models.Message{ID: 9, Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate}},
|
|
},
|
|
},
|
|
})
|
|
|
|
if len(stub.rollbackCalls) != 0 {
|
|
t.Error("deny executed rollback")
|
|
}
|
|
|
|
var foundDeny bool
|
|
for _, req := range *requests {
|
|
if req.method == "answerCallbackQuery" && strings.Contains(req.form["text"], "denied") {
|
|
foundDeny = true
|
|
}
|
|
}
|
|
if !foundDeny {
|
|
t.Errorf("deny was not acknowledged; requests: %#v", *requests)
|
|
}
|
|
}
|
|
|
|
func TestDangerousCallbackExecutionFailure(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{rollbackErr: fmt.Errorf("checkpoint corrupted")}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
token := g.registerDangerousAction("/rollback 1", "failure test", allowedUserID, func(ctx context.Context) (string, error) {
|
|
return g.Dangerous.Rollback(ctx, 1)
|
|
})
|
|
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-id",
|
|
From: models.User{ID: allowedUserID},
|
|
Data: dangerousCmdPrefix + "approve:" + token,
|
|
Message: models.MaybeInaccessibleMessage{
|
|
Type: models.MaybeInaccessibleMessageTypeMessage,
|
|
Message: &models.Message{ID: 9, Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate}},
|
|
},
|
|
},
|
|
})
|
|
|
|
var foundFail bool
|
|
for _, req := range *requests {
|
|
if req.method == "answerCallbackQuery" && strings.Contains(req.form["text"], "Command failed") {
|
|
foundFail = true
|
|
}
|
|
if req.method == "editMessageText" && strings.Contains(req.form["text"], "Failed") {
|
|
foundFail = true
|
|
}
|
|
}
|
|
if !foundFail {
|
|
t.Errorf("execution failure was not reported; requests: %#v", *requests)
|
|
}
|
|
}
|
|
|
|
func TestPermanentApprovalSkipsInlineKeyboard(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{
|
|
rollbackResult: "Restored (permanent approval).",
|
|
}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
|
|
// Grant permanent approval for rollback.
|
|
g.dangerousMu.Lock()
|
|
g.permanentApprovals = append(g.permanentApprovals, permanentApproval{
|
|
CommandPattern: "/rollback",
|
|
Recipient: allowedUserID,
|
|
GrantedAt: time.Now(),
|
|
ExpiresAt: time.Now().Add(24 * time.Hour),
|
|
Scope: "test permanent approval",
|
|
})
|
|
g.dangerousMu.Unlock()
|
|
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/rollback 3",
|
|
},
|
|
})
|
|
|
|
// Should execute immediately without inline keyboard.
|
|
if len(stub.rollbackCalls) != 1 || stub.rollbackCalls[0] != 3 {
|
|
t.Errorf("rollbackCalls = %v, want [3]", stub.rollbackCalls)
|
|
}
|
|
|
|
text := messageText(*requests, 0)
|
|
if !strings.Contains(text, "Restored (permanent approval)") {
|
|
t.Errorf("expected immediate execution result, got: %s", text)
|
|
}
|
|
if _, ok := (*requests)[0].form["reply_markup"]; ok {
|
|
t.Error("permanent approval path rendered inline keyboard (should not)")
|
|
}
|
|
}
|
|
|
|
func TestPermanentApprovalExpired(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{rollbackResult: "ok"}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
|
|
// Grant expired permanent approval.
|
|
g.dangerousMu.Lock()
|
|
g.permanentApprovals = append(g.permanentApprovals, permanentApproval{
|
|
CommandPattern: "/rollback",
|
|
Recipient: allowedUserID,
|
|
GrantedAt: time.Now().Add(-48 * time.Hour),
|
|
ExpiresAt: time.Now().Add(-1 * time.Minute), // expired
|
|
Scope: "expired test",
|
|
})
|
|
g.dangerousMu.Unlock()
|
|
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/rollback 2",
|
|
},
|
|
})
|
|
|
|
// Should show inline keyboard (approval required).
|
|
text := (*requests)[0].form["text"]
|
|
if !strings.Contains(text, "Dangerous command") {
|
|
t.Errorf("expected dangerous command prompt for expired permanent approval, got: %s", text)
|
|
}
|
|
if _, ok := (*requests)[0].form["reply_markup"]; !ok {
|
|
t.Error("expired permanent approval should still require inline keyboard")
|
|
}
|
|
}
|
|
|
|
func TestPermanentApprovalButtonGrantsApproval(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{
|
|
rollbackResult: "Restored after permanent approval grant.",
|
|
}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
// Step 1: Trigger approval prompt.
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/rollback 1",
|
|
},
|
|
})
|
|
|
|
// Extract permanent approval callback.
|
|
var markup models.InlineKeyboardMarkup
|
|
if err := json.Unmarshal([]byte((*requests)[0].form["reply_markup"]), &markup); err != nil {
|
|
t.Fatalf("decode markup: %v", err)
|
|
}
|
|
permanentCallback := markup.InlineKeyboard[1][0].CallbackData
|
|
|
|
*requests = nil
|
|
|
|
// Step 2: Click Approve Permanently.
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-id",
|
|
From: models.User{ID: allowedUserID},
|
|
Data: permanentCallback,
|
|
Message: models.MaybeInaccessibleMessage{
|
|
Type: models.MaybeInaccessibleMessageTypeMessage,
|
|
Message: &models.Message{ID: 9, Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate}},
|
|
},
|
|
},
|
|
})
|
|
|
|
// Should execute and record permanent approval.
|
|
if len(stub.rollbackCalls) != 1 || stub.rollbackCalls[0] != 1 {
|
|
t.Errorf("rollbackCalls = %v, want [1]", stub.rollbackCalls)
|
|
}
|
|
|
|
// Verify permanent approval was recorded.
|
|
g.dangerousMu.Lock()
|
|
hasPerm := false
|
|
for _, pa := range g.permanentApprovals {
|
|
if pa.CommandPattern == "/rollback 1" && pa.Recipient == allowedUserID {
|
|
hasPerm = true
|
|
if pa.ExpiresAt.Before(time.Now().Add(23 * time.Hour)) {
|
|
t.Error("permanent approval expiry too short")
|
|
}
|
|
}
|
|
}
|
|
g.dangerousMu.Unlock()
|
|
if !hasPerm {
|
|
t.Error("permanent approval was not recorded")
|
|
}
|
|
}
|
|
|
|
func TestPermanentApprovalScopedToRecipient(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
const otherUserID = int64(99)
|
|
stub := &dangerousStub{rollbackResult: "ok"}
|
|
g := New("1:test", []int64{allowedUserID, otherUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
|
|
// Grant permanent approval for user 42.
|
|
g.dangerousMu.Lock()
|
|
g.permanentApprovals = append(g.permanentApprovals, permanentApproval{
|
|
CommandPattern: "/rollback",
|
|
Recipient: 42,
|
|
GrantedAt: time.Now(),
|
|
ExpiresAt: time.Now().Add(24 * time.Hour),
|
|
Scope: "recipient scoping test",
|
|
})
|
|
g.dangerousMu.Unlock()
|
|
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
// User 99 tries /rollback — should require approval.
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: otherUserID},
|
|
Chat: models.Chat{ID: 8, Type: models.ChatTypePrivate},
|
|
Text: "/rollback 2",
|
|
},
|
|
})
|
|
|
|
text := (*requests)[0].form["text"]
|
|
if !strings.Contains(text, "Dangerous command") {
|
|
t.Errorf("user 99 should require approval; got: %s", text)
|
|
}
|
|
}
|
|
|
|
func TestApproveHandlerShowsPendingCommands(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
|
|
// Register a pending command.
|
|
g.registerDangerousAction("/rollback 2", "Rollback to checkpoint 2", allowedUserID, func(ctx context.Context) (string, error) {
|
|
return "", nil
|
|
})
|
|
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.approveHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/approve",
|
|
},
|
|
})
|
|
|
|
text := messageText(*requests, 0)
|
|
if !strings.Contains(text, "Pending dangerous commands") {
|
|
t.Errorf("expected pending list, got: %s", text)
|
|
}
|
|
if !strings.Contains(text, "Rollback to checkpoint 2") {
|
|
t.Errorf("expected pending command description, got: %s", text)
|
|
}
|
|
}
|
|
|
|
func TestApproveHandlerNoPendingCommands(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.approveHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/approve",
|
|
},
|
|
})
|
|
|
|
text := messageText(*requests, 0)
|
|
if !strings.Contains(text, "No pending commands") {
|
|
t.Errorf("expected no pending message, got: %s", text)
|
|
}
|
|
}
|
|
|
|
func TestRollbackNotConfigured(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
// Dangerous is nil.
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.rollbackHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/rollback 1",
|
|
},
|
|
})
|
|
|
|
text := messageText(*requests, 0)
|
|
if !strings.Contains(text, "not configured") {
|
|
t.Errorf("expected not configured message, got: %s", text)
|
|
}
|
|
}
|
|
|
|
func TestStopNotConfigured(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
// Dangerous is nil.
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.stopHandler(nil)(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
Text: "/stop process",
|
|
},
|
|
})
|
|
|
|
text := messageText(*requests, 0)
|
|
if !strings.Contains(text, "not configured") {
|
|
t.Errorf("expected not configured message, got: %s", text)
|
|
}
|
|
}
|
|
|
|
func TestDangerousCommandHelpContainsNewCommands(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
|
|
var sentText string
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.helpHandler()(context.Background(), b, &models.Update{
|
|
Message: &models.Message{
|
|
From: &models.User{ID: allowedUserID},
|
|
Chat: models.Chat{ID: 7, Type: models.ChatTypePrivate},
|
|
},
|
|
})
|
|
|
|
for _, req := range *requests {
|
|
if req.method == "sendRichMessage" {
|
|
sentText = req.form["rich_message"]
|
|
}
|
|
}
|
|
if sentText == "" {
|
|
t.Fatal("help sent no rich message")
|
|
}
|
|
for _, want := range []string{"/rollback", "/stop"} {
|
|
if !strings.Contains(sentText, want) {
|
|
t.Errorf("help text missing %q", want)
|
|
}
|
|
}
|
|
// /approve and /deny should NOT appear in help (they are hidden).
|
|
for _, hidden := range []string{"/approve\n", "/deny\n"} {
|
|
if strings.Contains(sentText, hidden) {
|
|
t.Errorf("help text contains hidden command %q", hidden)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseDangerousCallback(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
data string
|
|
decision string
|
|
token string
|
|
}{
|
|
{name: "approve", data: dangerousCmdPrefix + "approve:abc123", decision: "approve", token: "abc123"},
|
|
{name: "permanent", data: dangerousCmdPrefix + "permanent:def456", decision: "permanent", token: "def456"},
|
|
{name: "deny", data: dangerousCmdPrefix + "deny:ghi789", decision: "deny", token: "ghi789"},
|
|
{name: "wrong prefix", data: "other:approve:abc", decision: "", token: ""},
|
|
{name: "unknown decision", data: dangerousCmdPrefix + "unknown:abc", decision: "", token: ""},
|
|
{name: "empty", data: "", decision: "", token: ""},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
decision, token := parseDangerousCallback(tt.data)
|
|
if decision != tt.decision || token != tt.token {
|
|
t.Errorf("parseDangerousCallback(%q) = (%q, %q), want (%q, %q)",
|
|
tt.data, decision, token, tt.decision, tt.token)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDangerousCommandSpecDescriptions(t *testing.T) {
|
|
found := make(map[string]bool)
|
|
for _, spec := range gatewayCommandSpecs {
|
|
found[spec.Command] = true
|
|
}
|
|
for _, cmd := range []string{"rollback", "stop"} {
|
|
if !found[cmd] {
|
|
t.Errorf("/%s is missing from gatewayCommandSpecs", cmd)
|
|
}
|
|
}
|
|
for _, cmd := range []string{"approve", "deny"} {
|
|
if found[cmd] {
|
|
t.Errorf("/%s should NOT be in gatewayCommandSpecs (hidden command)", cmd)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDangerousCallbackRejectsWrongRecipient(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
const otherUserID = int64(43)
|
|
stub := &dangerousStub{rollbackResult: "ok"}
|
|
g := New("1:test", []int64{allowedUserID, otherUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, _ := newTelegramTestBot(t)
|
|
|
|
token := g.registerDangerousAction("/rollback 1", "recipient test", allowedUserID, func(ctx context.Context) (string, error) {
|
|
return "ok", nil
|
|
})
|
|
|
|
// User 43 tries to approve user 42's command.
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-id",
|
|
From: models.User{ID: otherUserID},
|
|
Data: dangerousCmdPrefix + "approve:" + token,
|
|
},
|
|
})
|
|
|
|
if len(stub.rollbackCalls) != 0 {
|
|
t.Error("wrong recipient executed rollback")
|
|
}
|
|
}
|
|
|
|
func TestMalformedDangerousCallback(t *testing.T) {
|
|
const allowedUserID = int64(42)
|
|
stub := &dangerousStub{}
|
|
g := New("1:test", []int64{allowedUserID}, slog.Default())
|
|
g.Dangerous = stub
|
|
b, requests := newTelegramTestBot(t)
|
|
|
|
g.handleDangerousCallback(context.Background(), b, &models.Update{
|
|
CallbackQuery: &models.CallbackQuery{
|
|
ID: "callback-id",
|
|
From: models.User{ID: allowedUserID},
|
|
Data: dangerousCmdPrefix + "bad-format",
|
|
},
|
|
})
|
|
|
|
for _, req := range *requests {
|
|
if req.method == "answerCallbackQuery" && req.form["text"] == "That action is no longer valid." {
|
|
return
|
|
}
|
|
}
|
|
t.Errorf("malformed callback not rejected; requests: %#v", *requests)
|
|
}
|