From a170204ecb9053cb39e31d41a040658fe7519e2c Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 19:50:40 +0100 Subject: [PATCH] test: close gaps found by mutation checks on the archive import - Cover the CRLF separator line, cycle roots entered from outside, linear rethreading of long chains - Cover a read error mid-import, the "and N more" lines, and the mbox content type - Cover clearing the previous result and the capped error list in the UI --- admin/src/tabs/ArchiveTab.test.tsx | 38 ++++++++++++++++++++++- internal/api/api_test.go | 33 ++++++++++++++++++++ internal/cli/cli_test.go | 40 ++++++++++++++++++++++++ internal/mbox/mbox_test.go | 3 ++ internal/store/archive_test.go | 49 ++++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 1 deletion(-) diff --git a/admin/src/tabs/ArchiveTab.test.tsx b/admin/src/tabs/ArchiveTab.test.tsx index 20360af..49f6d50 100644 --- a/admin/src/tabs/ArchiveTab.test.tsx +++ b/admin/src/tabs/ArchiveTab.test.tsx @@ -1,5 +1,5 @@ import { screen, waitFor, within } from '@testing-library/react' -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import type { Message } from '@/lib/api' import { aList, installFakeApi, newState } from '@/test/fakeApi' import { renderApp } from '@/test/render' @@ -244,5 +244,41 @@ describe('archive tab', () => { await user.upload(screen.getByLabelText(/mbox file/i), file) await waitFor(() => expect(state.importTypes).toHaveLength(2)) }) + + it('forgets the previous result when a new import starts', async () => { + const { user, state } = await setup([]) + state.fail['POST /lists/1/archive/import'] = { status: 500, error: 'disk full' } + await user.upload(screen.getByLabelText(/mbox file/i), mboxFile(1)) + expect(await screen.findByRole('alert')).toHaveTextContent('disk full') + + await user.upload(screen.getByLabelText(/mbox file/i), mboxFile(1)) + expect(await screen.findByRole('status')).toHaveTextContent('Imported 1 message') + expect(screen.queryByRole('alert')).not.toBeInTheDocument() // the old error is gone + + state.fail['POST /lists/1/archive/import'] = { status: 500, error: 'again' } + await user.upload(screen.getByLabelText(/mbox file/i), mboxFile(2)) + expect(await screen.findByRole('alert')).toHaveTextContent('again') + expect(screen.queryByRole('status')).not.toBeInTheDocument() // the old summary is gone + }) + + it('says how many more failures there are than the server listed', async () => { + const { user } = await setup([]) + const real = fetch + vi.stubGlobal('fetch', vi.fn((url: string, init?: RequestInit) => { + if (url.endsWith('/archive/import')) { + const errors = Array.from({ length: 20 }, (_, i) => `message ${i + 1}: parse message: no headers`) + return Promise.resolve(new Response(JSON.stringify({ imported: 0, duplicates: 0, failed: 25, errors }), { + status: 200, + headers: { 'Content-Type': 'application/json' }, + })) + } + return real(url, init) + })) + await user.upload(screen.getByLabelText(/mbox file/i), mboxFile(1)) + const status = await screen.findByRole('status') + expect(status).toHaveTextContent('25 failed') + expect(within(status).getByText(/and 5 more/)).toBeInTheDocument() + expect(within(status).getAllByRole('listitem')).toHaveLength(21) + }) }) }) diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 4c22d97..1e58c06 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -3,6 +3,7 @@ package api import ( "context" "encoding/json" + "errors" "fmt" "net/http" "net/http/httptest" @@ -596,3 +597,35 @@ func TestArchiveImportEdgeCases(t *testing.T) { t.Fatalf("an import works even when the archive policy is off: %+v", sum) } } + +type failingBody struct { + data string + done bool +} + +func (f *failingBody) Read(p []byte) (int, error) { + if !f.done { + f.done = true + return copy(p, f.data), nil + } + return 0, errors.New("connection reset by peer") +} + +func TestArchiveImportStoppedByAReadErrorIsAnErrorResponse(t *testing.T) { + h, s := queueServer(t) + s.CreateList("dev", "example.org", "Dev") + req := httptest.NewRequest("POST", "/api/v1/lists/1/archive/import", &failingBody{data: importMbox}) + req.Header.Set("Authorization", "Bearer "+token) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + expect(t, rec, 400) + var e struct{ Error string } + decode(t, rec, &e) + if !strings.Contains(e.Error, "connection reset") || !strings.Contains(e.Error, "1 message") { + t.Fatalf("the error says what happened and how far it got: %q", e.Error) + } + l, _ := s.FindList("dev@example.org") + if _, total, _ := s.ListMessages(l.ID, 10, 0); total != 1 { + t.Fatalf("the first message was complete and is kept, got %d", total) + } +} diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index 2fb14ee..f64aece 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "net/http/httptest" "os" @@ -512,3 +513,42 @@ func TestLongRunningUploadsAreNotCutByTheShortRequestTimeout(t *testing.T) { t.Fatal("uploads of big archives must not have an overall timeout") } } + +func TestArchiveImportSaysHowManyMoreFailuresThereAre(t *testing.T) { + e := newEnv(t) + e.ok(t, "list", "add", "dev@example.org") + var b strings.Builder + for i := 0; i < 25; i++ { + b.WriteString("From x@y.z Tue Jan 2 15:04:05 2024\n\nno headers\n\n") + } + code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, b.String(), "archive", "import", "dev@example.org", "-") + if code != 1 || !strings.Contains(out, "25 failed") { + t.Fatalf("%d %q", code, out) + } + if !strings.Contains(errs, "... and 5 more") { + t.Fatalf("only 20 are listed, the rest is counted: %q", errs) + } +} + +func TestArchiveImportSendsTheFileAsAnMbox(t *testing.T) { + e := newEnv(t) + e.ok(t, "list", "add", "dev@example.org") + var contentType, body string + spy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "/archive/import") { + contentType = r.Header.Get("Content-Type") + b, _ := io.ReadAll(r.Body) + body = string(b) + r.Body = io.NopCloser(strings.NewReader(body)) + } + e.srv.Config.Handler.ServeHTTP(w, r) + })) + defer spy.Close() + vars := map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": spy.URL} + if code, _, errs := e.runEnv(t, vars, cliMbox, "archive", "import", "dev@example.org", "-"); code != 0 { + t.Fatalf("%d %s", code, errs) + } + if contentType != "application/mbox" || body != cliMbox { + t.Fatalf("content type %q, body unchanged: %v", contentType, body == cliMbox) + } +} diff --git a/internal/mbox/mbox_test.go b/internal/mbox/mbox_test.go index c508f43..a39e7e5 100644 --- a/internal/mbox/mbox_test.go +++ b/internal/mbox/mbox_test.go @@ -104,6 +104,9 @@ func TestCRLFAndMissingFinalNewline(t *testing.T) { if len(ms) != 1 || !strings.HasSuffix(string(ms[0].Data), "body without newline") || !strings.Contains(string(ms[0].Data), "Subject: s\r\n") { t.Fatalf("%q", ms[0].Data) } + if string(readAll(t, "From a@x.com Tue Jan 2 15:04:05 2024\r\nSubject: one\r\n\r\nb\r\n\r\nFrom b@x.com Tue Jan 2 15:04:05 2024\r\nSubject: two\r\n\r\nb2\r\n", 0)[0].Data) != "Subject: one\r\n\r\nb\r\n" { + t.Fatal("the CRLF blank line before a separator belongs to the separator") + } crlf := "From a@x.com Tue Jan 2 15:04:05 2024\r\nSubject: one\r\n\r\nb\r\n\r\nFrom b@x.com Tue Jan 2 15:04:05 2024\r\nSubject: two\r\n\r\nb2\r\n" if got := readAll(t, crlf, 0); len(got) != 2 { t.Fatalf("CRLF separators: %d", len(got)) diff --git a/internal/store/archive_test.go b/internal/store/archive_test.go index 1ee9133..c8fb809 100644 --- a/internal/store/archive_test.go +++ b/internal/store/archive_test.go @@ -2,6 +2,7 @@ package store import ( "errors" + "fmt" "strings" "testing" "time" @@ -383,3 +384,51 @@ func TestRethreadOnlyTouchesTheGivenList(t *testing.T) { t.Fatal("list b's child must now follow its parent") } } + +func TestRethreadCycleRootIsTheSmallestIdEvenWhenEnteredFromElsewhere(t *testing.T) { + s := newTestStore(t) + l, _ := s.CreateList("dev", "example.org", "Dev") + // ids: x=1 replies to c; b=2 replies to c; c=3 replies to b. The cycle is {b, c}, entered from x at c, + // which is not its smallest id. + x := msg("", "X", "x") + x.InReplyTo = "" + b := msg("", "B", "b") + b.InReplyTo = "" + c := msg("", "C", "c") + c.InReplyTo = "" + ix, _, _ := s.ArchiveMessage(l.ID, x) + ib, _, _ := s.ArchiveMessage(l.ID, b) + ic, _, _ := s.ArchiveMessage(l.ID, c) + if _, err := s.Rethread(l.ID); err != nil { + t.Fatal(err) + } + want := min(ib, ic) + for _, id := range []int64{ix, ib, ic} { + if got := threadOf(t, s, id); got != want { + t.Fatalf("message %d: thread %d, want %d (the smallest id of the cycle)", id, got, want) + } + } +} + +func TestRethreadIsLinearOnLongChains(t *testing.T) { + s := newTestStore(t) + l, _ := s.CreateList("dev", "example.org", "Dev") + const n = 20000 + ms := make([]Message, n) + for i := range ms { + ms[i] = msg(fmt.Sprintf("", i), "S", "b") + if i > 0 { + ms[i].InReplyTo = fmt.Sprintf("", i-1) + } + } + if _, err := s.ArchiveBatch(l.ID, ms); err != nil { + t.Fatal(err) + } + done := make(chan struct{}) + go func() { s.Rethread(l.ID); close(done) }() + select { + case <-done: + case <-time.After(5 * time.Second): + t.Fatal("Rethread is not linear: a 20000-message chain took more than 5 seconds") + } +}