782 lines
27 KiB
Go
782 lines
27 KiB
Go
package diagnostics
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"math"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
|
)
|
|
|
|
func TestPrepareOutputAllowsInPlaceTranscriptButRejectsReportCollisions(t *testing.T) {
|
|
dir := t.TempDir()
|
|
input := filepath.Join(dir, "input.json")
|
|
if err := os.WriteFile(input, []byte("source"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
alias := filepath.Join(dir, "alias.json")
|
|
if err := os.Symlink(input, alias); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hardlink := filepath.Join(dir, "hardlink.json")
|
|
if err := os.Link(input, hardlink); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, output := range []string{input, alias, hardlink} {
|
|
if _, err := PrepareOutput(OutputSelection{TranscriptPath: output, InputTranscriptPath: input}); err != nil {
|
|
t.Fatalf("in-place output rejected: %s: %v", output, err)
|
|
}
|
|
}
|
|
if _, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(dir, ErrorsFile), ReportDir: dir, ReportingEnabled: true}); err == nil {
|
|
t.Fatal("report/transcript collision accepted")
|
|
}
|
|
if _, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true, ProtectedPaths: []string{filepath.Join(dir, ErrorsFile)}}); err == nil {
|
|
t.Fatal("report/source collision accepted")
|
|
}
|
|
if err := os.Symlink(input, filepath.Join(dir, ErrorsFile)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true, InputTranscriptPath: input}); err == nil {
|
|
t.Fatal("report/input alias accepted")
|
|
}
|
|
if err := os.Remove(filepath.Join(dir, ErrorsFile)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(filepath.Join(dir, MarkdownFile), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true}); err == nil {
|
|
t.Fatal("directory report target accepted")
|
|
}
|
|
if got, err := os.ReadFile(input); err != nil || string(got) != "source" {
|
|
t.Fatalf("source changed: %s %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestPrepareOutputPreservesExistingTranscriptAndChecksParent(t *testing.T) {
|
|
dir := t.TempDir()
|
|
output := filepath.Join(dir, "out.json")
|
|
if err := os.WriteFile(output, []byte("old content"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: output})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := os.ReadFile(output); err != nil || string(got) != "old content" {
|
|
t.Fatalf("preflight truncated output: %s %v", got, err)
|
|
}
|
|
if failures := session.finalize(context.Background(), nil, []byte("new content"), nil); len(failures) != 0 {
|
|
t.Fatal(failures)
|
|
}
|
|
if got, err := os.ReadFile(output); err != nil || string(got) != "new content" {
|
|
t.Fatalf("publication: %s %v", got, err)
|
|
}
|
|
if info, _ := os.Stat(output); info.Mode().Perm() != 0600 {
|
|
t.Fatalf("permission changed: %v", info.Mode())
|
|
}
|
|
if _, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(dir, "missing", "out.json")}); err == nil {
|
|
t.Fatal("absent parent accepted")
|
|
}
|
|
if _, err := PrepareOutput(OutputSelection{TranscriptPath: dir}); err == nil {
|
|
t.Fatal("directory target accepted")
|
|
}
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, entry := range entries {
|
|
if strings.HasPrefix(entry.Name(), ".audita-") {
|
|
t.Fatalf("probe left behind: %s", entry.Name())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOutputPathsUseFilesystemParent(t *testing.T) {
|
|
dir := t.TempDir()
|
|
resolved := filepath.Join(dir, "destination")
|
|
child := filepath.Join(resolved, "child")
|
|
if err := os.MkdirAll(child, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
alias := filepath.Join(dir, "alias")
|
|
if err := os.Symlink(child, alias); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
parent := alias + string(filepath.Separator) + ".."
|
|
output := parent + string(filepath.Separator) + "out.json"
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: output, ReportDir: parent, ReportingEnabled: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := session.Close(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
})
|
|
if err := session.Finish(context.Background(), nil, []byte("transcript"), func(error, []FileFailure) (ReportBundle, error) {
|
|
return ReportBundle{Markdown: []byte("report")}, nil
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for name, want := range map[string]string{"out.json": "transcript", MarkdownFile: "report"} {
|
|
got, err := os.ReadFile(filepath.Join(resolved, name))
|
|
if err != nil || string(got) != want {
|
|
t.Fatalf("%s: %s, %v", name, got, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOutputPublishesThroughTranscriptAndReportSymlinks(t *testing.T) {
|
|
dir := t.TempDir()
|
|
transcript := filepath.Join(dir, "transcript.json")
|
|
report := filepath.Join(dir, MarkdownFile)
|
|
for path, target := range map[string]string{transcript: "actual.json", report: "actual.md"} {
|
|
if err := os.Symlink(target, path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: transcript, ReportDir: dir, ReportingEnabled: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = session.Finish(context.Background(), nil, []byte("transcript"), func(error, []FileFailure) (ReportBundle, error) {
|
|
return ReportBundle{Markdown: []byte("report")}, nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for path, want := range map[string]string{transcript: "transcript", report: "report"} {
|
|
if _, err := os.Readlink(path); err != nil {
|
|
t.Errorf("lost symlink at %q: %v", path, err)
|
|
}
|
|
got, err := os.ReadFile(path)
|
|
if err != nil || string(got) != want {
|
|
t.Fatalf("%s: %s, %v", path, got, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStagingCleansTemporaryResourcesAfterPublicationFailure(t *testing.T) {
|
|
dir := t.TempDir()
|
|
blocked := filepath.Join(dir, "directory-target")
|
|
var file stagedFile
|
|
if err := file.prepare(blocked); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(blocked, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := file.publish(context.Background(), blocked, []byte("new content")); err == nil {
|
|
t.Fatal("directory replacement unexpectedly succeeded")
|
|
}
|
|
if err := file.close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, entry := range entries {
|
|
if strings.HasPrefix(entry.Name(), ".audita-output-") {
|
|
t.Fatalf("replacement left temporary file: %s", entry.Name())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDisabledReportingIgnoresUnusableDirectory(t *testing.T) {
|
|
dir := t.TempDir()
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(dir, "out.json"), ReportDir: filepath.Join(dir, "missing"), ReportingEnabled: false})
|
|
if err != nil || len(session.ReportPaths()) != 0 {
|
|
t.Fatalf("disabled reports probed: %+v %v", session, err)
|
|
}
|
|
if err := session.Initialize(context.Background(), ReportBundle{}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "missing")); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("disabled destination created: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestEnabledReportingRequiresExistingDirectory(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if _, err := PrepareOutput(OutputSelection{ReportDir: filepath.Join(dir, "missing"), ReportingEnabled: true}); err == nil {
|
|
t.Fatal("missing report directory accepted")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "missing")); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("report parent created: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestStdoutModeInitializesAndFinalizesReports(t *testing.T) {
|
|
dir := t.TempDir()
|
|
session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
initial := ReportBundle{Errors: []byte("in progress errors"), Recoveries: []byte("in progress recoveries"), Markdown: []byte("in progress report")}
|
|
finished := ReportBundle{Errors: []byte("completed errors"), Recoveries: []byte("completed recoveries"), Markdown: []byte("completed report")}
|
|
if err := session.Initialize(context.Background(), initial, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if session.TranscriptPath() != "" {
|
|
t.Fatal("stdout mode acquired transcript path")
|
|
}
|
|
for i, path := range session.ReportPaths() {
|
|
got, err := os.ReadFile(path)
|
|
if err != nil || string(got) != string(bundleParts(initial)[i]) {
|
|
t.Fatalf("initial %s: %s %v", path, got, err)
|
|
}
|
|
}
|
|
if failures := session.finalize(context.Background(), nil, nil, func(error, []FileFailure) (ReportBundle, error) { return finished, nil }); len(failures) != 0 {
|
|
t.Fatal(failures)
|
|
}
|
|
for i, path := range session.ReportPaths() {
|
|
got, err := os.ReadFile(path)
|
|
if err != nil || string(got) != string(bundleParts(finished)[i]) {
|
|
t.Fatalf("final %s: %s %v", path, got, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInitializeFailureMarksPriorReportFailedBeforeModelWork(t *testing.T) {
|
|
dir := t.TempDir()
|
|
session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
actual := session.replace
|
|
session.replace = func(ctx context.Context, path string, payload []byte) error {
|
|
if filepath.Base(path) == RecoveriesFile && string(payload) == "initial" {
|
|
return os.ErrPermission
|
|
}
|
|
return actual(ctx, path, payload)
|
|
}
|
|
modelCalled := false
|
|
if err := session.Initialize(context.Background(), ReportBundle{Errors: []byte("initial"), Recoveries: []byte("initial"), Markdown: []byte("initial")}, func(_ error, failures []FileFailure) (ReportBundle, error) {
|
|
if len(failures) != 1 || filepath.Base(failures[0].Path) != RecoveriesFile {
|
|
t.Fatalf("initialization failures: %+v", failures)
|
|
}
|
|
return ReportBundle{Errors: []byte("failed"), Recoveries: []byte("failed"), Markdown: []byte("failed")}, nil
|
|
}); err == nil {
|
|
modelCalled = true
|
|
}
|
|
if modelCalled {
|
|
t.Fatal("model work continued after initialization failure")
|
|
}
|
|
got, err := os.ReadFile(filepath.Join(dir, ErrorsFile))
|
|
if err != nil || string(got) != "failed" {
|
|
t.Fatalf("prior report not failed: %s %v", got, err)
|
|
}
|
|
if got, err := os.ReadFile(filepath.Join(dir, MarkdownFile)); err != nil || string(got) != "failed" {
|
|
t.Fatalf("later report not finalized: %q %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestFinalizationCollectsFailuresAndRetainsPublishedTranscript(t *testing.T) {
|
|
dir := t.TempDir()
|
|
output := filepath.Join(dir, "output.json")
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := session.Initialize(context.Background(), ReportBundle{Errors: []byte("initial"), Recoveries: []byte("initial"), Markdown: []byte("initial")}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
actual := session.replace
|
|
var attempts []string
|
|
session.replace = func(ctx context.Context, path string, payload []byte) error {
|
|
attempts = append(attempts, filepath.Base(path)+"="+string(payload))
|
|
if filepath.Base(path) == ErrorsFile && string(payload) == "complete" {
|
|
return os.ErrPermission
|
|
}
|
|
return actual(ctx, path, payload)
|
|
}
|
|
complete := ReportBundle{Errors: []byte("complete"), Recoveries: []byte("complete"), Markdown: []byte("complete")}
|
|
failed := ReportBundle{Errors: []byte("failed"), Recoveries: []byte("failed"), Markdown: []byte("failed")}
|
|
failures := session.finalize(context.Background(), nil, []byte("transcript"), func(_ error, failures []FileFailure) (ReportBundle, error) {
|
|
if len(failures) > 0 {
|
|
return failed, nil
|
|
}
|
|
return complete, nil
|
|
})
|
|
if len(failures) != 1 || failures[0].Path != filepath.Join(dir, ErrorsFile) {
|
|
t.Fatalf("failures: %+v", failures)
|
|
}
|
|
if got := strings.Join(attempts, ","); got != "errors.yml=complete,recoveries.yml=failed,report.md=failed,errors.yml=failed" {
|
|
t.Fatalf("unexpected publication attempts: %s", got)
|
|
}
|
|
if got, err := os.ReadFile(output); err != nil || string(got) != "transcript" {
|
|
t.Fatalf("transcript erased: %s %v", got, err)
|
|
}
|
|
for _, path := range session.ReportPaths() {
|
|
if got, err := os.ReadFile(path); err != nil || string(got) != "failed" {
|
|
t.Fatalf("failed-status pass %s: %s %v", path, got, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFailedRenderingKeepsInitializedEvidenceAfterStreamError(t *testing.T) {
|
|
dir := t.TempDir()
|
|
delivery := errors.New("stream delivery failed")
|
|
rendering := errors.New("failed report rendering failed")
|
|
session, err := PrepareOutput(OutputSelection{
|
|
Stdout: writerFunc(func([]byte) (int, error) { return 0, delivery }),
|
|
ReportDir: dir, ReportingEnabled: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
initial := ReportBundle{Errors: []byte("initial"), Recoveries: []byte("initial"), Markdown: []byte("initial")}
|
|
if err := session.Initialize(ctx, initial, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
session.replace = func(context.Context, string, []byte) error {
|
|
t.Error("attempted to replace initialized report without a failed bundle")
|
|
return nil
|
|
}
|
|
err = session.Finish(ctx, nil, []byte("transcript"), func(primary error, failures []FileFailure) (ReportBundle, error) {
|
|
if len(failures) != 1 || !errors.Is(failures[0], delivery) {
|
|
t.Fatalf("renderer received %v, %v", primary, failures)
|
|
}
|
|
return ReportBundle{}, rendering
|
|
})
|
|
if !errors.Is(err, delivery) || !errors.Is(err, rendering) {
|
|
t.Fatalf("lost failure causes: %v", err)
|
|
}
|
|
for _, path := range session.ReportPaths() {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil || string(raw) != "initial" {
|
|
t.Fatalf("report at %s: %s (%v)", path, raw, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFailedReportsIncludeLaterDeliveryFailures(t *testing.T) {
|
|
primary := errors.New("processing failed")
|
|
for _, tc := range []struct {
|
|
name string
|
|
primary error
|
|
failFiles []string
|
|
checkFiles []string
|
|
want string
|
|
}{
|
|
{
|
|
name: "later writable report", primary: primary,
|
|
failFiles: []string{ErrorsFile, RecoveriesFile}, checkFiles: []string{MarkdownFile},
|
|
want: "processing failed,errors.yml,recoveries.yml",
|
|
},
|
|
{
|
|
name: "previously written reports", primary: primary,
|
|
failFiles: []string{MarkdownFile}, checkFiles: []string{ErrorsFile, RecoveriesFile},
|
|
want: "processing failed,report.md",
|
|
},
|
|
{
|
|
name: "completion switches to failed status",
|
|
failFiles: []string{RecoveriesFile, MarkdownFile}, checkFiles: []string{ErrorsFile},
|
|
want: "recoveries.yml,report.md",
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
actual := session.replace
|
|
attempts := make(map[string]int)
|
|
session.replace = func(ctx context.Context, path string, payload []byte) error {
|
|
name := filepath.Base(path)
|
|
attempts[name]++
|
|
if slices.Contains(tc.failFiles, name) && attempts[name] == 1 {
|
|
return os.ErrPermission
|
|
}
|
|
return actual(ctx, path, payload)
|
|
}
|
|
failures := session.finalize(context.Background(), tc.primary, nil, func(cause error, failures []FileFailure) (ReportBundle, error) {
|
|
var facts []string
|
|
if cause != nil {
|
|
facts = append(facts, cause.Error())
|
|
}
|
|
for _, failure := range failures {
|
|
facts = append(facts, filepath.Base(failure.Path))
|
|
}
|
|
payload := []byte(strings.Join(facts, ","))
|
|
return ReportBundle{Errors: payload, Recoveries: payload, Markdown: payload}, nil
|
|
})
|
|
if len(failures) != len(tc.failFiles) {
|
|
t.Fatalf("file failures = %v", failures)
|
|
}
|
|
for _, name := range tc.checkFiles {
|
|
got, err := os.ReadFile(filepath.Join(dir, name))
|
|
if err != nil || string(got) != tc.want {
|
|
t.Errorf("%s = %q, %v, want %q", name, got, err, tc.want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type writerFunc func([]byte) (int, error)
|
|
|
|
func (write writerFunc) Write(payload []byte) (int, error) { return write(payload) }
|
|
|
|
func TestCancellationAfterStreamDeliveryUsesOnlyFailedReports(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
delivery := errors.New("late stream error")
|
|
var escaped []byte
|
|
session, err := PrepareOutput(OutputSelection{
|
|
Stdout: writerFunc(func(payload []byte) (int, error) {
|
|
escaped = append(escaped, payload...)
|
|
cancel()
|
|
return len(payload), delivery
|
|
}),
|
|
ReportDir: t.TempDir(), ReportingEnabled: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var attempts int
|
|
session.replace = func(ctx context.Context, path string, payload []byte) error {
|
|
attempts++
|
|
if ctx.Err() != nil || string(payload) != "failed" {
|
|
t.Errorf("unexpected publication for %s: %q, context %v", path, payload, ctx.Err())
|
|
}
|
|
return nil
|
|
}
|
|
err = session.Finish(ctx, nil, []byte("transcript"), func(primary error, failures []FileFailure) (ReportBundle, error) {
|
|
if !errors.Is(primary, context.Canceled) || len(failures) != 1 || !errors.Is(failures[0], delivery) {
|
|
t.Fatalf("renderer received %v, %v", primary, failures)
|
|
}
|
|
return ReportBundle{Errors: []byte("failed"), Recoveries: []byte("failed"), Markdown: []byte("failed")}, nil
|
|
})
|
|
if string(escaped) != "transcript" || attempts != 3 || !errors.Is(err, context.Canceled) || !errors.Is(err, delivery) {
|
|
t.Fatalf("escaped=%q reports=%d error=%v", escaped, attempts, err)
|
|
}
|
|
}
|
|
|
|
func TestCleanupFailureKeepsTemporaryDirectoryForRetry(t *testing.T) {
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: filepath.Join(t.TempDir(), "output.json")})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tempDir := session.transcriptFile.dir
|
|
extra := filepath.Join(tempDir, "extra")
|
|
if err := os.WriteFile(extra, nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
primary := errors.New("processing failed")
|
|
err = session.Finish(context.Background(), primary, nil, nil)
|
|
var failure FileFailure
|
|
if !errors.Is(err, primary) || !errors.As(err, &failure) || failure.Kind != "output cleanup" || session.transcriptFile.dir != tempDir {
|
|
t.Fatalf("cleanup error=%v, directory=%q", err, session.transcriptFile.dir)
|
|
}
|
|
if err := os.Remove(extra); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := session.Close(); err != nil || session.transcriptFile.dir != "" {
|
|
t.Fatalf("retry error=%v, directory=%q", err, session.transcriptFile.dir)
|
|
}
|
|
}
|
|
|
|
func TestDebugRunDirectoryUsesSuppliedIDAndIsRetained(t *testing.T) {
|
|
parent := filepath.Join(t.TempDir(), "debug")
|
|
if _, err := os.Stat(parent); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("unexpected parent: %v", err)
|
|
}
|
|
run, err := NewRunDirectory(parent, "run-123")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if run.Path() != filepath.Join(parent, "run-123") {
|
|
t.Fatalf("run ID changed: %s", run.Path())
|
|
}
|
|
if _, err := NewRunDirectory(parent, "run-123"); err == nil {
|
|
t.Fatal("duplicate run ID accepted")
|
|
}
|
|
if err := run.WriteJSONArtifact(ArtifactCorrectionLedger, map[string]string{"proposal": "edit"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
contents, err := os.ReadFile(filepath.Join(run.Path(), ArtifactCorrectionLedger))
|
|
if err != nil || string(contents) != "{\"proposal\":\"edit\"}\n" {
|
|
t.Fatalf("debug artifact: %s %v", contents, err)
|
|
}
|
|
if _, err := os.Stat(run.Path()); err != nil {
|
|
t.Fatalf("debug directory removed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDebugTranscriptSnapshotsPreserveSourceAndTypedViews(t *testing.T) {
|
|
run, err := NewRunDirectory(t.TempDir(), "run-transcripts")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw := []byte(" [\n {\"id\": 2, \"text\": \"literal\", \"speaker\": \"A\", \"start\": 0, \"end\": 1}\n ] ")
|
|
source, err := schema.ParseSourceTranscriptJSON(raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := run.WriteSourceTranscript(source, raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
normalized := &schema.Transcript{Segments: []schema.Segment{{ID: 2, Text: "normalized"}}}
|
|
if err := run.WriteNormalizedTranscript(normalized); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
acquired, err := os.ReadFile(filepath.Join(run.Path(), ArtifactSourceTranscript))
|
|
if err != nil || !bytes.Equal(acquired, raw) {
|
|
t.Fatalf("acquired source = %q, error = %v", acquired, err)
|
|
}
|
|
for name, want := range map[string]string{ArtifactParsedSourceTranscript: "literal", ArtifactNormalizedTranscript: "normalized"} {
|
|
payload, err := os.ReadFile(filepath.Join(run.Path(), name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var transcript schema.Transcript
|
|
if err := json.Unmarshal(payload, &transcript); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(transcript.Segments) != 1 || transcript.Segments[0].ID != 2 || transcript.Segments[0].Text != want {
|
|
t.Fatalf("%s: %s", name, payload)
|
|
}
|
|
}
|
|
if err := run.WriteJSONArtifact("invalid.json", math.NaN()); err == nil {
|
|
t.Fatal("expected serialization failure")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(run.Path(), "invalid.json")); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("failed artifact persisted: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSelectionAndAbortPreserveTranscript(t *testing.T) {
|
|
dir := t.TempDir()
|
|
output := filepath.Join(dir, "output.json")
|
|
if err := os.WriteFile(output, []byte("original"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
session, err := SelectOutput(OutputSelection{TranscriptPath: output})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if entries, err := os.ReadDir(dir); err != nil || len(entries) != 1 {
|
|
t.Fatalf("selection changed directory: %v %v", entries, err)
|
|
}
|
|
if err := session.Prepare(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if session.transcriptFile.file == nil {
|
|
t.Fatal("temporary output not open")
|
|
}
|
|
temp := session.transcriptFile.path
|
|
if err := session.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(temp); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("unpublished file remains: %v", err)
|
|
}
|
|
if data, err := os.ReadFile(output); err != nil || string(data) != "original" {
|
|
t.Fatalf("output changed: %q %v", data, err)
|
|
}
|
|
}
|
|
|
|
func TestStdoutAndDeviceDelivery(t *testing.T) {
|
|
var stdout bytes.Buffer
|
|
session, err := PrepareOutput(OutputSelection{Stdout: &stdout})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if failures := session.finalize(context.Background(), nil, []byte("transcript"), nil); len(failures) != 0 {
|
|
t.Fatal(failures)
|
|
}
|
|
if stdout.String() != "transcript" {
|
|
t.Fatalf("stdout=%q", stdout.String())
|
|
}
|
|
session, err = PrepareOutput(OutputSelection{TranscriptPath: os.DevNull})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !session.direct || session.transcriptFile.file != nil {
|
|
t.Fatal("device was not deferred")
|
|
}
|
|
if failures := session.finalize(context.Background(), nil, []byte("discard"), nil); len(failures) != 0 {
|
|
t.Fatal(failures)
|
|
}
|
|
}
|
|
|
|
func TestFinishRetainsPrimaryFailureAndAttemptsReports(t *testing.T) {
|
|
dir := t.TempDir()
|
|
output := filepath.Join(dir, "transcript.json")
|
|
if err := os.WriteFile(output, []byte("original"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: output, ReportDir: dir, ReportingEnabled: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
originalReplace := session.replace
|
|
session.replace = func(ctx context.Context, path string, data []byte) error {
|
|
if filepath.Base(path) == ErrorsFile {
|
|
return os.ErrPermission
|
|
}
|
|
return originalReplace(ctx, path, data)
|
|
}
|
|
primary := errors.New("processing failed")
|
|
bundle := ReportBundle{Errors: []byte("failed"), Recoveries: []byte("failed"), Markdown: []byte("failed")}
|
|
result := session.Finish(context.Background(), primary, []byte("unwanted"), func(error, []FileFailure) (ReportBundle, error) { return bundle, nil })
|
|
if !errors.Is(result, primary) || !errors.Is(result, os.ErrPermission) {
|
|
t.Fatalf("combined error: %v", result)
|
|
}
|
|
if data, err := os.ReadFile(output); err != nil || string(data) != "original" {
|
|
t.Fatalf("output=%q err=%v", data, err)
|
|
}
|
|
if data, err := os.ReadFile(filepath.Join(dir, MarkdownFile)); err != nil || string(data) != "failed" {
|
|
t.Fatalf("remaining report=%q err=%v", data, err)
|
|
}
|
|
}
|
|
|
|
func TestOptionalDebugPreparationReportsProblems(t *testing.T) {
|
|
dir := t.TempDir()
|
|
file := filepath.Join(dir, "not-directory")
|
|
if err := os.WriteFile(file, []byte("x"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if run, problems := PrepareDebug(file, "run", InvocationMetadata{}, nil); run != nil || len(problems) == 0 {
|
|
t.Fatalf("run=%v problems=%v", run, problems)
|
|
}
|
|
run, problems := PrepareDebug(filepath.Join(dir, "debug"), "run", InvocationMetadata{Operation: "process"}, []byte("version: 3\n"))
|
|
if len(problems) != 0 {
|
|
t.Fatal(problems)
|
|
}
|
|
contents, err := os.ReadFile(filepath.Join(run.Path(), ArtifactEffectiveConfig))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(contents) != "version: 3\n" {
|
|
t.Fatalf("changed effective configuration: %q", contents)
|
|
}
|
|
}
|
|
|
|
type shortOutput struct{}
|
|
|
|
func (shortOutput) Write(b []byte) (int, error) { return len(b) / 2, nil }
|
|
|
|
func TestShortStdoutWriteIsFailure(t *testing.T) {
|
|
session, err := PrepareOutput(OutputSelection{Stdout: shortOutput{}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
failures := session.finalize(context.Background(), nil, []byte("transcript"), nil)
|
|
if len(failures) != 1 || !errors.Is(failures[0], io.ErrShortWrite) {
|
|
t.Fatalf("failures=%v", failures)
|
|
}
|
|
}
|
|
|
|
func TestTranscriptWriteFailureCleansTemporaryResource(t *testing.T) {
|
|
dir := t.TempDir()
|
|
output := filepath.Join(dir, "out.json")
|
|
if err := os.WriteFile(output, []byte("original"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: output})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
temp := session.transcriptFile.path
|
|
if err := session.transcriptFile.file.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
failures := session.finalize(context.Background(), nil, []byte("new"), nil)
|
|
if len(failures) == 0 {
|
|
t.Fatal("closed output accepted")
|
|
}
|
|
if _, err := os.Stat(temp); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("temporary output remains: %v", err)
|
|
}
|
|
if data, err := os.ReadFile(output); err != nil || string(data) != "original" {
|
|
t.Fatalf("output=%q err=%v", data, err)
|
|
}
|
|
}
|
|
|
|
func TestNamedPipeIsNotOpenedDuringPreparation(t *testing.T) {
|
|
pipe := filepath.Join(t.TempDir(), "output-pipe")
|
|
makeNamedPipe(t, pipe)
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: pipe})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !session.direct || session.transcriptFile.file != nil {
|
|
t.Fatal("pipe was not deferred")
|
|
}
|
|
if err := session.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestLateNamedPipeIsPreserved(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "output")
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: path})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer session.Close()
|
|
makeNamedPipe(t, path)
|
|
err = session.Finish(context.Background(), nil, []byte("transcript"), nil)
|
|
if err == nil || !strings.Contains(err.Error(), "changed type") {
|
|
t.Fatalf("expected clear delivery failure, got %v", err)
|
|
}
|
|
info, err := os.Stat(path)
|
|
if err != nil || info.Mode()&os.ModeNamedPipe == 0 {
|
|
t.Fatalf("late pipe was lost: %v %v", info, err)
|
|
}
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil || len(entries) != 1 {
|
|
t.Fatalf("unpublished output resources remain: %v %v", entries, err)
|
|
}
|
|
}
|
|
|
|
func TestStreamChangedToRegularFileIsPreserved(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "output")
|
|
makeNamedPipe(t, path)
|
|
session, err := PrepareOutput(OutputSelection{TranscriptPath: path})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer session.Close()
|
|
if err := os.Remove(path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte("existing content"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = session.Finish(context.Background(), nil, []byte("new"), nil)
|
|
if err == nil || !strings.Contains(err.Error(), "no longer a stream") {
|
|
t.Fatalf("expected clear delivery failure, got %v", err)
|
|
}
|
|
if data, err := os.ReadFile(path); err != nil || string(data) != "existing content" {
|
|
t.Fatalf("replacement file changed: %q %v", data, err)
|
|
}
|
|
}
|
|
|
|
func makeNamedPipe(t *testing.T, path string) {
|
|
t.Helper()
|
|
tool, err := exec.LookPath("mkfifo")
|
|
if err != nil {
|
|
t.Skip("mkfifo is unavailable")
|
|
}
|
|
if output, err := exec.Command(tool, path).CombinedOutput(); err != nil {
|
|
t.Fatalf("mkfifo: %v %s", err, output)
|
|
}
|
|
}
|