Files
archie/internal/webui/api_tasks_log_download_test.go
T
sam 21d8250cde feat(webui): read a task's log through the State Store and let it be downloaded
The dashboard's attempt-log panel answered every read with "task logging is
optional and was not enabled for this run". That message was false: logging is
wired unconditionally in the daemon, and the log files exist. The dashboard
process simply had no reader -- composition left TaskLogs absent because no
contract carried it -- so the handler saw a nil registry and reported the
disabled shape, which the page rendered as a claim about configuration.

internal/logging now answers whether an attempt has a log at all: Tail sets
Result.Found, so "this attempt has no log" is a value rather than an empty
list indistinguishable from a read that never happened. Its registry gains
TaskLog (a decoded page) and TaskLogContent (the file verbatim, streamed,
because a log is unbounded input), and an absent reader reports
ErrTaskLogsUnavailable rather than an empty page -- a process that cannot read
logs must not implicate the attempt.

Those two answers travel the State Store contract as storecontract.TaskLogStore
(ReadTaskLog, StreamTaskLogContent) and are rehydrated on the client by
(Unavailable, canonical message), so archie-ui composes the reader from its own
store client instead of opening another process's files. The daemon's registry
is wired as the server-side reader in the State Store process only.

On top of that read, GET /api/tasks/{id}/logs/download serves the attempt's log
as a Content-Disposition attachment named task-<id>-attempt-<n>.log, and the
panel offers it. The JSON route keeps its shape and gains found, so the page
states the attempt has no log instead of blaming a setting.

Task log files stay owned by their directory's owner: archiving still removes a
task's log directory, and that removal is the daemon's, not the dashboard's.
2026-09-17 12:12:43 +00:00

148 lines
5.4 KiB
Go

package webui
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/logging"
)
// TestHandleTaskLogsDistinguishesNoLogForTheAttempt covers the dashboard's
// misleading report. A process that HAS a task-log reader and an attempt that
// genuinely has no file on disk is a different answer from a process that
// cannot read logs at all, and only the second one is "logging is not enabled
// here". The response has to carry that difference or the page cannot state it.
func TestHandleTaskLogsDistinguishesNoLogForTheAttempt(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
srv.TaskLogs = logging.NewTaskRegistry(t.TempDir(), logging.NewFeed(10), logging.TaskSinkOptions{})
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
// No log is ever opened for this task: the reader works, the attempt has
// no file.
w := getTaskLogs(t, srv, task.ID, nil)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body %s", w.Code, w.Body)
}
var body struct {
Disabled bool `json:"disabled"`
Found bool `json:"found"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v (%s)", err, w.Body)
}
if body.Disabled {
t.Error("disabled = true for a process that has a task-log reader; that is the report the page turns into \"logging was not enabled for this run\"")
}
if body.Found {
t.Error("found = true for an attempt with no log file on disk")
}
if !strings.Contains(w.Body.String(), `"found"`) {
t.Errorf("body = %s, want an explicit found marker so the page can say the attempt has no log rather than that logging is off", w.Body)
}
}
// TestHandleTaskLogsDownloadServesTheAttemptLog pins the deliverable: the
// endpoint hands back the attempt's log as a file download. The route falling
// through to the SPA asset handler would answer 200 with an HTML page, so this
// asserts on the attachment headers and the body, not merely on the status.
func TestHandleTaskLogsDownloadServesTheAttemptLog(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
logs := logging.NewTaskRegistry(t.TempDir(), logging.NewFeed(10), logging.TaskSinkOptions{})
srv.TaskLogs = logs
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
if err := logs.Open(task.ID, task.Attempt); err != nil {
t.Fatal(err)
}
logs.Write(task.ID, logging.Entry{Level: "ERROR", Message: "gate failed"})
if err := logs.Close(task.ID); err != nil {
t.Fatal(err)
}
url := "/api/tasks/" + strconv.FormatInt(task.ID, 10) + "/logs/download"
req := httptest.NewRequestWithContext(ctx, http.MethodGet, url, nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("GET %s = %d, body %s", url, w.Code, w.Body)
}
wantName := "task-" + strconv.FormatInt(task.ID, 10) + "-attempt-" + strconv.Itoa(task.Attempt) + ".log"
if got := w.Header().Get("Content-Disposition"); got != `attachment; filename="`+wantName+`"` {
t.Errorf("Content-Disposition = %q, want an attachment named %q", got, wantName)
}
if !strings.Contains(w.Body.String(), "gate failed") {
t.Errorf("download body = %s, want the attempt's own log content", w.Body)
}
}
// TestHandleTaskLogsDownloadWithoutALogAnswersNotFound keeps the download
// honest: there is nothing to attach, so it says so rather than handing back a
// zero-byte file that looks like a successful download of nothing.
func TestHandleTaskLogsDownloadWithoutALogAnswersNotFound(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
srv.TaskLogs = logging.NewTaskRegistry(t.TempDir(), logging.NewFeed(10), logging.TaskSinkOptions{})
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
url := "/api/tasks/" + strconv.FormatInt(task.ID, 10) + "/logs/download"
req := httptest.NewRequestWithContext(ctx, http.MethodGet, url, nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("GET %s with no log = %d, want 404", url, w.Code)
}
}
// TestHandleTaskLogsDownloadWithoutAReaderAnswersServiceUnavailable is the
// degradation the boundary permits: a process with no task-log contract answers
// 503 rather than 404, because "I cannot read logs" and "there is no log" are
// not the same claim.
func TestHandleTaskLogsDownloadWithoutAReaderAnswersServiceUnavailable(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "task", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil {
t.Fatal(err)
}
url := "/api/tasks/" + strconv.FormatInt(task.ID, 10) + "/logs/download"
req := httptest.NewRequestWithContext(ctx, http.MethodGet, url, nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusServiceUnavailable {
body, _ := io.ReadAll(w.Result().Body)
t.Fatalf("GET %s without a reader = %d (%s), want 503", url, w.Code, body)
}
}