From: Anderson A. Giacomolli Date: Mon, 9 Nov 2026 20:52:43 +0000 (-0300) Subject: Check test assertions with testifylint X-Git-Url: http://git.giacomolli.uk/onechat/commitdiff_plain/000ae97acb21a1c0c90e9278177a74411ba6eab2 Check test assertions with testifylint Test assertions use the specific require helpers for errors, lengths, JSON, and numeric values. Test setup completes before its server starts so that a failed assertion stops the test goroutine, not a worker. --- diff --git a/.golangci.yml b/.golangci.yml index 082c4f4..1a7a596 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,3 +5,4 @@ linters: enable: - errorlint - gocritic + - testifylint diff --git a/daemon_start_helper_test.go b/daemon_start_helper_test.go index 8ed36e9..81399ce 100644 --- a/daemon_start_helper_test.go +++ b/daemon_start_helper_test.go @@ -50,9 +50,10 @@ func daemonStartTestWithSchedules(t *testing.T, server *ModelFake, bot *Telegram client := newModelClient(server.URL, ModelAPIKey(modelTestKey), agent.CacheKey(), filepath.Join(agent.dir, "usage.jsonl")) ctx, cancel := context.WithCancel(t.Context()) done := make(chan error) + subagents := newTestSubagents(t) go func() { done <- daemonServe(ctx, listener, bot, whatsapp, schedules, func(notify func(Event)) (*TurnLoop, error) { - return newTurnLoop(agent, memory, newTestSubagents(t), whatsapp, schedules, client, clock, discardLog(), notify) + return newTurnLoop(agent, memory, subagents, whatsapp, schedules, client, clock, discardLog(), notify) }) }() diff --git a/log_test.go b/log_test.go index 306b21f..e8e07ac 100644 --- a/log_test.go +++ b/log_test.go @@ -95,11 +95,11 @@ func TestLogWritesOneFilePerDay(t *testing.T) { first, err := os.ReadFile(filepath.Join(root, "main", "2026-01-01.jsonl")) require.NoError(t, err) - require.Equal(t, `{"i":0,"kind":"user","text":"first day","size":9,"date":"2026-01-01T12:00:00Z"}`+"\n", string(first)) + require.JSONEq(t, `{"i":0,"kind":"user","text":"first day","size":9,"date":"2026-01-01T12:00:00Z"}`+"\n", string(first)) second, err := os.ReadFile(filepath.Join(root, "main", "2026-01-02.jsonl")) require.NoError(t, err) - require.Equal(t, `{"i":1,"kind":"user","text":"second day","size":10,"date":"2026-01-02T12:00:00Z"}`+"\n", string(second)) + require.JSONEq(t, `{"i":1,"kind":"user","text":"second day","size":10,"date":"2026-01-02T12:00:00Z"}`+"\n", string(second)) } func TestLogAllowsOneWriter(t *testing.T) { diff --git a/replay_test.go b/replay_test.go index 96091c2..547b5e4 100644 --- a/replay_test.go +++ b/replay_test.go @@ -71,7 +71,7 @@ func TestReplaySyntheticLog(t *testing.T) { memory = replayRequireRestart(t, memory, root, clock) require.Equal(t, messages, memory.log.Len()) - require.Greater(t, batches, 0) + require.Positive(t, batches) require.NoError(t, memory.Wait(t.Context())) t.Logf("%d messages, %d nodes, %d batches in %s", messages, memory.tree.Count(), batches, time.Since(start).Round(time.Second)) diff --git a/telegram_test.go b/telegram_test.go index 48e5503..5ced143 100644 --- a/telegram_test.go +++ b/telegram_test.go @@ -223,7 +223,7 @@ func TestBotStartsAtTheStoredOffset(t *testing.T) { terminal.WaitFor(t, "[connected to OneChat]") telegramWaitForSent(t, telegram, "ok") - require.Equal(t, float64(10), telegram.Calls()[0].params["offset"]) + require.InDelta(t, float64(10), telegram.Calls()[0].params["offset"], 0) require.NotContains(t, terminal.Output(), "handled before the restart") require.Eventually(t, func() bool { @@ -569,7 +569,7 @@ func TestRefusedRichPartGoesWithTheTextAsWritten(t *testing.T) { require.LessOrEqual(t, telegramUTF16Units(text), telegramMessageUnits) } - require.True(t, reply == strings.Join(fallback, "")+telegramUnescapeTags(rich[0]), "reply is not sent once and whole") + require.Equal(t, reply, strings.Join(fallback, "")+telegramUnescapeTags(rich[0]), "reply is not sent once and whole") } func TestRefusedEntitiesGoAgainAsPlainText(t *testing.T) { diff --git a/tools_test.go b/tools_test.go index 3ab7845..45cd4a2 100644 --- a/tools_test.go +++ b/tools_test.go @@ -211,7 +211,7 @@ func TestHeadTailKeepsTheEndsOfALongOutput(t *testing.T) { require.True(t, strings.HasPrefix(text, "0000xxx")) require.True(t, strings.HasSuffix(text, "0999"+strings.Repeat("x", 995)+"\n")) require.Contains(t, text, fmt.Sprintf("\n[... %d bytes cut ...]\n", 1000*1000-toolClipCharacters)) - require.Equal(t, toolClipCharacters+len(fmt.Sprintf("\n[... %d bytes cut ...]\n", 1000*1000-toolClipCharacters)), len(text)) + require.Len(t, text, toolClipCharacters+len(fmt.Sprintf("\n[... %d bytes cut ...]\n", 1000*1000-toolClipCharacters))) } // ToolRecordingLog is a chat log that records each append and flush. The flush diff --git a/tree_test.go b/tree_test.go index 46c17c7..f2cec17 100644 --- a/tree_test.go +++ b/tree_test.go @@ -80,7 +80,7 @@ func TestTreeReopenLoadsTheSameNodes(t *testing.T) { data, err := os.ReadFile(filepath.Join(root, "tree", "2026-01-01.jsonl")) require.NoError(t, err) - require.Equal(t, `{"l":0,"i":1,"text":"reply: b","size":8}`+"\n", strings.SplitAfter(string(data), "\n")[0]) + require.JSONEq(t, `{"l":0,"i":1,"text":"reply: b","size":8}`+"\n", strings.SplitAfter(string(data), "\n")[0]) tree, err = openTree(t.Context(), root, 2, clock.Now) require.NoError(t, err) diff --git a/turn_test.go b/turn_test.go index 9bb0e99..2763965 100644 --- a/turn_test.go +++ b/turn_test.go @@ -3,7 +3,6 @@ package main import ( "context" "encoding/json" - "errors" "log" "net/http" "path/filepath" @@ -410,7 +409,7 @@ func newTestTurnLoopWith(t *testing.T, server *ModelFake, summarizer Summarizer) t.Cleanup(func() { cancel() - require.True(t, errors.Is(<-done, context.Canceled)) + require.ErrorIs(t, <-done, context.Canceled) require.NoError(t, memory.Close()) })