Files
archie/internal/infrastructure/staterpc/task_log_nilslice_test.go
T
sam a484693f11 refactor(logging): pass the caller's context to TaskRegistry.Write
Write built its own context.Background() for the handler call, which meant
any new caller with a context to offer could not hand it over. Taking the
caller's context is what the next commit's park-reason writer needs, and it
is what contextcheck requires of a daemon-side writer.

No behaviour change: the context is used for the handler's own attribute
extraction and never consulted for cancellation -- a park is recorded at
exactly the moment a run's context has been cancelled, so a cancelled
context must not suppress the entry.
2026-09-18 01:49:42 +10:00

92 lines
3.2 KiB
Go

package staterpc
import (
"encoding/json"
"path/filepath"
"testing"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/store"
)
// A decoded task-log page must never carry a nil slice for a collection field.
// Go marshals a nil slice to JSON null, and null is not an empty list: a
// client that reads the field as an array sees a different type than the same
// route serves when the collection is non-empty.
//
// This is the nil-slice class the contract-conformance audit deferred (its
// named revisit condition is a handler that can produce nil). The local reader
// already normalises both fields; the wire mapping did not, because protobuf
// decodes an empty `repeated` field to nil, so the grpc path regressed what the
// local path got right. Asserting the JSON shape rather than the slice catches
// it on both paths without depending on which one is under test.
func TestTaskLogPageCarriesNoNilCollectionFields(t *testing.T) {
// Both collections must be exercised empty. A log with entries but no
// structured "component" field leaves only Components empty; an attempt
// whose log file exists but holds no lines leaves only Entries empty, and
// that second shape is what catches an empty-slice mapping regressing on
// the entries side.
for _, tc := range []struct {
name string
write bool
}{
{name: "entries present, components empty", write: true},
{name: "entries empty, components empty", write: false},
} {
for _, mode := range []string{"local", "grpc"} {
t.Run(tc.name+"/"+mode, func(t *testing.T) {
ctx := t.Context()
baseDir := filepath.Join(t.TempDir(), "logs", "tasks")
reader := logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{})
const taskID = 7
if err := reader.Open(taskID, 0); err != nil {
t.Fatalf("open sink: %v", err)
}
if tc.write {
// No "component" field at all: Components stays empty while
// Entries does not.
reader.Write(ctx, taskID, logging.Entry{Level: "INFO", Message: "starting"})
}
if err := reader.Close(taskID); err != nil {
t.Fatalf("close sink: %v", err)
}
var c taskLogContract = reader
if mode == "grpc" {
c = remoteTaskStore(t, store.OpenTest(t), reader)
}
page, err := c.TaskLog(ctx, taskID, 0, logging.Query{})
if err != nil {
t.Fatalf("TaskLog: %v", err)
}
if !page.Found {
t.Fatal("Found = false for an attempt whose log file exists")
}
if tc.write && len(page.Entries) != 1 {
t.Fatalf("entries = %d, want the line the sink wrote", len(page.Entries))
}
if !tc.write && len(page.Entries) != 0 {
t.Fatalf("entries = %d, want none for a log with no lines", len(page.Entries))
}
raw, err := json.Marshal(page)
if err != nil {
t.Fatalf("marshal page: %v", err)
}
var decoded map[string]json.RawMessage
if err := json.Unmarshal(raw, &decoded); err != nil {
t.Fatalf("unmarshal page: %v", err)
}
for _, field := range []string{"entries", "components"} {
if got := string(decoded[field]); got == "null" {
t.Errorf("%s marshalled to null, want an empty list: the field's type must not depend on whether it has contents",
field)
}
}
})
}
}
}