diff --git a/cmd/workspace/task_test.go b/cmd/workspace/task_test.go index de9b1756e1..1c612ffc54 100644 --- a/cmd/workspace/task_test.go +++ b/cmd/workspace/task_test.go @@ -1,15 +1,27 @@ package workspace import ( + "encoding/json" + "io" + "os" + "strings" "testing" "time" "github.com/devsy-org/devsy/pkg/devcontainer/config" "github.com/devsy-org/devsy/pkg/status" "github.com/devsy-org/devsy/pkg/task" + "github.com/stretchr/testify/require" ) -const testResultContainerID = "abc123" +const ( + testResultContainerID = "abc123" + testBuild = "build" + testPhase = "building_image" + testOp = "op-1" + testStarted = "started" + testPlain = "task task-1: building_image\n" +) func TestResultEnvelopeFrom_NilResult(t *testing.T) { got := resultEnvelopeFrom(&task.State{}) @@ -18,6 +30,159 @@ func TestResultEnvelopeFrom_NilResult(t *testing.T) { } } +type taskTransitionCase struct { + name string + change func(*task.State) + first, jsonSilent bool + phase, step, operationID string + state status.State + duration int64 + plain string +} + +func taskTransitionCases() []taskTransitionCase { + return []taskTransitionCase{ + {"nil-last", nil, true, false, testPhase, testBuild, testOp, testStarted, 100, testPlain}, + {"identical", nil, false, true, "", "", "", "", 0, ""}, + { + "duration-only", func(s *task.State) { s.DurationMs = 250 }, false, false, + testPhase, testBuild, testOp, testStarted, 250, "", + }, + { + "phase-only", + func(s *task.State) { s.Phase = "running_command" }, + false, + false, + "running_command", + testBuild, + testOp, + testStarted, + 100, + "task task-1: running_command\n", + }, + { + "step-only", func(s *task.State) { s.Step = "execute" }, false, false, + testPhase, "execute", testOp, testStarted, 100, testPlain, + }, + { + "status-only", func(s *task.State) { s.Status = task.StatusSucceeded }, false, false, + testPhase, testBuild, testOp, "succeeded", 100, testPlain, + }, + { + "operationID-only", func(s *task.State) { s.OperationID = "op-2" }, false, false, + testPhase, testBuild, "op-2", testStarted, 100, testPlain, + }, + {"empty-phase", func(s *task.State) { s.Phase = "" }, false, true, "", "", "", "", 0, ""}, + } +} + +func TestEmitTaskTransition(t *testing.T) { + for _, mode := range []struct { + name string + json bool + }{{"JSON", true}, {"plain", false}} { + t.Run(mode.name, func(t *testing.T) { + for _, tc := range taskTransitionCases() { + t.Run(tc.name, func(t *testing.T) { + last, current := taskTransitionFixture(), taskTransitionFixture() + if tc.first { + last = nil + } + if tc.change != nil { + tc.change(current) + } + output := captureTaskTransitionStdout( + t, + func() { emitTaskTransition(last, current, mode.json) }, + ) + switch { + case !mode.json: + require.Equal(t, tc.plain, output) + case tc.jsonSilent: + require.Equal(t, "", output) + default: + checkTaskTransitionJSON(t, output, config.StatusEnvelope{ + Kind: "status", SchemaVersion: 1, ParentOperationID: "parent-1", + Phase: tc.phase, Step: tc.step, OperationID: tc.operationID, + State: tc.state, DurationMs: tc.duration, + }) + } + }) + } + }) + } +} + +func taskTransitionFixture() *task.State { + return &task.State{ + ID: "task-1", + Phase: testPhase, + Step: testBuild, + Status: task.StatusRunning, + OperationID: testOp, + ParentOperationID: "parent-1", + DurationMs: 100, + } +} + +func captureTaskTransitionStdout(t *testing.T, emit func()) string { + t.Helper() + reader, writer, err := os.Pipe() + require.NoError(t, err) + original := os.Stdout + restore := func() { + if os.Stdout == writer { + os.Stdout = original + } + } + readerClosed, writerClosed := false, false + defer func() { + restore() + if !writerClosed { + _ = writer.Close() + } + if !readerClosed { + _ = reader.Close() + } + }() + os.Stdout = writer + func() { + defer restore() + emit() + }() + writeCloseErr := writer.Close() + writerClosed = true + output, readErr := io.ReadAll(reader) + readCloseErr := reader.Close() + readerClosed = true + require.NoError(t, writeCloseErr) + require.NoError(t, readErr) + require.NoError(t, readCloseErr) + return string(output) +} + +func checkTaskTransitionJSON(t *testing.T, output string, expected config.StatusEnvelope) { + t.Helper() + require.NotEmpty(t, output) + require.Equal(t, byte('\n'), output[len(output)-1]) + require.Equal(t, 1, strings.Count(output, "\n")) + var fields map[string]json.RawMessage + require.NoError(t, json.Unmarshal([]byte(output), &fields)) + require.Len(t, fields, 8) + for _, key := range []string{ + "kind", "schemaVersion", "operationId", "parentOperationId", + "phase", "step", "state", "durationMs", + } { + require.Contains(t, fields, key) + } + decoder := json.NewDecoder(strings.NewReader(output)) + var got config.StatusEnvelope + require.NoError(t, decoder.Decode(&got)) + require.Equal(t, expected, got) + var extra any + require.ErrorIs(t, decoder.Decode(&extra), io.EOF) +} + func TestResultEnvelopeFrom_PopulatedResult(t *testing.T) { state := &task.State{ Result: &config.Result{