Fix correctness bugs found by code review

- matrix: drop cryptohelper LoginAs so device.json creds aren't
  invalidated on every restart by Init's re-login
- matrix: thread ctx through PostThreadedReply; bot dispatcher derives
  per-command ctx from app lifecycle so SIGTERM cancels in-flight work
- matrix: isTokenValid verifies /whoami user_id matches configured one
- matrix: loadDevice distinguishes missing file from corrupt parse;
  refuse to silently overwrite a corrupt device.json
- matrix: Start checks Syncer type assertion and returns error
- arr: parseLookup skips empty-title items and extracts existing id
- bot: skip Add when Result.Exists; reply "already in library"
This commit is contained in:
prosolis
2026-05-24 20:50:51 -07:00
parent 8c295d183b
commit 035089c159
5 changed files with 137 additions and 52 deletions

View File

@@ -44,7 +44,7 @@ type stubReplier struct {
calls int
}
func (r *stubReplier) PostThreadedReply(_ id.RoomID, _ id.EventID, plain, htmlBody string) error {
func (r *stubReplier) PostThreadedReply(_ context.Context, _ id.RoomID, _ id.EventID, plain, htmlBody string) error {
r.mu.Lock()
defer r.mu.Unlock()
r.calls++
@@ -53,6 +53,10 @@ func (r *stubReplier) PostThreadedReply(_ id.RoomID, _ id.EventID, plain, htmlBo
return nil
}
func newDispatcher(prefix string, services Services, replier Replier) *Dispatcher {
return New(context.Background(), prefix, services, replier)
}
const (
room = id.RoomID("!room:example.org")
event = id.EventID("$evt")
@@ -87,7 +91,7 @@ func TestDispatchMovieTopHit(t *testing.T) {
{Title: "Dune", Year: 1984},
}}
rep := &stubReplier{}
d := New("!", Services{Radarr: radarr}, rep)
d := newDispatcher("!", Services{Radarr: radarr}, rep)
d.Handle(room, event, sender, "!movie dune")
@@ -105,7 +109,7 @@ func TestDispatchMovieTopHit(t *testing.T) {
func TestDispatchNoResults(t *testing.T) {
sonarr := &stubClient{}
rep := &stubReplier{}
d := New("!", Services{Sonarr: sonarr}, rep)
d := newDispatcher("!", Services{Sonarr: sonarr}, rep)
d.Handle(room, event, sender, "!tv obscure show")
@@ -119,7 +123,7 @@ func TestDispatchNoResults(t *testing.T) {
func TestDispatchUnconfigured(t *testing.T) {
rep := &stubReplier{}
d := New("!", Services{}, rep)
d := newDispatcher("!", Services{}, rep)
d.Handle(room, event, sender, "!music radiohead")
@@ -131,7 +135,7 @@ func TestDispatchUnconfigured(t *testing.T) {
func TestDispatchEmptyQuery(t *testing.T) {
radarr := &stubClient{}
rep := &stubReplier{}
d := New("!", Services{Radarr: radarr}, rep)
d := newDispatcher("!", Services{Radarr: radarr}, rep)
d.Handle(room, event, sender, "!movie")
@@ -149,7 +153,7 @@ func TestDispatchAddError(t *testing.T) {
addErr: errors.New("boom"),
}
rep := &stubReplier{}
d := New("!", Services{Radarr: radarr}, rep)
d := newDispatcher("!", Services{Radarr: radarr}, rep)
d.Handle(room, event, sender, "!movie dune")
@@ -161,7 +165,7 @@ func TestDispatchAddError(t *testing.T) {
func TestDispatchSearchError(t *testing.T) {
radarr := &stubClient{srchErr: errors.New("network down")}
rep := &stubReplier{}
d := New("!", Services{Radarr: radarr}, rep)
d := newDispatcher("!", Services{Radarr: radarr}, rep)
d.Handle(room, event, sender, "!movie dune")
@@ -172,7 +176,7 @@ func TestDispatchSearchError(t *testing.T) {
func TestDispatchHelp(t *testing.T) {
rep := &stubReplier{}
d := New("!", Services{}, rep)
d := newDispatcher("!", Services{}, rep)
d.Handle(room, event, sender, "!help")
@@ -185,7 +189,7 @@ func TestDispatchHelp(t *testing.T) {
func TestDispatchUnknown(t *testing.T) {
rep := &stubReplier{}
d := New("!", Services{}, rep)
d := newDispatcher("!", Services{}, rep)
d.Handle(room, event, sender, "!frobnicate stuff")
@@ -196,7 +200,7 @@ func TestDispatchUnknown(t *testing.T) {
func TestNonCommandIgnored(t *testing.T) {
rep := &stubReplier{}
d := New("!", Services{}, rep)
d := newDispatcher("!", Services{}, rep)
d.Handle(room, event, sender, "just chatting")
@@ -205,10 +209,25 @@ func TestNonCommandIgnored(t *testing.T) {
}
}
func TestDispatchAlreadyInLibrary(t *testing.T) {
radarr := &stubClient{results: []arr.Result{{Title: "Dune", Year: 2021, Exists: true}}}
rep := &stubReplier{}
d := newDispatcher("!", Services{Radarr: radarr}, rep)
d.Handle(room, event, sender, "!movie dune")
if len(radarr.added) != 0 {
t.Fatalf("should not re-add existing item, got %d adds", len(radarr.added))
}
if !strings.Contains(rep.plain, "already has") || !strings.Contains(rep.plain, "Dune (2021)") {
t.Fatalf("unexpected reply: %q", rep.plain)
}
}
func TestCustomPrefix(t *testing.T) {
radarr := &stubClient{results: []arr.Result{{Title: "Dune", Year: 2021}}}
rep := &stubReplier{}
d := New(".bh ", Services{Radarr: radarr}, rep)
d := newDispatcher(".bh ", Services{Radarr: radarr}, rep)
d.Handle(room, event, sender, ".bh movie dune")