From 896ba7052167632e9a41924e5b21e27559080396 Mon Sep 17 00:00:00 2001 From: skovranek <59619403+skovranek@users.noreply.github.com> Date: Wed, 4 Nov 2026 17:02:05 -0500 Subject: [PATCH 1/2] refactor output Clarify numbered checks and simplify failure output Preserve skipped step details and show submission errors Preserve grading diagnostics beneath failed checks Preserve unmatched grading errors and bound diagnostics changes test changes --- README.md | 16 ++- checks/http.go | 13 +- checks/http_test.go | 33 +++++ checks/jq.go | 2 +- checks/jq_test.go | 2 +- checks/local.go | 2 +- checks/runner.go | 10 +- checks/runner_test.go | 45 +++++++ client/lessons.go | 11 +- cmd/configure.go | 14 +- cmd/localtest.go | 58 ++++++++- cmd/localtest_test.go | 43 ++++++ cmd/submit.go | 49 +++---- main_test.go | 37 ++++-- render/header.go | 38 ++++++ render/http.go | 63 +++++---- render/json.go | 67 ++++++++++ render/json_test.go | 98 ++++++++++++++ render/models.go | 3 + render/palette.go | 12 ++ render/render.go | 49 ++++--- render/step.go | 134 +++++++++++++++++++ render/step_test.go | 148 +++++++++++++++++++++ render/variables.go | 8 +- render/variables_test.go | 2 +- render/view.go | 201 +++++++++++----------------- render/view_test.go | 275 ++++++++++++++++++++++++++++++++++++++- 27 files changed, 1199 insertions(+), 234 deletions(-) create mode 100644 render/header.go create mode 100644 render/json.go create mode 100644 render/json_test.go create mode 100644 render/palette.go create mode 100644 render/step.go create mode 100644 render/step_test.go diff --git a/README.md b/README.md index d0b27ff..b423241 100644 --- a/README.md +++ b/README.md @@ -175,15 +175,25 @@ For lessons with HTTP tests, you can configure the CLI with a base URL that over ### CLI colors -The CLI text output is rendered with extra colors: green (e.g., success messages), red (e.g., error messages), and gray (e.g., secondary text). +CLI colors are configured by their role. - To customize these colors, run: ```sh - bootdev config colors --red VALUE --green VALUE --gray VALUE --magenta VALUE --yellow VALUE + bootdev config colors \ + --header VALUE \ + --subheader VALUE \ + --information VALUE \ + --warning VALUE \ + --pass VALUE \ + --fail VALUE \ + --secondary VALUE \ + --accent VALUE ``` - _You can use an [ANSI color code](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) or a hex string as the `VALUE`._ + _You can use an [ANSI palette color index](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) (`0`–`255`, such as `6` for cyan) or a hex string (such as `#7AA2F7`) as the `VALUE`. Palette indexes `0`–`15` follow your terminal's theme._ + +- Alternatively, edit the `color` section in your active configuration file (such as `~/.bootdev.yaml`). Omitted keys use the default colors. - To get the current colors, run: diff --git a/checks/http.go b/checks/http.go index ab58ea5..e5c57c8 100644 --- a/checks/http.go +++ b/checks/http.go @@ -31,6 +31,9 @@ func runHTTPRequest( captured := make(map[string]string) defer func() { result.Err = publishCaptures(captureNames(api.CLIStep{HTTPRequest: &requestStep}), variables, captured, result.Err) + if result.Err != "" && result.ErrorCategory == "" { + result.ErrorCategory = api.HTTPErrorResponse + } result.Variables = maps.Clone(variables) result.Request = requestStep }() @@ -49,7 +52,7 @@ func runHTTPRequest( bodyJSON := interpolateJSONStrings(requestStep.Request.BodyJSON, variables) dat, err := json.Marshal(bodyJSON) if err != nil { - return api.HTTPRequestResult{Err: fmt.Sprintf("Failed to marshal request body: %s", err)} + return api.HTTPRequestResult{Err: fmt.Sprintf("Failed to marshal request body: %s", err), ErrorCategory: api.HTTPErrorRequest} } requestBody = bytes.NewReader(dat) contentType = "application/json" @@ -66,7 +69,7 @@ func runHTTPRequest( req, err := http.NewRequest(requestStep.Request.Method, completeURL, requestBody) if err != nil { - return api.HTTPRequestResult{Err: fmt.Sprintf("Failed to create request: %s", err)} + return api.HTTPRequestResult{Err: fmt.Sprintf("Failed to create request: %s", err), ErrorCategory: api.HTTPErrorRequest} } if contentType != "" { req.Header.Set("Content-Type", contentType) @@ -92,14 +95,14 @@ func runHTTPRequest( resp, err := requestClient.Do(req) if err != nil { errString := fmt.Sprintf("Failed to fetch: %s", err.Error()) - result = api.HTTPRequestResult{Err: errString} + result = api.HTTPRequestResult{Err: errString, ErrorCategory: api.HTTPErrorExchange} return result } defer resp.Body.Close() body, err := io.ReadAll(io.LimitReader(resp.Body, maxHTTPResponseBodyBytes+1)) if err != nil { - result = api.HTTPRequestResult{Err: "Failed to read response body"} + result = api.HTTPRequestResult{Err: fmt.Sprintf("Failed to read response body: %s", err), ErrorCategory: api.HTTPErrorResponse} return result } @@ -122,10 +125,12 @@ func runHTTPRequest( } if err := parseVariables([]byte(bodyString), requestStep.ResponseVariables, captured); err != nil { result.Err = fmt.Sprintf("Failed to parse response variable: %s", err) + result.ErrorCategory = api.HTTPErrorResponse return result } if err := parseHeaderVariables(headers, requestStep.ResponseHeaderVariables, captured); err != nil { result.Err = fmt.Sprintf("Failed to parse response header variable: %s", err) + result.ErrorCategory = api.HTTPErrorResponse } return result diff --git a/checks/http_test.go b/checks/http_test.go index 6535960..0d6ff7d 100644 --- a/checks/http_test.go +++ b/checks/http_test.go @@ -193,6 +193,39 @@ func TestRunHTTPRequestReportsInvalidRequest(t *testing.T) { if !strings.Contains(result.Err, "Failed to create request") { t.Fatalf("runHTTPRequest() error = %q, want request creation error", result.Err) } + if result.ErrorCategory != api.HTTPErrorRequest { + t.Fatalf("unexpected error category: %s", result.ErrorCategory) + } +} + +func TestRunHTTPRequestCategorizesResponseErrors(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/incomplete" { + w.Header().Set("Content-Length", "100") + } + _, _ = w.Write([]byte(`{}`)) + })) + defer server.Close() + for _, tt := range []struct { + name string + path string + variables []api.HTTPRequestResponseVariable + want api.HTTPErrorCategory + }{ + {name: "incomplete response", path: "/incomplete", want: api.HTTPErrorResponse}, + {name: "invalid extraction", path: "/", variables: []api.HTTPRequestResponseVariable{{Name: "value", Path: ".["}}, want: api.HTTPErrorResponse}, + {name: "missing capture", path: "/", variables: []api.HTTPRequestResponseVariable{{Name: "value", Path: ".missing"}}, want: api.HTTPErrorResponse}, + } { + t.Run(tt.name, func(t *testing.T) { + result := runHTTPRequest(server.Client(), server.URL, map[string]string{}, api.CLIStepHTTPRequest{ + Request: api.HTTPRequest{Method: http.MethodGet, FullURL: server.URL + tt.path}, + ResponseVariables: tt.variables, + }) + if result.Err == "" || result.ErrorCategory != tt.want { + t.Fatalf("category = %q, want %q; error = %q", result.ErrorCategory, tt.want, result.Err) + } + }) + } } func TestTruncateAndStringifyBodyCapsBinaryBody(t *testing.T) { diff --git a/checks/jq.go b/checks/jq.go index 7053a7d..38f2270 100644 --- a/checks/jq.go +++ b/checks/jq.go @@ -236,7 +236,7 @@ func compareInt(actual int, expected int, operator api.JqOperator) bool { } } -func valFromJqPath(path string, jsn string) (any, error) { +func JSONValueAtPath(path string, jsn string) (any, error) { vals, err := valsFromJqPath(path, jsn) if err != nil { return nil, err diff --git a/checks/jq_test.go b/checks/jq_test.go index 2177282..1faf0a2 100644 --- a/checks/jq_test.go +++ b/checks/jq_test.go @@ -60,7 +60,7 @@ func TestRunStdoutJqQuery(t *testing.T) { } func TestValFromJqPathRejectsMultipleValues(t *testing.T) { - _, err := valFromJqPath(`.items[].id`, `{"items":[{"id":1},{"id":2}]}`) + _, err := JSONValueAtPath(`.items[].id`, `{"items":[{"id":1},{"id":2}]}`) if err == nil { t.Fatal("expected error for multiple values") } diff --git a/checks/local.go b/checks/local.go index cc77934..cea5a43 100644 --- a/checks/local.go +++ b/checks/local.go @@ -253,7 +253,7 @@ func capturedVariableMatches(vars map[string]string, name, expectedValue string) } func jsonValOp(test api.HTTPRequestTestJSONValue, jsn string, variables map[string]string) error { - val, err := valFromJqPath(test.Path, jsn) + val, err := JSONValueAtPath(test.Path, jsn) if err != nil { return err } diff --git a/checks/runner.go b/checks/runner.go index 16c6ea4..11f418c 100644 --- a/checks/runner.go +++ b/checks/runner.go @@ -63,7 +63,15 @@ func CLIChecks(cliData api.CLIData, options RunOptions, send func(tea.Msg)) ([]a delete(variables, name) } results[i].DependencyFailure = &api.DependencyFailure{Names: missing} - send(messages.StartStepMsg{Description: step.Description, NoPenaltyOnFail: step.NoPenaltyOnFail}) + startMessage := messages.StartStepMsg{Description: step.Description, NoPenaltyOnFail: step.NoPenaltyOnFail} + if step.CLICommand != nil { + startMessage.CMD = step.CLICommand.Command + startMessage.TmdlQuery = step.CLICommand.StdoutFilterTmdl + } else if step.HTTPRequest != nil { + startMessage.Method = step.HTTPRequest.Request.Method + startMessage.URL = step.HTTPRequest.Request.FullURL + } + send(startMessage) send(messages.ResolveStepMsg{Index: i, Result: &results[i]}) continue } diff --git a/checks/runner_test.go b/checks/runner_test.go index 809ef4d..46cd84a 100644 --- a/checks/runner_test.go +++ b/checks/runner_test.go @@ -132,6 +132,51 @@ func TestCLIChecksReturnsManifestErrors(t *testing.T) { } } +func TestSkippedStepsPreserveCommandAndRequestDetails(t *testing.T) { + command := `printf '%s' '${token}'` + requestURL := "${baseURL}/reports/${token}" + query := "report" + cliData := api.CLIData{ + BaseURLDefault: "http://localhost:1", + Steps: []api.CLIStep{ + {Description: "Skipped command", CLICommand: &api.CLIStepCLICommand{Command: command, StdoutFilterTmdl: &query}}, + {Description: "Skipped request", HTTPRequest: &api.CLIStepHTTPRequest{Request: api.HTTPRequest{Method: http.MethodGet, FullURL: requestURL}}}, + {CLICommand: &api.CLIStepCLICommand{ + Command: "echo 'token=ready'", + StdoutVariables: []api.CLICommandStdoutVariable{{Name: "token", Regex: "token=(.*)"}}, + }}, + }, + } + var starts []messages.StartStepMsg + results, err := CLIChecks(cliData, RunOptions{}, func(msg tea.Msg) { + if start, ok := msg.(messages.StartStepMsg); ok { + starts = append(starts, start) + } + }) + if err != nil { + t.Fatal(err) + } + if len(starts) != 3 || len(results) != 3 { + t.Fatalf("expected three step messages and results") + } + for _, index := range []int{0, 1} { + result := results[index] + if result.DependencyFailure == nil || len(result.DependencyFailure.Names) != 1 || result.DependencyFailure.Names[0] != "token" || + result.CLICommandResult != nil || result.HTTPRequestResult != nil { + t.Fatalf("step %d should be skipped without execution: %+v", index+1, result) + } + } + if starts[0].CMD != command || starts[0].TmdlQuery == nil || *starts[0].TmdlQuery != query || starts[0].Description != "Skipped command" { + t.Fatalf("skipped command lost its details: %+v", starts[0]) + } + if starts[1].Method != http.MethodGet || starts[1].URL != requestURL || starts[1].Description != "Skipped request" { + t.Fatalf("skipped request lost its details: %+v", starts[1]) + } + if results[2].CLICommandResult == nil || results[2].CLICommandResult.Variables["token"] != "ready" { + t.Fatal("independent later step did not execute") + } +} + func TestApplySubmissionResultsMarksAllStepsAndTestsPassedWhenNoFailure(t *testing.T) { cliData := api.CLIData{Steps: []api.CLIStep{ {CLICommand: &api.CLIStepCLICommand{Tests: []api.CLICommandTest{{}, {}}}}, diff --git a/client/lessons.go b/client/lessons.go index 0e00cc2..7b0c9ca 100644 --- a/client/lessons.go +++ b/client/lessons.go @@ -225,8 +225,17 @@ type CLICommandJqOutput struct { Error string } +type HTTPErrorCategory string + +const ( + HTTPErrorRequest HTTPErrorCategory = "request" + HTTPErrorExchange HTTPErrorCategory = "http" + HTTPErrorResponse HTTPErrorCategory = "response" +) + type HTTPRequestResult struct { - Err string `json:"FetchErr,omitempty"` + ErrorCategory HTTPErrorCategory `json:"-"` + Err string `json:"FetchErr,omitempty"` StatusCode int ResponseHeaders map[string]string ResponseTrailers map[string]string diff --git a/cmd/configure.go b/cmd/configure.go index 4228d40..84357de 100644 --- a/cmd/configure.go +++ b/cmd/configure.go @@ -4,6 +4,7 @@ import ( "fmt" "net/url" + "github.com/bootdotdev/bootdev/render" "github.com/charmbracelet/lipgloss" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -18,11 +19,14 @@ var configureCmd = &cobra.Command{ } var defaultColors = map[string]string{ - "gray": "8", - "red": "1", - "green": "2", - "magenta": "5", - "yellow": "#f6bd45", + "secondary": render.DefaultSecondaryColor, + "fail": render.DefaultFailColor, + "pass": render.DefaultPassColor, + "accent": render.DefaultAccentColor, + "warning": render.DefaultWarningColor, + "header": render.DefaultHeaderColor, + "subheader": render.DefaultSubheaderColor, + "information": render.DefaultInformationColor, } // configureColorsCmd represents the `configure colors` command for changing diff --git a/cmd/localtest.go b/cmd/localtest.go index 18c9cb4..b42e246 100644 --- a/cmd/localtest.go +++ b/cmd/localtest.go @@ -7,6 +7,8 @@ import ( "path/filepath" "runtime" "slices" + "strconv" + "strings" "github.com/bootdotdev/bootdev/checks" api "github.com/bootdotdev/bootdev/client" @@ -19,6 +21,7 @@ import ( func init() { rootCmd.AddCommand(localTestCmd) localTestCmd.Flags().Bool("ignore-os", false, "skip the lesson's allowed operating systems check") + localTestCmd.Flags().Bool("run", false, "run local lesson commands without grading") localTestCmd.Flags().String("shell", "", "shell for lesson commands: sh or pwsh (default depends on host OS)") localTestCmd.Flags().BoolVarP(&verboseOutput, "verbose", "v", false, "show detailed final output for every step") } @@ -33,6 +36,7 @@ var localTestCmd = &cobra.Command{ func localTestHandler(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true + cmd.SilenceErrors = false data, err := readLocalCLIData(args[0]) if err != nil { @@ -51,14 +55,23 @@ func localTestHandler(cmd *cobra.Command, args []string) error { if err != nil { return err } + runOnly, err := cmd.Flags().GetBool("run") + if err != nil { + return err + } overrideBaseURL := viper.GetString("override_base_url") + runContext := localRunContext(args[0]) + if runOnly { + runContext.Action = "Running lesson:" + } + runContext.BaseURL = data.BaseURLDefault if overrideBaseURL != "" { - fmt.Printf("Using overridden base_url: %v\n", overrideBaseURL) - fmt.Printf("You can reset to the default with `bootdev config base_url --reset`\n\n") + runContext.BaseURL = overrideBaseURL + runContext.BaseURLOverridden = true } - send, finish := render.StartRenderer(true, verboseOutput, false) + send, finish := render.StartRenderer(!runOnly, verboseOutput, false, runContext) submissionEvent := api.LessonSubmissionEvent{} defer func() { finish(submissionEvent) @@ -68,16 +81,55 @@ func localTestHandler(cmd *cobra.Command, args []string) error { if err != nil { return err } + if runOnly { + return nil + } submissionEvent = checks.LocalSubmissionEvent(data, cliResults) checks.ApplySubmissionResults(data, submissionEvent.StructuredErrCLI, send) if submissionEvent.ResultSlug != api.VerificationResultSlugSuccess { + // The renderer already reports the failure. Suppress Cobra's duplicate + // error message while returning the error to preserve a nonzero exit status. + cmd.SilenceErrors = true return localTestFailureError(submissionEvent.StructuredErrCLI) } return nil } +func localRunContext(path string) render.RunContext { + manifestPath := filepath.Clean(path) + if absolutePath, err := filepath.Abs(manifestPath); err == nil { + manifestPath = absolutePath + } + if info, err := os.Stat(manifestPath); err == nil && info.IsDir() { + manifestPath = filepath.Join(manifestPath, "cli.yaml") + } + lessonDirectory := filepath.Dir(manifestPath) + context := render.RunContext{ + Action: "Submitting lesson:", + LessonTitle: filepath.Base(lessonDirectory), + } + if readme, err := os.ReadFile(filepath.Join(lessonDirectory, "readme.md")); err == nil { + for _, line := range strings.Split(string(readme), "\n") { + if strings.HasPrefix(line, "# ") { + if title := strings.TrimSpace(strings.TrimPrefix(line, "# ")); title != "" { + context.LessonTitle = title + } + break + } + } + } + chapterPrefix, _, chapterFound := strings.Cut(filepath.Base(filepath.Dir(lessonDirectory)), "-") + lessonPrefix, _, lessonFound := strings.Cut(filepath.Base(lessonDirectory), "-") + chapterNumber, chapterErr := strconv.Atoi(chapterPrefix) + lessonNumber, lessonErr := strconv.Atoi(lessonPrefix) + if chapterFound && lessonFound && chapterErr == nil && lessonErr == nil && chapterNumber > 0 && lessonNumber > 0 { + context.LessonTitle = fmt.Sprintf("%d.%d - %s", chapterNumber, lessonNumber, context.LessonTitle) + } + return context +} + func localTestFailureError(failure *api.StructuredErrCLI) error { if failure == nil { return errors.New("local checks failed") diff --git a/cmd/localtest_test.go b/cmd/localtest_test.go index 1fd6973..2226d4c 100644 --- a/cmd/localtest_test.go +++ b/cmd/localtest_test.go @@ -28,6 +28,49 @@ func TestReadLocalCLIDataAcceptsLessonDirectory(t *testing.T) { } } +func TestLocalRunContext(t *testing.T) { + for _, tt := range []struct { + name string + readme string + wantTitle string + }{ + {name: "lesson", readme: "# Sorting Chirps\n", wantTitle: "Sorting Chirps"}, + {name: "missing README"}, + {name: "README without title", readme: "No title"}, + } { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + if tt.readme != "" { + if err := os.WriteFile(filepath.Join(dir, "readme.md"), []byte(tt.readme), 0o600); err != nil { + t.Fatal(err) + } + } + title := tt.wantTitle + if title == "" { + title = filepath.Base(dir) + } + if got := localRunContext(dir); got.LessonTitle != title { + t.Fatalf("title = %q, want %q", got.LessonTitle, title) + } + }) + } + t.Run("numbered lesson paths", func(t *testing.T) { + dir := filepath.Join(t.TempDir(), "9-documentation", "3-sorting-chirps") + if err := os.MkdirAll(dir, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "readme.md"), []byte("# Sorting Chirps\n"), 0o600); err != nil { + t.Fatal(err) + } + t.Chdir(dir) + for _, path := range []string{dir, filepath.Join(dir, "cli.yaml"), ".", "cli.yaml", "./cli.yaml"} { + if got := localRunContext(path); got.LessonTitle != "9.3 - Sorting Chirps" { + t.Errorf("path %q: unexpected title %q", path, got.LessonTitle) + } + } + }) +} + func TestLocalTestShellDoesNotBypassOSValidation(t *testing.T) { for _, allowedOS := range []string{"[]", "[unsupported-os]"} { t.Run(allowedOS, func(t *testing.T) { diff --git a/cmd/submit.go b/cmd/submit.go index 6aca1c2..ad74b83 100644 --- a/cmd/submit.go +++ b/cmd/submit.go @@ -58,8 +58,6 @@ func submissionHandler(cmd *cobra.Command, args []string) error { ) { return errors.New("submission canceled") } - } else { - fmt.Printf("Running next lesson:\n%s\n", lessonLabel) } lessonUUID = nextLesson.LessonUUID } @@ -69,39 +67,35 @@ func submissionHandler(cmd *cobra.Command, args []string) error { return err } - if len(args) > 0 { - action := "Running" - if isSubmit { - action = "Submitting" - } - fmt.Printf( - "%s lesson:\n%d.%d - %s\n", - action, - lesson.ChapterNumber, - lesson.LessonNumber, - lesson.Lesson.Title, - ) + lessonTitle := fmt.Sprintf("%d.%d - %s", lesson.ChapterNumber, lesson.LessonNumber, lesson.Lesson.Title) + lessonAction := lessonRunAction(isSubmit, len(args) == 0) + reportValidationError := func(err error) error { + fmt.Printf("%s\n%s\n", lessonAction, lessonTitle) + return err } - if lesson.Lesson.Type != "type_cli" { - return errors.New("unable to run lesson: unsupported lesson type") + return reportValidationError(errors.New("unable to run lesson: unsupported lesson type")) } if lesson.Lesson.LessonDataCLI == nil { - return errors.New("unable to run lesson: missing lesson data") + return reportValidationError(errors.New("unable to run lesson: missing lesson data")) } data := lesson.Lesson.LessonDataCLI.CLIData if err := validateAllowedOS(data); err != nil { - return err + return reportValidationError(err) } overrideBaseURL := viper.GetString("override_base_url") + runContext := render.RunContext{ + Action: lessonAction, + LessonTitle: lessonTitle, + BaseURL: data.BaseURLDefault, + } if overrideBaseURL != "" { - fmt.Printf("Using overridden base_url: %v\n", overrideBaseURL) - fmt.Printf("You can reset to the default with `bootdev config base_url --reset`\n\n") + runContext.BaseURL = overrideBaseURL + runContext.BaseURLOverridden = true } - - send, finish := render.StartRenderer(isSubmit, verboseOutput, isSubmit && len(args) > 0) + send, finish := render.StartRenderer(isSubmit, verboseOutput, isSubmit && len(args) > 0, runContext) finalEvent := api.LessonSubmissionEvent{} var debugPath string var debugWriteErr error @@ -142,6 +136,17 @@ func applySubmissionEvent(data api.CLIData, event api.LessonSubmissionEvent, sen return nil } +func lessonRunAction(isSubmit, isNext bool) string { + action := "Running" + if isSubmit { + action = "Submitting" + } + if isNext { + return action + " next lesson:" + } + return action + " lesson:" +} + func reportDebugFileWrite(path string, err error) { if err != nil { fmt.Fprintf(os.Stderr, "warning: failed to write submission debug output: %v\n", err) diff --git a/main_test.go b/main_test.go index 316e1e9..b767674 100644 --- a/main_test.go +++ b/main_test.go @@ -32,11 +32,14 @@ func TestLocalTestRedirectedOutput(t *testing.T) { name string fail bool verbose bool + runOnly bool }{ - {"pass verbose", false, true}, - {"fail verbose", true, true}, - {"pass compact", false, false}, - {"fail compact", true, false}, + {"pass verbose", false, true, false}, + {"fail verbose", true, true, false}, + {"pass compact", false, false, false}, + {"fail compact", true, false, false}, + {"run matching expectation", false, false, true}, + {"run mismatched expectation", true, false, true}, } { t.Run(tt.name, func(t *testing.T) { wantExit := 0 @@ -54,6 +57,10 @@ func TestLocalTestRedirectedOutput(t *testing.T) { if tt.verbose { args = append(args, "--verbose") } + if tt.runOnly { + args = append(args, "--run") + wantExit = 0 + } cmd := exec.CommandContext(ctx, binary, args...) var stdout, stderr bytes.Buffer cmd.Stdout, cmd.Stderr = &stdout, &stderr @@ -68,14 +75,26 @@ func TestLocalTestRedirectedOutput(t *testing.T) { if strings.Count(output, "Output check") != 1 { t.Fatalf("expected one final report: %s", output) } - for _, diagnostic := range []string{"Command stdout:\n\nstdout-marker", "Command stderr:\n\nstderr-marker"} { - if got, want := strings.Contains(output, diagnostic), tt.verbose || tt.fail; got != want { + for _, diagnostic := range []string{"Standard output\n stdout-marker", "Standard error\n stderr-marker"} { + if got, want := strings.Contains(output, diagnostic), tt.verbose || tt.fail || tt.runOnly; got != want { t.Fatalf("diagnostic %q present = %t, want %t; output: %s", diagnostic, got, want, output) } } - if tt.fail { - if !strings.Contains(stderr.String(), "local checks failed") { - t.Fatalf("missing failure error: %s", &stderr) + if tt.runOnly { + if !strings.Contains(output, "Running lesson:") || stderr.Len() != 0 { + t.Fatalf("unexpected run output: %s; stderr: %s", output, &stderr) + } + for _, forbidden := range []string{"PASS", "FAIL", "All tests passed!", "Submitting lesson:"} { + if strings.Contains(output, forbidden) { + t.Fatalf("run exposed %q: %s", forbidden, output) + } + } + } else if tt.fail { + if !strings.Contains(output, "FAIL") || !strings.Contains(output, "Expected:") || !strings.Contains(output, "Actual:") { + t.Fatalf("missing failed check or its comparison: %s", output) + } + if stderr.Len() != 0 { + t.Fatalf("duplicate failure error: %s", &stderr) } } else if stderr.Len() != 0 || !strings.Contains(output, "All tests passed!") { t.Fatalf("unexpected success output: stdout=%q stderr=%q", output, stderr.String()) diff --git a/render/header.go b/render/header.go new file mode 100644 index 0000000..f25b8eb --- /dev/null +++ b/render/header.go @@ -0,0 +1,38 @@ +package render + +import ( + "fmt" + "strings" + + "github.com/charmbracelet/lipgloss" +) + +type RunContext struct { + Action string + LessonTitle string + BaseURL string + BaseURLOverridden bool +} + +func renderRunHeader(context RunContext) string { + if context.LessonTitle == "" { + return "" + } + var str strings.Builder + action := context.Action + if action == "" { + action = "Running lesson:" + } + fmt.Fprintln(&str, action) + fmt.Fprintln(&str, lipgloss.NewStyle().Bold(true).Render(context.LessonTitle)) + if context.BaseURL != "" { + if context.BaseURLOverridden { + fmt.Fprintf(&str, "%s %s\n", warningStyle.Render("Using overridden base_url:"), context.BaseURL) + fmt.Fprintln(&str, informationStyle.Render("You can reset to the default with `bootdev config base_url --reset`")) + } else { + fmt.Fprintf(&str, "%s %s\n", secondaryStyle.Render("base_url:"), context.BaseURL) + } + } + str.WriteByte('\n') + return str.String() +} diff --git a/render/http.go b/render/http.go index 925b835..2aa4554 100644 --- a/render/http.go +++ b/render/http.go @@ -1,6 +1,7 @@ package render import ( + "bytes" "fmt" "net/http" "strings" @@ -10,13 +11,24 @@ import ( "github.com/goccy/go-json" ) +func httpErrorHeading(category api.HTTPErrorCategory) string { + switch category { + case api.HTTPErrorRequest: + return "Request error" + case api.HTTPErrorResponse: + return "Response error" + default: + return "HTTP error" + } +} + func printHTTPRequestResult(result api.HTTPRequestResult) string { if result.Err != "" { - return fmt.Sprintf(" Err: %v\n\n", result.Err) + return fmt.Sprintf("%s\n%s\n", warningStyle.Render(httpErrorHeading(result.ErrorCategory)), indentOutput(result.Err, 2)) } var str strings.Builder - fmt.Fprintf(&str, " Response Status Code: %v\n", result.StatusCode) + fmt.Fprintf(&str, "%s %v\n", subheaderStyle.Render("Response status:"), result.StatusCode) filteredHeaders := make(map[string]string) for respK, respV := range result.ResponseHeaders { @@ -54,37 +66,36 @@ func printHTTPRequestResult(result api.HTTPRequestResult) string { } if len(filteredHeaders) > 0 { - str.WriteString(" Response Headers: \n") + str.WriteString("\n" + subheaderStyle.Render("Response headers") + "\n") for k, v := range filteredHeaders { fmt.Fprintf(&str, " - %v: %v\n", k, v) } } - str.WriteString(" Response Body: \n") - bytes := []byte(result.BodyString) - contentType := http.DetectContentType(bytes) - if contentType == "application/json" || strings.HasPrefix(contentType, "text/") { - body := result.BodyString - var unmarshalled any - err := json.Unmarshal([]byte(result.BodyString), &unmarshalled) - if err == nil { - pretty, err := json.MarshalIndent(unmarshalled, "", " ") - if err == nil { - body = string(pretty) + str.WriteString("\n" + subheaderStyle.Render("Response body") + "\n") + if result.BodyString == "" { + str.WriteString(" " + secondaryStyle.Render("") + "\n") + } else { + var pretty bytes.Buffer + bytes := []byte(result.BodyString) + contentType := http.DetectContentType(bytes) + if contentType == "application/json" || strings.HasPrefix(contentType, "text/") { + body := result.BodyString + if err := json.Indent(&pretty, bytes, "", " "); err == nil { + body = pretty.String() } + str.WriteString(indentOutput(truncateVisualOutput(body), 2)) + } else { + str.WriteString(informationStyle.Render(fmt.Sprintf( + "Binary %s file. Raw data hidden. To manually debug, use curl -o myfile.bin and inspect the file", + contentType, + ))) } - str.WriteString(truncateVisualOutput(body)) - } else { - fmt.Fprintf( - &str, - "Binary %s file. Raw data hidden. To manually debug, use curl -o myfile.bin and inspect the file", - contentType, - ) + str.WriteByte('\n') } - str.WriteByte('\n') if len(filteredTrailers) > 0 { - str.WriteString(" Response Trailers: \n") + str.WriteString("\n" + subheaderStyle.Render("Response trailers") + "\n") for k, v := range filteredTrailers { fmt.Fprintf(&str, " - %v: %v\n", k, v) } @@ -92,14 +103,14 @@ func printHTTPRequestResult(result api.HTTPRequestResult) string { savedVariables, missingVariables := savedAndMissingVariablesForHTTPResult(result) if len(savedVariables) > 0 { - str.WriteString(renderVariableSection("Variables Saved", savedVariables)) + str.WriteString(renderVariableSection("Variables Saved", savedVariables, subheaderStyle)) } if len(missingVariables) > 0 { - str.WriteString(renderVariableSection("Variables Missing", missingVariables)) + str.WriteString(renderVariableSection("Variables Missing", missingVariables, warningStyle)) } availableVariables, expectsVariables := availableVariablesForHTTPResult(result) if expectsVariables { - str.WriteString(renderVariableSection("Variables Available", availableVariables)) + str.WriteString(renderVariableSection("Variables Available", availableVariables, subheaderStyle)) } str.WriteByte('\n') diff --git a/render/json.go b/render/json.go new file mode 100644 index 0000000..9f02546 --- /dev/null +++ b/render/json.go @@ -0,0 +1,67 @@ +package render + +import ( + "bytes" + "fmt" + "strings" + + "github.com/bootdotdev/bootdev/checks" + api "github.com/bootdotdev/bootdev/client" + "github.com/goccy/go-json" +) + +func renderJSONMismatch(test api.HTTPRequestTestJSONValue, result api.HTTPRequestResult, evaluated bool, title string) string { + var expected any + switch { + case test.BoolValue != nil: + expected = *test.BoolValue + case test.IntValue != nil: + expected = *test.IntValue + case test.StringValue != nil: + expected = checks.InterpolateVariables(*test.StringValue, result.Variables) + default: + return "" + } + operator := "" + switch test.Operator { + case api.OpEquals: + case api.OpGreaterThan: + operator = "> " + case api.OpContains: + operator = "contains " + case api.OpNotContains: + operator = "does not contain " + default: + return "" + } + expectedJSON, err := formatJSONValue(expected) + if err != nil { + return "" + } + expectedText := truncateVisualOutput(operator + expectedJSON) + if !evaluated { + return renderCheckValues(title, expectedText, "", false) + } + actual, err := checks.JSONValueAtPath(test.Path, result.BodyString) + actualText := "" + if err != nil { + actualText = fmt.Sprintf("unavailable (%s)", err) + } else { + actualJSON, err := formatJSONValue(actual) + if err != nil { + return "" + } + actualText = actualJSON + } + return renderCheckValues(title, expectedText, truncateVisualOutput(actualText), true) +} + +func formatJSONValue(value any) (string, error) { + var buffer bytes.Buffer + encoder := json.NewEncoder(&buffer) + encoder.SetEscapeHTML(false) + if err := encoder.Encode(value); err != nil { + return "", err + } + return strings.TrimSuffix(buffer.String(), "\n"), nil +} diff --git a/render/json_test.go b/render/json_test.go new file mode 100644 index 0000000..9b3959b --- /dev/null +++ b/render/json_test.go @@ -0,0 +1,98 @@ +package render + +import ( + "strings" + "testing" + + api "github.com/bootdotdev/bootdev/client" +) + +func TestFailedJSONCheckShowsSelectedValue(t *testing.T) { + expectedString := "Darn that fly, I just wanna cook" + variableString := "${name}" + expectedInt := 3 + expectedBool := true + expectedHTML := "Hello & goodbye" + for _, tt := range []struct { + name string + test api.HTTPRequestTestJSONValue + body string + want string + }{ + {name: "array field", test: api.HTTPRequestTestJSONValue{Path: ".[0].body", Operator: api.OpEquals, StringValue: &expectedString}, body: `[{"body":"I'm the one who knocks!"}]`, want: "JSON .[0].body\n Expected: \"Darn that fly, I just wanna cook\"\n Actual: \"I'm the one who knocks!\""}, + {name: "interpolation", test: api.HTTPRequestTestJSONValue{Path: ".name", Operator: api.OpEquals, StringValue: &variableString}, body: `{"name":"other"}`, want: "Expected: \"Walt\"\n Actual: \"other\""}, + {name: "integer comparison", test: api.HTTPRequestTestJSONValue{Path: ".count", Operator: api.OpGreaterThan, IntValue: &expectedInt}, body: `{"count":2}`, want: "Expected: > 3\n Actual: 2"}, + {name: "boolean", test: api.HTTPRequestTestJSONValue{Path: ".ok", Operator: api.OpEquals, BoolValue: &expectedBool}, body: `{"ok":false}`, want: "Expected: true\n Actual: false"}, + {name: "HTML characters", test: api.HTTPRequestTestJSONValue{Path: ".html", Operator: api.OpEquals, StringValue: &expectedHTML}, body: `{"html":"Hello & goodbye"}`, want: "Expected: \"Hello & goodbye\"\n Actual: \"Hello & goodbye\""}, + {name: "type mismatch", test: api.HTTPRequestTestJSONValue{Path: ".count", Operator: api.OpEquals, IntValue: &expectedInt}, body: `{"count":"3"}`, want: "Expected: 3\n Actual: \"3\""}, + {name: "contains", test: api.HTTPRequestTestJSONValue{Path: ".name", Operator: api.OpContains, StringValue: &variableString}, body: `{"name":"other"}`, want: "Expected: contains \"Walt\""}, + {name: "not contains", test: api.HTTPRequestTestJSONValue{Path: ".name", Operator: api.OpNotContains, StringValue: &variableString}, body: `{"name":"Walter"}`, want: "Expected: does not contain \"Walt\"\n Actual: \"Walter\""}, + {name: "missing", test: api.HTTPRequestTestJSONValue{Path: ".name", Operator: api.OpEquals, StringValue: &variableString}, body: `{}`, want: "Actual: unavailable (value not found)"}, + {name: "multiple results", test: api.HTTPRequestTestJSONValue{Path: ".[].name", Operator: api.OpEquals, StringValue: &variableString}, body: `[{"name":"one"},{"name":"two"}]`, want: "Actual: unavailable (invalid number of values found)"}, + {name: "invalid JSON", test: api.HTTPRequestTestJSONValue{Path: ".name", Operator: api.OpEquals, StringValue: &variableString}, body: `not json`, want: "Actual: unavailable ("}, + {name: "invalid query", test: api.HTTPRequestTestJSONValue{Path: ".[", Operator: api.OpEquals, StringValue: &variableString}, body: `{}`, want: "Actual: unavailable ("}, + } { + t.Run(tt.name, func(t *testing.T) { + step := stepModel{ + tests: []testModel{{text: "original condition"}}, + result: &api.CLIStepResult{HTTPRequestResult: &api.HTTPRequestResult{ + BodyString: tt.body, Variables: map[string]string{"name": "Walt"}, + Request: api.CLIStepHTTPRequest{Tests: []api.HTTPRequestTest{{JSONValue: &tt.test}}}, + }}, + } + passed, failed := true, false + got, _ := renderCheckDetails(step, 0, &failed) + if !strings.HasPrefix(got, "FAIL · 1. JSON ") || !strings.Contains(got, tt.want) { + t.Fatalf("expected %q in:\n%s", tt.want, got) + } + check, _ := renderCheckDetails(step, 0, &passed) + if strings.TrimPrefix(check, "PASS · ") != strings.TrimPrefix(got, "FAIL · ") { + t.Fatalf("check details should not depend on grading status: %s", check) + } + unevaluated, _ := renderCheckDetails(step, 0, nil) + if strings.Contains(unevaluated, "Actual:") || !strings.Contains(unevaluated, "Expected:") { + t.Fatalf("unevaluated check should show only the expectation: %s", unevaluated) + } + }) + } +} + +func TestCompoundJSONCheckRetainsOtherConditions(t *testing.T) { + expectedStatus, expectedInt := 200, 3 + step := stepModel{ + tests: []testModel{{text: "status and JSON conditions"}}, + result: &api.CLIStepResult{HTTPRequestResult: &api.HTTPRequestResult{ + Request: api.CLIStepHTTPRequest{Tests: []api.HTTPRequestTest{{ + StatusCode: &expectedStatus, + JSONValue: &api.HTTPRequestTestJSONValue{Path: ".count", Operator: api.OpEquals, IntValue: &expectedInt}, + }}}, + }}, + } + failed := false + if got, _ := renderCheckDetails(step, 0, &failed); got != "FAIL · 1. status and JSON conditions" { + t.Fatalf("compound assertion lost context: %s", got) + } +} + +func TestJSONCheckTruncatesLargeExpectedAndActualValues(t *testing.T) { + expected := strings.Repeat("界", 20000) + actual := strings.Repeat("x", 20000) + test := api.HTTPRequestTestJSONValue{Path: ".value", Operator: api.OpContains, StringValue: &expected} + result := api.HTTPRequestResult{BodyString: `{"value":"` + actual + `"}`} + for _, evaluated := range []bool{false, true} { + got := renderJSONMismatch(test, result, evaluated, "JSON .value") + wantMarkers := 1 + if evaluated { + wantMarkers = 2 + } + if strings.Count(got, "... output visually truncated") != wantMarkers { + t.Fatalf("evaluated=%v: expected %d truncation markers", evaluated, wantMarkers) + } + if strings.Contains(got, strings.Repeat("界", 5120)) || strings.Contains(got, strings.Repeat("x", 5120)) { + t.Fatalf("evaluated=%v: JSON value exceeds visual limit", evaluated) + } + if strings.Contains(got, "Actual:") != evaluated { + t.Fatalf("evaluated=%v: unexpected actual value visibility", evaluated) + } + } +} diff --git a/render/models.go b/render/models.go index dc135de..a0d3a14 100644 --- a/render/models.go +++ b/render/models.go @@ -14,6 +14,7 @@ type testModel struct { type stepModel struct { description string detail string + detailLabel string passed *bool result *api.CLIStepResult finished bool @@ -23,6 +24,8 @@ type stepModel struct { } type rootModel struct { + runContext RunContext + terminalWidth int steps []stepModel spinner spinner.Model result api.VerificationResultSlug diff --git a/render/palette.go b/render/palette.go new file mode 100644 index 0000000..0b23d5b --- /dev/null +++ b/render/palette.go @@ -0,0 +1,12 @@ +package render + +const ( + DefaultHeaderColor = "#7AA2F7" // Blue + DefaultSubheaderColor = "#A9B1D6" // Light lavender gray + DefaultInformationColor = "#0DB9D7" // Cyan + DefaultWarningColor = "#E0AF68" // Amber + DefaultPassColor = "#9ECE6A" // Soft green + DefaultFailColor = "#F7768E" // Coral pink + DefaultSecondaryColor = "#737AA2" // Muted blue gray + DefaultAccentColor = "#BB9AF7" // Lavender purple +) diff --git a/render/render.go b/render/render.go index 672237c..c7a2c46 100644 --- a/render/render.go +++ b/render/render.go @@ -15,27 +15,34 @@ import ( ) var ( - green lipgloss.Style - red lipgloss.Style - magenta lipgloss.Style - yellow lipgloss.Style - gray lipgloss.Style - white lipgloss.Style - borderBox = lipgloss.NewStyle().Border(lipgloss.RoundedBorder()) + passStyle lipgloss.Style + failStyle lipgloss.Style + accentStyle lipgloss.Style + warningStyle lipgloss.Style + secondaryStyle lipgloss.Style + headerStyle lipgloss.Style + subheaderStyle lipgloss.Style + informationStyle lipgloss.Style ) func (m rootModel) Init() tea.Cmd { - green = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.green"))) - red = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.red"))) - magenta = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.magenta"))) - yellow = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.yellow"))) - gray = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.gray"))) - white = lipgloss.NewStyle().Foreground(lipgloss.Color("15")) + passStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.pass"))) + failStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.fail"))) + accentStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.accent"))) + warningStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.warning"))) + secondaryStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.secondary"))) + headerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.header"))) + subheaderStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.subheader"))) + informationStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(viper.GetString("color.information"))) return m.spinner.Tick } func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { + case tea.WindowSizeMsg: + m.terminalWidth = msg.Width + return m, nil + case messages.DoneStepMsg: m.result = msg.Result m.failure = msg.Failure @@ -46,20 +53,22 @@ func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case messages.StartStepMsg: description := strings.TrimSpace(msg.Description) - detail := fmt.Sprintf("Command: %s", msg.CMD) + detailLabel := "Command" + detail := msg.CMD if msg.TmdlQuery != nil { detail += fmt.Sprintf(" (TMDL query: '%s')", *msg.TmdlQuery) } if msg.CMD == "" { - detail = fmt.Sprintf("Request: %s %s", msg.Method, msg.URL) + detailLabel = "Request" + detail = fmt.Sprintf("%s %s", msg.Method, msg.URL) } if description == "" { - description = strings.TrimPrefix(detail, "Command: ") - description = strings.TrimPrefix(description, "Request: ") + description = detail } m.steps = append(m.steps, stepModel{ description: description, detail: detail, + detailLabel: detailLabel, tests: []testModel{}, noPenaltyOnFail: msg.NoPenaltyOnFail, }) @@ -106,8 +115,12 @@ func (m rootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } -func StartRenderer(isSubmit bool, verbose bool, showOmitLessonIDTip bool) (func(tea.Msg), func(api.LessonSubmissionEvent)) { +func StartRenderer(isSubmit bool, verbose bool, showOmitLessonIDTip bool, runContext RunContext) (func(tea.Msg), func(api.LessonSubmissionEvent)) { m := initModel(isSubmit, verbose) + if terminalWidth, _, err := term.GetSize(int(os.Stdout.Fd())); err == nil { + m.terminalWidth = terminalWidth + } + m.runContext = runContext m.showOmitLessonIDTip = showOmitLessonIDTip interactive := term.IsTerminal(int(os.Stdin.Fd())) && term.IsTerminal(int(os.Stdout.Fd())) options := []tea.ProgramOption{tea.WithoutSignalHandler(), tea.WithInput(nil)} diff --git a/render/step.go b/render/step.go new file mode 100644 index 0000000..7f1ace1 --- /dev/null +++ b/render/step.go @@ -0,0 +1,134 @@ +package render + +import ( + "fmt" + "strings" + + api "github.com/bootdotdev/bootdev/client" +) + +func renderStepDetail(label, detail string) string { + if label != "" { + return subheaderStyle.Render(label+":") + " " + detail + } + return detail +} + +func indentOutput(text string, spaces int) string { + prefix := strings.Repeat(" ", spaces) + lines := strings.Split(strings.TrimRight(text, "\n"), "\n") + for index, line := range lines { + if line != "" { + lines[index] = prefix + line + } + } + return strings.Join(lines, "\n") +} + +func renderDetailedStep(step stepModel, number int, isSubmit bool, failure *api.StructuredErrCLI) string { + var str strings.Builder + headingStyle := headerStyle.Bold(true) + fmt.Fprintf(&str, "%s\n%s\n", headingStyle.Render(fmt.Sprintf("STEP %d · %s", number, step.description)), renderStepDetail(step.detailLabel, step.detail)) + if step.noPenaltyOnFail { + str.WriteString(secondaryStyle.Render("Safe step") + "\n") + } + gradingFailureHandled := false + if isSubmit && len(step.tests) > 0 { + str.WriteString("\n " + subheaderStyle.Render("Checks:") + "\n") + for index, test := range step.tests { + check, explainsFailure := renderCheckDetails(step, index, test.passed) + fmt.Fprintf(&str, "\n%s\n", indentOutput(check, 4)) + if test.passed != nil && !*test.passed && failure != nil && + failure.FailedStepIndex == number-1 && failure.FailedTestIndex == index { + gradingFailureHandled = true + if !explainsFailure && failure.ErrorMessage != "" { + str.WriteString(renderGradingError(failure.ErrorMessage, 6)) + } + } + } + } + if isSubmit && !gradingFailureHandled && failure != nil && failure.FailedStepIndex == number-1 && failure.ErrorMessage != "" { + stepErrorShown := false + if result := step.result; result != nil { + stepErrorShown = result.DependencyFailure != nil || + (result.CLICommandResult != nil && result.CLICommandResult.Err != "") || + (result.HTTPRequestResult != nil && result.HTTPRequestResult.Err != "") + } + if !stepErrorShown { + str.WriteByte('\n') + str.WriteString(renderGradingError(failure.ErrorMessage, 2)) + } + } + if step.result != nil { + str.WriteString(renderStepResult(step, isSubmit)) + } + if step.sleepAfter != "" { + fmt.Fprintf(&str, "\n %s\n", informationStyle.Render(step.sleepAfter)) + } + str.WriteByte('\n') + return str.String() +} + +func renderStatus(status string) string { + switch status { + case "PASS": + return passStyle.Bold(true).Render(status) + case "FAIL": + return failStyle.Bold(true).Render(status) + default: + return secondaryStyle.Render(status) + } +} + +func renderGradingError(message string, spaces int) string { + return indentOutput(warningStyle.Render("Error:")+" "+truncateVisualOutput(message), spaces) + "\n" +} + +// Only simple numeric comparisons fully explain the grader's failure. +// JSON extraction, types, and truncated values may need additional diagnostics. +func renderCheckDetails(step stepModel, index int, passed *bool) (string, bool) { + if step.result != nil { + if result := step.result.CLICommandResult; result != nil && index < len(result.Command.Tests) { + test := result.Command.Tests[index] + if isExitCodeOnlyCheck(test) { + return renderCheckValues(renderCheckTitle("Exit code", index, passed), fmt.Sprint(*test.ExitCode), fmt.Sprint(result.ExitCode), passed != nil), passed != nil + } + } + if result := step.result.HTTPRequestResult; result != nil && index < len(result.Request.Tests) { + test := result.Request.Tests[index] + if test.JSONValue != nil && test.StatusCode == nil && test.BodyContains == nil && test.BodyContainsNone == nil && test.HeadersEqual == nil && test.HeadersContain == nil && test.TrailersEqual == nil && test.TrailersContain == nil { + if detail := renderJSONMismatch(*test.JSONValue, *result, passed != nil, renderCheckTitle("JSON "+test.JSONValue.Path, index, passed)); detail != "" { + return detail, false + } + } + if test.StatusCode != nil && test.BodyContains == nil && test.BodyContainsNone == nil && test.HeadersEqual == nil && test.HeadersContain == nil && test.TrailersEqual == nil && test.TrailersContain == nil && test.JSONValue == nil { + return renderCheckValues(renderCheckTitle("HTTP status", index, passed), fmt.Sprint(*test.StatusCode), fmt.Sprint(result.StatusCode), passed != nil), passed != nil + } + } + } + return renderCheckTitle(step.tests[index].text, index, passed), false +} + +func isExitCodeOnlyCheck(test api.CLICommandTest) bool { + return test.ExitCode != nil && test.StdoutContainsAll == nil && test.StdoutContainsNone == nil && test.StdoutLinesGT == nil && test.StdoutJq == nil +} + +func renderCheckTitle(title string, index int, passed *bool) string { + numberedTitle := fmt.Sprintf("%d. %s", index+1, title) + if passed == nil { + return numberedTitle + } + status := "PASS" + if !*passed { + status = "FAIL" + } + return renderStatus(status) + " · " + numberedTitle +} + +func renderCheckValues(title, expected, actual string, evaluated bool) string { + detail := fmt.Sprintf("%s\n %s %s", title, secondaryStyle.Render("Expected:"), expected) + if evaluated { + detail += fmt.Sprintf("\n %s %s", secondaryStyle.Render("Actual:"), actual) + } + return detail +} diff --git a/render/step_test.go b/render/step_test.go new file mode 100644 index 0000000..da94ddc --- /dev/null +++ b/render/step_test.go @@ -0,0 +1,148 @@ +package render + +import ( + "strings" + "testing" + + api "github.com/bootdotdev/bootdev/client" + "github.com/bootdotdev/bootdev/messages" +) + +func TestStepDetailsKeepLabelsSeparateFromValues(t *testing.T) { + for _, tt := range []struct { + name string + message messages.StartStepMsg + label string + value string + }{ + {name: "command containing label text", message: messages.StartStepMsg{CMD: "Request: literal command"}, label: "Command", value: "Request: literal command"}, + {name: "request", message: messages.StartStepMsg{Method: "GET", URL: "http://localhost/Command:"}, label: "Request", value: "GET http://localhost/Command:"}, + } { + t.Run(tt.name, func(t *testing.T) { + model := initModel(false, false) + updated, _ := model.Update(tt.message) + model = updated.(rootModel) + model.finalized = true + view := model.View() + if want := "\n" + tt.label + ": " + tt.value + "\n"; !strings.Contains(view, want) { + t.Fatalf("missing detail %q in:\n%s", want, view) + } + }) + } +} + +func TestDetailedStepGroupsFailureAndDiagnostics(t *testing.T) { + passed, failed := true, false + expectedExit := 0 + step := stepModel{ + description: "Read log", detailLabel: "Command", detail: "cat repl.log", + passed: &failed, + tests: []testModel{ + {text: "earlier passing check", passed: &passed}, + {text: "Expect exit code 0", passed: &failed}, + {text: "Expect stdout to contain:\n greeting"}, + }, + result: &api.CLIStepResult{CLICommandResult: &api.CLICommandResult{ + ExitCode: 1, Stderr: "missing file\ncheck path", + Command: api.CLIStepCLICommand{Tests: []api.CLICommandTest{{}, {ExitCode: &expectedExit}, {}}}, + }}, + } + view := renderDetailedStep(step, 1, true, nil) + for _, expected := range []string{ + "STEP 1 · Read log\nCommand: cat repl.log", + " Checks:", + " FAIL · 2. Exit code\n Expected: 0\n Actual: 1", + " Standard error\n missing file\n check path", + " Standard output\n ", + " 3. Expect stdout to contain:\n greeting", + } { + if !strings.Contains(view, expected) { + t.Errorf("missing %q in:\n%s", expected, view) + } + } + previous := -1 + for _, marker := range []string{"PASS · 1. earlier passing check", "FAIL · 2. Exit code", "3. Expect stdout", "Standard output"} { + position := strings.Index(view, marker) + if position <= previous { + t.Fatalf("%q missing or out of order in:\n%s", marker, view) + } + previous = position + } + if strings.Contains(view, "Command exit code") { + t.Errorf("duplicate exit-code diagnostic in:\n%s", view) + } + if strings.Contains(view, "FAIL · STEP") { + t.Errorf("expanded step heading repeats check status:\n%s", view) + } +} + +func TestOnlySubmitShowsCheckDetails(t *testing.T) { + passed := true + expectedStatus := 200 + step := stepModel{ + description: "Health", detailLabel: "Request", detail: "GET /health", + tests: []testModel{{text: "Expecting status code: 200", passed: &passed}}, + result: &api.CLIStepResult{HTTPRequestResult: &api.HTTPRequestResult{ + StatusCode: 200, + Request: api.CLIStepHTTPRequest{Tests: []api.HTTPRequestTest{{StatusCode: &expectedStatus}}}, + }}, + } + runOutput := renderDetailedStep(step, 1, false, nil) + submitOutput := renderDetailedStep(step, 1, true, nil) + if strings.Contains(runOutput, "Expected:") || strings.Contains(runOutput, "Expecting") || strings.Contains(runOutput, "PASS") { + t.Fatalf("run should not display checks: %s", runOutput) + } + if !strings.Contains(runOutput, "Response status: 200") { + t.Fatalf("run is missing response status: %s", runOutput) + } + if !strings.Contains(submitOutput, "1. HTTP status\n Expected: 200\n Actual: 200") { + t.Fatalf("submit is missing expected/actual values: %s", submitOutput) + } +} + +func TestRunRetainsCommandOutputAndExitCode(t *testing.T) { + expectedExit := 0 + step := stepModel{ + description: "Run", detailLabel: "Command", detail: "./app", + tests: []testModel{{text: "Expect exit code 0"}}, + result: &api.CLIStepResult{CLICommandResult: &api.CLICommandResult{ + ExitCode: 1, Stdout: "hello", Stderr: "problem", + Command: api.CLIStepCLICommand{Tests: []api.CLICommandTest{{ExitCode: &expectedExit}}}, + }}, + } + view := renderDetailedStep(step, 1, false, nil) + for _, expected := range []string{"Command: ./app", "Command exit code: 1", "hello", "problem"} { + if !strings.Contains(view, expected) { + t.Fatalf("run missing %q: %s", expected, view) + } + } + if strings.Contains(view, "Expect") { + t.Fatalf("run includes checks: %s", view) + } +} + +func TestGradingErrorsUseVisualLimitsAtCheckAndStepLevel(t *testing.T) { + failed := false + for _, message := range []string{strings.Repeat("界", 20000), strings.Repeat("diagnostic\n", 100)} { + for _, testIndex := range []int{0, -1, 2} { + step := stepModel{ + description: "Example", passed: &failed, finished: true, + tests: []testModel{{text: "Requirement", passed: &failed, finished: true}}, + } + failure := &api.StructuredErrCLI{FailedStepIndex: 0, FailedTestIndex: testIndex, ErrorMessage: message} + got := renderDetailedStep(step, 1, true, failure) + if strings.Count(got, "Error:") != 1 || strings.Count(got, "... output visually truncated") != 1 { + t.Fatalf("testIndex=%d: expected one truncated error", testIndex) + } + if strings.Contains(got, message) || strings.Contains(got, strings.Repeat("界", 5121)) || strings.Count(got, "diagnostic") > 32 { + t.Fatalf("testIndex=%d: error exceeds visual limits", testIndex) + } + if otherStep := renderDetailedStep(step, 2, true, failure); strings.Contains(otherStep, "Error:") { + t.Fatal("grading error displayed on the wrong step") + } + if run := renderDetailedStep(step, 1, false, failure); strings.Contains(run, "Error:") { + t.Fatal("run exposed grading error") + } + } + } +} diff --git a/render/variables.go b/render/variables.go index 034651e..e1d3e00 100644 --- a/render/variables.go +++ b/render/variables.go @@ -7,6 +7,7 @@ import ( "github.com/bootdotdev/bootdev/checks" api "github.com/bootdotdev/bootdev/client" + "github.com/charmbracelet/lipgloss" "github.com/goccy/go-json" ) @@ -17,7 +18,7 @@ type variableEntry struct { description string } -func renderVariableSection(title string, entries []variableEntry) string { +func renderVariableSection(title string, entries []variableEntry, headingStyle lipgloss.Style) string { sort.Slice(entries, func(i, j int) bool { if entries[i].name == entries[j].name { return entries[i].description < entries[j].description @@ -26,9 +27,10 @@ func renderVariableSection(title string, entries []variableEntry) string { }) var str strings.Builder - fmt.Fprintf(&str, " %s: \n", title) + headingText := headingStyle.Render(title + ":") + fmt.Fprintf(&str, "\n%s\n", headingText) for _, entry := range entries { - fmt.Fprintf(&str, " - %s: %s (%s)\n", entry.name, formatVariableValue(entry.value, entry.found), entry.description) + fmt.Fprintf(&str, " - %s: %s (%s)\n", entry.name, formatVariableValue(entry.value, entry.found), entry.description) } return str.String() } diff --git a/render/variables_test.go b/render/variables_test.go index 38785ac..c1544e7 100644 --- a/render/variables_test.go +++ b/render/variables_test.go @@ -98,7 +98,7 @@ func TestCLIAvailableVariables(t *testing.T) { if !expectsVariables { t.Fatalf("expected CLI command to use variables") } - got := renderVariableSection("Variables Available", available) + got := renderVariableSection("Variables Available", available, subheaderStyle) if !strings.Contains(got, "url: http://localhost:42069 (Command)") { t.Fatalf("expected url entry in:\n%s", got) diff --git a/render/view.go b/render/view.go index faa96ab..a5a325d 100644 --- a/render/view.go +++ b/render/view.go @@ -7,87 +7,36 @@ import ( "unicode/utf8" api "github.com/bootdotdev/bootdev/client" - "github.com/charmbracelet/bubbles/spinner" - "github.com/charmbracelet/lipgloss" ) const safeStepIcon = "🛡︎" -func renderTestHeader(header string, spinner spinner.Model, isFinished bool, isSubmit bool, passed *bool, noPenaltyOnFail bool) string { - if noPenaltyOnFail { - header = fmt.Sprintf("%s %s", header, white.Render(safeStepIcon)) - } - cmdStr := renderTest(header, spinner.View(), isFinished, isSubmit, passed) - box := borderBox.Render(fmt.Sprintf(" %s ", cmdStr)) - sliced := strings.Split(box, "\n") - sliced[2] = strings.Replace(sliced[2], "─", "┬", 1) - return strings.Join(sliced, "\n") + "\n" -} - -func renderTests(tests []testModel, spinner string) string { - var str strings.Builder - var edges strings.Builder - - for _, test := range tests { - testStr := renderTest(test.text, spinner, test.finished, true, test.passed) - testStr = fmt.Sprintf(" %s", testStr) - height := lipgloss.Height(testStr) - - edges.Reset() - edges.WriteString(" ├─") - for i := 1; i < height; i++ { - edges.WriteString("\n │ ") - } - - str.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, edges.String(), testStr)) - str.WriteByte('\n') - } - - return str.String() -} - -func renderTest(text string, spinner string, isFinished bool, showStatus bool, passed *bool) string { - testStr := "" - if !isFinished { - testStr += fmt.Sprintf("%s %s", spinner, text) - } else if !showStatus { - testStr += text - } else if passed == nil { - testStr += gray.Render(fmt.Sprintf("? %s", text)) - } else if *passed { - testStr += green.Render(fmt.Sprintf("✓ %s", text)) - } else { - testStr += red.Render(fmt.Sprintf("X %s", text)) - } - return testStr -} - func renderJqOutputs(outputs []api.CLICommandJqOutput) string { if len(outputs) == 0 { return "" } var str strings.Builder - str.WriteString("\n > jq output:\n\n") + str.WriteString("\n " + subheaderStyle.Render("> jq output:") + "\n\n") for _, output := range outputs { - str.WriteString(gray.Render(fmt.Sprintf("Query: %s", output.Query))) + str.WriteString(secondaryStyle.Render(fmt.Sprintf("Query: %s", output.Query))) str.WriteByte('\n') if output.Error != "" { - str.WriteString(gray.Render(fmt.Sprintf("Error: %s", output.Error))) + str.WriteString(warningStyle.Render("Error:") + " " + output.Error) str.WriteByte('\n') str.WriteByte('\n') continue } if len(output.Results) == 0 { - str.WriteString(gray.Render("Results: [none]")) + str.WriteString(secondaryStyle.Render("Results: [none]")) str.WriteByte('\n') str.WriteByte('\n') continue } - str.WriteString(gray.Render("Results:")) + str.WriteString(secondaryStyle.Render("Results:")) str.WriteByte('\n') for _, line := range output.Results { - str.WriteString(gray.Render(" - " + line)) + str.WriteString(secondaryStyle.Render(" - " + line)) str.WriteByte('\n') } str.WriteByte('\n') @@ -101,8 +50,9 @@ func (m rootModel) View() string { } s := m.spinner.View() var str strings.Builder + str.WriteString(renderRunHeader(m.runContext)) failedStepIndex := -1 - if m.failure != nil && m.failure.FailedStepIndex >= 0 && m.failure.FailedStepIndex < len(m.steps) { + if m.isSubmit && m.failure != nil && m.failure.FailedStepIndex >= 0 && m.failure.FailedStepIndex < len(m.steps) { failedStepIndex = m.failure.FailedStepIndex } for i, step := range m.steps { @@ -112,39 +62,27 @@ func (m rootModel) View() string { showAllDetails := m.finalized && (m.verbose || !m.isSubmit) failed := step.passed != nil && !*step.passed - if showAllDetails { - str.WriteString(renderTestHeader(step.description, m.spinner, step.finished, m.isSubmit, step.passed, step.noPenaltyOnFail)) - fmt.Fprintf(&str, " > %s\n", step.detail) - str.WriteString(renderTests(step.tests, s)) + if showAllDetails || (failed && m.finalized) { + dividerWidth := m.terminalWidth + if dividerWidth <= 0 { + dividerWidth = 80 + } + str.WriteString(secondaryStyle.Render(strings.Repeat("─", dividerWidth)) + "\n") + str.WriteString(renderDetailedStep(step, i+1, m.isSubmit, m.failure)) } else { + step.description = fmt.Sprintf("Step %d · %s", i+1, step.description) str.WriteString(renderCompactStep(step, s, m.isSubmit)) - if failed && m.finalized { - fmt.Fprintf(&str, "\n > %s\n", step.detail) - str.WriteString(renderTests(step.tests, s)) - } - } - - if step.sleepAfter != "" && step.finished && showAllDetails { - sleepBox := borderBox.Render(fmt.Sprintf(" %s ", step.sleepAfter)) - str.WriteString(sleepBox) - str.WriteByte('\n') } - - if step.result == nil || !m.finalized || (!showAllDetails && !failed) { - continue - } - - str.WriteString(renderStepResult(step)) } if m.result == api.VerificationResultSlugSuccess && m.isSubmit { str.WriteByte('\n') str.WriteByte('\n') - str.WriteString(green.Render("All tests passed! 🎉")) + str.WriteString(passStyle.Render("All tests passed! 🎉")) str.WriteByte('\n') if m.xpReward >= 0 { str.WriteByte('\n') - str.WriteString(green.Bold(true).Render(fmt.Sprintf("Gained +%d XP", m.xpReward))) + str.WriteString(passStyle.Bold(true).Render(fmt.Sprintf("Gained +%d XP", m.xpReward))) str.WriteByte('\n') for _, item := range m.xpBreakdown { if item.XP == 0 { @@ -157,104 +95,111 @@ func (m rootModel) View() string { xp = -xp } if item.Percent > 0 { - str.WriteString(gray.Render(fmt.Sprintf("%s%3d XP (%-4s %s)", sign, xp, fmt.Sprintf("%.0f%%", item.Percent*100), item.Name))) + str.WriteString(secondaryStyle.Render(fmt.Sprintf("%s%3d XP (%-4s %s)", sign, xp, fmt.Sprintf("%.0f%%", item.Percent*100), item.Name))) } else { - str.WriteString(gray.Render(fmt.Sprintf("%s%3d XP (%s)", sign, xp, item.Name))) + str.WriteString(secondaryStyle.Render(fmt.Sprintf("%s%3d XP (%s)", sign, xp, item.Name))) } str.WriteByte('\n') } } str.WriteByte('\n') - str.WriteString(green.Render("Return to your browser to continue with the next lesson.")) + str.WriteString(passStyle.Render("Return to your browser to continue with the next lesson.")) str.WriteByte('\n') str.WriteByte('\n') if m.showOmitLessonIDTip { - str.WriteString(yellow.Render("Tip:")) + str.WriteString(informationStyle.Render("Tip:")) str.WriteString(" When you reach your next CLI lesson, you can skip copying its ID:\n ") - str.WriteString(yellow.Render("bootdev run")) + str.WriteString(informationStyle.Render("bootdev run")) str.WriteByte('\n') str.WriteByte('\n') } } else if m.result == api.VerificationResultSlugSystemError { str.WriteByte('\n') str.WriteByte('\n') - str.WriteString(red.Render("Unable to verify this lesson due to a system error.")) + str.WriteString(failStyle.Render("Unable to verify this lesson due to a system error.")) if m.failure != nil && m.failure.ErrorMessage != "" { - str.WriteString(red.Render(fmt.Sprintf("\nError: %s", m.failure.ErrorMessage))) + str.WriteString(failStyle.Render(fmt.Sprintf("\nError: %s", m.failure.ErrorMessage))) } str.WriteByte('\n') - str.WriteString(red.Render("Please try again.")) + str.WriteString(failStyle.Render("Please try again.")) str.WriteByte('\n') str.WriteByte('\n') - } else if m.result == api.VerificationResultSlugNoop { - str.WriteString("\n\nTests failed! ❌") - fmt.Fprintf(&str, "\n\nFailed Step: %v", m.failure.FailedStepIndex+1) - str.WriteString("\nError: ") - str.WriteString(m.failure.ErrorMessage) + } else if m.isSubmit && m.result == api.VerificationResultSlugNoop { str.WriteByte('\n') str.WriteByte('\n') - str.WriteString(white.Render(safeStepIcon)) - str.WriteString(" This was a safe step.\n") + str.WriteString(informationStyle.Render(safeStepIcon) + " " + informationStyle.Render("This was a safe step.") + "\n") str.WriteString("You haven't passed, but you also haven't lost armor or Sharpshooter progress.\n\n") - } else if m.result == api.VerificationResultSlugFailure { - str.WriteByte('\n') - str.WriteByte('\n') - str.WriteString(red.Render("Tests failed! ❌")) - if m.failure != nil { - str.WriteString(red.Render(fmt.Sprintf("\n\nFailed Step: %v", m.failure.FailedStepIndex+1))) - str.WriteString(red.Render(fmt.Sprintf("\nError: %s", m.failure.ErrorMessage))) - } else { - str.WriteString(red.Render("\n\nFailed Step: unknown")) - str.WriteString(red.Render("\nError: unknown")) - } - str.WriteByte('\n') - str.WriteByte('\n') - currentDate := time.Now().Format("2006-01-02") - if strings.HasSuffix(currentDate, "04-01") { - str.WriteString(magenta.Render(fmt.Sprintf("This incident has been reported to your system administrator. [%s]\n", currentDate))) - } + } + currentDate := time.Now().Format("2006-01-02") + if m.isSubmit && m.result == api.VerificationResultSlugFailure && strings.HasSuffix(currentDate, "04-01") { + str.WriteString(accentStyle.Render(fmt.Sprintf("This incident has been reported to your system administrator. [%s]\n", currentDate))) } return str.String() } func renderCompactStep(step stepModel, spinner string, isSubmit bool) string { - line := renderTest(step.description, spinner, step.finished, isSubmit, step.passed) + line := headerStyle.Bold(true).Render(step.description) + if !step.finished { + line = spinner + " " + line + } else if isSubmit { + status := "?" + if step.passed != nil { + status = "PASS" + if !*step.passed { + status = "FAIL" + } + } + line = renderStatus(status) + " " + line + } if step.noPenaltyOnFail { - line = fmt.Sprintf("%s %s", line, white.Render(safeStepIcon)) + line = fmt.Sprintf("%s %s", line, informationStyle.Render(safeStepIcon)) } return line + "\n" } -func renderStepResult(step stepModel) string { +func renderStepResult(step stepModel, isSubmit bool) string { if failure := step.result.DependencyFailure; failure != nil { - return " > Skipped: unavailable variables: " + strings.Join(failure.Names, ", ") + "\n" + return "\n " + warningStyle.Render("Skipped:") + " unavailable variables: " + strings.Join(failure.Names, ", ") + "\n" } var str strings.Builder if step.result.CLICommandResult != nil { - for _, test := range step.result.CLICommandResult.Command.Tests { - if test.ExitCode != nil { - fmt.Fprintf(&str, "\n > Command exit code: %d\n", step.result.CLICommandResult.ExitCode) - break + if isSubmit && step.result.CLICommandResult.Err != "" { + fmt.Fprintf(&str, "\n %s %s\n", warningStyle.Render("Error:"), step.result.CLICommandResult.Err) + } + exitCodeShown := false + for index, test := range step.result.CLICommandResult.Command.Tests { + if isSubmit && isExitCodeOnlyCheck(test) && index < len(step.tests) && step.tests[index].passed != nil { + exitCodeShown = true } } - str.WriteString(" > Command stdout:\n\n") - str.WriteString(gray.Render(truncateVisualOutput(step.result.CLICommandResult.Stdout))) - str.WriteByte('\n') + if !exitCodeShown { + fmt.Fprintf(&str, "\n %s %d\n", subheaderStyle.Render("Command exit code:"), step.result.CLICommandResult.ExitCode) + } + str.WriteString("\n " + subheaderStyle.Render("Standard output") + "\n") + if step.result.CLICommandResult.Stdout == "" { + str.WriteString(" " + secondaryStyle.Render("") + "\n") + } else { + str.WriteString(indentOutput(truncateVisualOutput(step.result.CLICommandResult.Stdout), 4)) + str.WriteByte('\n') + } if step.result.CLICommandResult.Stderr != "" { - str.WriteString(" > Command stderr:\n\n") - str.WriteString(gray.Render(truncateVisualOutput(step.result.CLICommandResult.Stderr))) + str.WriteString("\n " + subheaderStyle.Render("Standard error") + "\n") + str.WriteString(indentOutput(truncateVisualOutput(step.result.CLICommandResult.Stderr), 4)) str.WriteByte('\n') } str.WriteString(renderJqOutputs(step.result.CLICommandResult.JqOutputs)) availableVariables, expectsVariables := availableVariablesForCLIResult(*step.result.CLICommandResult) if expectsVariables { - str.WriteString(renderVariableSection("Variables Available", availableVariables)) + str.WriteString(indentOutput(renderVariableSection("Variables Available", availableVariables, subheaderStyle), 2)) + str.WriteByte('\n') } } if step.result.HTTPRequestResult != nil { - str.WriteString(printHTTPRequestResult(*step.result.HTTPRequestResult)) + str.WriteByte('\n') + str.WriteString(indentOutput(printHTTPRequestResult(*step.result.HTTPRequestResult), 2)) + str.WriteByte('\n') } return str.String() } @@ -294,7 +239,7 @@ func truncateVisualOutput(output string) string { if str.Len() > 0 && !endsWithNewline { str.WriteByte('\n') } - str.WriteString("... output visually truncated") + str.WriteString(informationStyle.Render("... output visually truncated")) } return str.String() } diff --git a/render/view_test.go b/render/view_test.go index 69a2627..55410f7 100644 --- a/render/view_test.go +++ b/render/view_test.go @@ -6,6 +6,7 @@ import ( "testing" "unicode/utf8" + "github.com/bootdotdev/bootdev/checks" api "github.com/bootdotdev/bootdev/client" ) @@ -19,7 +20,8 @@ func TestCompactViewHidesSuccessfulDetailsAndExpandsFailure(t *testing.T) { m.steps = []stepModel{ { description: "The health check succeeds", - detail: "Request: GET http://localhost:3000/health", + detailLabel: "Request", + detail: "GET http://localhost:3000/health", passed: &passed, finished: true, tests: []testModel{{text: "Expecting status code: 200", passed: &passed, finished: true}}, @@ -30,7 +32,8 @@ func TestCompactViewHidesSuccessfulDetailsAndExpandsFailure(t *testing.T) { }, { description: "Email changes require a password", - detail: "Request: POST http://localhost:3000/account/email", + detailLabel: "Request", + detail: "POST http://localhost:3000/account/email", passed: &failed, finished: true, tests: []testModel{{text: "Expecting status code: 403", passed: &failed, finished: true}}, @@ -41,15 +44,16 @@ func TestCompactViewHidesSuccessfulDetailsAndExpandsFailure(t *testing.T) { }, { description: "A later check", - detail: "Request: GET http://localhost:3000/later", + detailLabel: "Request", + detail: "GET http://localhost:3000/later", finished: true, }, } view := m.View() for _, expected := range []string{ - "✓ The health check succeeds", - "X Email changes require a password", + "PASS Step 1 · The health check succeeds", + "STEP 2 · Email changes require a password", "Request: POST http://localhost:3000/account/email", "Expecting status code: 403", "failed response body", @@ -69,6 +73,72 @@ func TestCompactViewHidesSuccessfulDetailsAndExpandsFailure(t *testing.T) { } } +func TestNumberedStepsAcrossOutputModes(t *testing.T) { + for _, tt := range []struct { + name string + submit bool + verbose bool + finalized bool + }{ + {name: "running"}, + {name: "run", finalized: true}, + {name: "submit", submit: true, finalized: true}, + {name: "verbose submit", submit: true, verbose: true, finalized: true}, + } { + t.Run(tt.name, func(t *testing.T) { + m := initModel(tt.submit, tt.verbose) + m.finalized = tt.finalized + m.steps = []stepModel{{description: "Build", finished: true}, {description: "Run", finished: true}} + for range 2 { + view := m.View() + for _, expected := range []string{"1 · Build", "2 · Run"} { + if strings.Count(view, expected) != 1 { + t.Fatalf("expected one %q in view:\n%s", expected, view) + } + } + } + }) + } +} + +func TestFailedViewOmitsSummaryAndPreservesSafeStepMessage(t *testing.T) { + for _, tt := range []struct { + name string + failure *api.StructuredErrCLI + detail string + detailLabel string + safe bool + }{ + {name: "command", failure: &api.StructuredErrCLI{FailedStepIndex: 1, ErrorMessage: "wrong exit code"}, detailLabel: "Command", detail: "./app"}, + {name: "request", failure: &api.StructuredErrCLI{FailedStepIndex: 1, ErrorMessage: "wrong status"}, detailLabel: "Request", detail: "GET http://localhost/health"}, + {name: "safe step", failure: &api.StructuredErrCLI{FailedStepIndex: 1, ErrorMessage: "missing configuration"}, detailLabel: "Command", detail: "./app", safe: true}, + {name: "missing failure"}, + {name: "missing safe failure", safe: true}, + } { + t.Run(tt.name, func(t *testing.T) { + m := initModel(true, false) + m.finalized = true + m.result = api.VerificationResultSlugFailure + if tt.safe { + m.result = api.VerificationResultSlugNoop + } + m.failure = tt.failure + failed := false + m.steps = []stepModel{{description: "Build", finished: true}, {description: "Run", detailLabel: tt.detailLabel, detail: tt.detail, finished: true, passed: &failed}} + view := m.View() + if strings.Contains(view, "FAILED · ") { + t.Fatalf("unexpected failure recap:\n%s", view) + } + if strings.Contains(view, "This was a safe step.") != tt.safe { + t.Fatalf("incorrect safe-step message:\n%s", view) + } + if tt.detail != "" && strings.Count(view, tt.detail) != 1 { + t.Fatalf("expected failed-step detail exactly once: %s", view) + } + }) + } +} + func TestCompactViewDoesNotHideStepsForInvalidFailureIndex(t *testing.T) { for _, failedStepIndex := range []int{-1, 2} { t.Run(fmt.Sprintf("index_%d", failedStepIndex), func(t *testing.T) { @@ -102,14 +172,14 @@ func TestSystemErrorViewDoesNotShowStepsAsPassed(t *testing.T) { view := m.View() for _, expected := range []string{ - "? The command prints a greeting", + "? Step 1 · The command prints a greeting", "system error", } { if !strings.Contains(view, expected) { t.Errorf("view missing %q\n%s", expected, view) } } - for _, unexpected := range []string{"✓ The command prints a greeting", "All tests passed!"} { + for _, unexpected := range []string{"PASS Step 1 · The command prints a greeting", "All tests passed!"} { if strings.Contains(view, unexpected) { t.Errorf("view unexpectedly contains %q\n%s", unexpected, view) } @@ -152,3 +222,194 @@ func TestHTTPRequestBodyUsesVisualOutputLimit(t *testing.T) { t.Fatalf("expected HTTP response body to be visually truncated") } } + +func TestGradingExplanationStaysWithFailedCheck(t *testing.T) { + expectedExit, expectedLines, expectedStatus := 0, 50, 200 + expectedString := "ready" + for _, tt := range []struct { + name string + step api.CLIStep + result api.CLIStepResult + wantError bool + }{ + { + name: "truncated line count", + step: api.CLIStep{CLICommand: &api.CLIStepCLICommand{Tests: []api.CLICommandTest{{StdoutLinesGT: &expectedLines}}}}, + result: api.CLIStepResult{CLICommandResult: &api.CLICommandResult{Stdout: strings.TrimSuffix(strings.Repeat("line\n", 40), "\n")}}, + wantError: true, + }, + { + name: "compound command check", + step: api.CLIStep{CLICommand: &api.CLIStepCLICommand{Tests: []api.CLICommandTest{{ExitCode: &expectedExit, StdoutContainsAll: []string{"ready"}}}}}, + result: api.CLIStepResult{CLICommandResult: &api.CLICommandResult{Stdout: "pending"}}, + wantError: true, + }, + { + name: "simple exit code", + step: api.CLIStep{CLICommand: &api.CLIStepCLICommand{Tests: []api.CLICommandTest{{ExitCode: &expectedExit}}}}, + result: api.CLIStepResult{CLICommandResult: &api.CLICommandResult{ExitCode: 2}}, + }, + { + name: "simple HTTP status", + step: api.CLIStep{HTTPRequest: &api.CLIStepHTTPRequest{Tests: []api.HTTPRequestTest{{StatusCode: &expectedStatus}}}}, + result: api.CLIStepResult{HTTPRequestResult: &api.HTTPRequestResult{StatusCode: 500}}, + }, + { + name: "JSON wrong type", + step: api.CLIStep{HTTPRequest: &api.CLIStepHTTPRequest{Tests: []api.HTTPRequestTest{{JSONValue: &api.HTTPRequestTestJSONValue{ + Path: ".value", Operator: api.OpEquals, StringValue: &expectedString, + }}}}}, + result: api.CLIStepResult{HTTPRequestResult: &api.HTTPRequestResult{BodyString: `{"value":123}`}}, + wantError: true, + }, + } { + t.Run(tt.name, func(t *testing.T) { + if tt.result.CLICommandResult != nil { + tt.result.CLICommandResult.Command = *tt.step.CLICommand + } else { + tt.result.HTTPRequestResult.Request = *tt.step.HTTPRequest + } + failure := checks.EvaluateCLIResults(api.CLIData{Steps: []api.CLIStep{tt.step}}, []api.CLIStepResult{tt.result}) + if failure == nil || failure.FailedTestIndex != 0 { + t.Fatalf("expected check failure, got %+v", failure) + } + for _, submit := range []bool{false, true} { + for _, verbose := range []bool{false, true} { + failed := false + m := initModel(submit, verbose) + m.finalized = true + m.result = api.VerificationResultSlugFailure + m.failure = failure + m.steps = []stepModel{{description: "Example", passed: &failed, finished: true, result: &tt.result, + tests: []testModel{{text: "Requirement", passed: &failed, finished: true}}}} + got := m.View() + wantCount := 0 + if submit && tt.wantError { + wantCount = 1 + } + if strings.Count(got, "Error: "+failure.ErrorMessage) != wantCount || strings.Contains(got, "FAILED ·") { + t.Fatalf("submit=%v verbose=%v: wrong grading diagnostic:\n%s", submit, verbose, got) + } + if wantCount == 1 && !strings.Contains(got, "\n Error: "+failure.ErrorMessage) { + t.Fatalf("grading error should be nested beneath the check:\n%s", got) + } + if tt.name == "truncated line count" && submit && + (!strings.Contains(got, "got 40") || !strings.Contains(got, "... output visually truncated")) { + t.Fatalf("lost count with truncated output:\n%s", got) + } + } + } + }) + } +} + +func TestCommandCaptureErrorAppearsOnlyInSubmittedStep(t *testing.T) { + failed := false + expectedExit := 0 + const captureError = "missing value for variable 'token'" + for _, tt := range []struct { + name string + submit bool + verbose bool + }{ + {name: "run"}, + {name: "expanded run", verbose: true}, + {name: "submit", submit: true}, + {name: "expanded submit", submit: true, verbose: true}, + } { + t.Run(tt.name, func(t *testing.T) { + m := initModel(tt.submit, tt.verbose) + m.finalized = true + if tt.submit { + m.result = api.VerificationResultSlugFailure + m.failure = &api.StructuredErrCLI{FailedStepIndex: 0, FailedTestIndex: -1, ErrorMessage: captureError} + } + m.steps = []stepModel{{ + description: "Capture token", detailLabel: "Command", detail: "printf hello", finished: true, passed: &failed, + tests: []testModel{{text: "Expect exit code 0", finished: true}}, + result: &api.CLIStepResult{CLICommandResult: &api.CLICommandResult{ + Err: captureError, ExitCode: 0, Stdout: "hello", + Command: api.CLIStepCLICommand{Tests: []api.CLICommandTest{{ExitCode: &expectedExit}}}, + }}, + }} + got := m.View() + expectedCount := 0 + if tt.submit { + expectedCount = 1 + } + if strings.Count(got, "Error: "+captureError) != expectedCount { + t.Fatalf("unexpected capture error visibility:\n%s", got) + } + if tt.submit && strings.Index(got, captureError) > strings.Index(got, "Standard output") { + t.Fatalf("error should appear inside the step before its output:\n%s", got) + } + if !strings.Contains(got, "Command exit code: 0") || !strings.Contains(got, "hello") || strings.Contains(got, "FAILED ·") { + t.Fatalf("lost command diagnostics or restored footer:\n%s", got) + } + }) + } +} + +func TestCaptureValidationFailureAppearsOnSubmittedStep(t *testing.T) { + expectedExit, expectedStatus := 0, 200 + for _, tt := range []struct { + name string + step api.CLIStep + result api.CLIStepResult + }{ + { + name: "command", + step: api.CLIStep{CLICommand: &api.CLIStepCLICommand{ + Tests: []api.CLICommandTest{{ExitCode: &expectedExit}}, + StdoutVariables: []api.CLICommandStdoutVariable{{Name: "token", Regex: "token=(.*)"}}, + }}, + result: api.CLIStepResult{CLICommandResult: &api.CLICommandResult{ + Stdout: "token=expected", Variables: map[string]string{"token": "different"}, + }}, + }, + { + name: "HTTP", + step: api.CLIStep{HTTPRequest: &api.CLIStepHTTPRequest{ + Tests: []api.HTTPRequestTest{{StatusCode: &expectedStatus}}, + ResponseVariables: []api.HTTPRequestResponseVariable{{Name: "token", Path: ".token"}}, + }}, + result: api.CLIStepResult{HTTPRequestResult: &api.HTTPRequestResult{ + StatusCode: 200, BodyString: `{"token":"expected"}`, Variables: map[string]string{"token": "different"}, + }}, + }, + } { + t.Run(tt.name, func(t *testing.T) { + if tt.result.CLICommandResult != nil { + tt.result.CLICommandResult.Command = *tt.step.CLICommand + } else { + tt.result.HTTPRequestResult.Request = *tt.step.HTTPRequest + } + failure := checks.EvaluateCLIResults(api.CLIData{Steps: []api.CLIStep{tt.step}}, []api.CLIStepResult{tt.result}) + if failure == nil || failure.FailedTestIndex <= 0 { + t.Fatalf("expected capture validation failure outside check list, got %+v", failure) + } + for _, submit := range []bool{true, false} { + for _, verbose := range []bool{true, false} { + passed, failed := true, false + m := initModel(submit, verbose) + m.finalized = true + m.result = api.VerificationResultSlugFailure + m.failure = failure + m.steps = []stepModel{{description: "Capture", finished: true, passed: &failed, result: &tt.result, + tests: []testModel{{text: "Requirement", passed: &passed, finished: true}}}} + got := m.View() + wantCount := 0 + if submit { + wantCount = 1 + } + if strings.Count(got, "Error: "+failure.ErrorMessage) != wantCount { + t.Fatalf("submit=%v verbose=%v: unexpected capture diagnostic:\n%s", submit, verbose, got) + } + if submit && !strings.Contains(got, "\n Error: "+failure.ErrorMessage) { + t.Fatalf("capture diagnostic is not at step level:\n%s", got) + } + } + } + }) + } +} From eda2349b12ec8db2a9773936f848fac1cf0e32f0 Mon Sep 17 00:00:00 2001 From: skovranek <59619403+skovranek@users.noreply.github.com> Date: Tue, 10 Nov 2026 11:43:49 -0500 Subject: [PATCH 2/2] increment minor version --- version.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.txt b/version.txt index 7a48901..cde949b 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v1.32.6 +v1.33.0