|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func writeStaticProject(t *testing.T) string { |
| 13 | + t.Helper() |
| 14 | + root := t.TempDir() |
| 15 | + if err := os.MkdirAll(filepath.Join(root, "site"), 0o755); err != nil { |
| 16 | + t.Fatal(err) |
| 17 | + } |
| 18 | + if err := os.WriteFile(filepath.Join(root, "site", "index.html"), []byte("<h1>hi</h1>"), 0o644); err != nil { |
| 19 | + t.Fatal(err) |
| 20 | + } |
| 21 | + composeFile := filepath.Join(root, "compose.yaml") |
| 22 | + if err := os.WriteFile(composeFile, []byte(` |
| 23 | +services: |
| 24 | + web: |
| 25 | + image: nginx:alpine |
| 26 | + volumes: |
| 27 | + - ./site:/usr/share/nginx/html:ro |
| 28 | + debugtool: |
| 29 | + image: redis:7 |
| 30 | + profiles: |
| 31 | + - debug |
| 32 | +`), 0o644); err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + return composeFile |
| 36 | +} |
| 37 | + |
| 38 | +func TestRunAnalyzeHumanOutputSurfacesProfileWarning(t *testing.T) { |
| 39 | + composeFile := writeStaticProject(t) |
| 40 | + var stdout, stderr bytes.Buffer |
| 41 | + code := Run([]string{"analyze", "-f", composeFile}, &stdout, &stderr) |
| 42 | + if code != 0 { |
| 43 | + t.Fatalf("exit code = %d, stderr = %q", code, stderr.String()) |
| 44 | + } |
| 45 | + out := stdout.String() |
| 46 | + for _, want := range []string{"Browser readiness: 100%", "web: static-web adapter", "Warnings:", "profile-gated"} { |
| 47 | + if !strings.Contains(out, want) { |
| 48 | + t.Fatalf("analyze output missing %q:\n%s", want, out) |
| 49 | + } |
| 50 | + } |
| 51 | + // The service is named in the skip warning, but must not appear as an |
| 52 | + // analyzed service line (" debugtool: ..."). |
| 53 | + if strings.Contains(out, " debugtool:") { |
| 54 | + t.Fatalf("profile-gated service should not be listed as analyzed:\n%s", out) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestRunAnalyzeJSONIsValid(t *testing.T) { |
| 59 | + composeFile := writeStaticProject(t) |
| 60 | + var stdout, stderr bytes.Buffer |
| 61 | + code := Run([]string{"analyze", "-f", composeFile, "--json"}, &stdout, &stderr) |
| 62 | + if code != 0 { |
| 63 | + t.Fatalf("exit code = %d, stderr = %q", code, stderr.String()) |
| 64 | + } |
| 65 | + var payload struct { |
| 66 | + Mode string `json:"mode"` |
| 67 | + Readiness struct { |
| 68 | + Score int `json:"score"` |
| 69 | + } `json:"readiness"` |
| 70 | + Warnings []string `json:"warnings"` |
| 71 | + } |
| 72 | + if err := json.Unmarshal(stdout.Bytes(), &payload); err != nil { |
| 73 | + t.Fatalf("analyze --json did not produce valid JSON: %v\n%s", err, stdout.String()) |
| 74 | + } |
| 75 | + if payload.Mode != "browser-native" || payload.Readiness.Score != 100 { |
| 76 | + t.Fatalf("unexpected analysis payload: %+v", payload) |
| 77 | + } |
| 78 | + if len(payload.Warnings) == 0 { |
| 79 | + t.Fatalf("expected a profile-skip warning in JSON output") |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestRunAnalyzeMissingFileFails(t *testing.T) { |
| 84 | + var stdout, stderr bytes.Buffer |
| 85 | + code := Run([]string{"analyze", "-f", filepath.Join(t.TempDir(), "nope.yaml")}, &stdout, &stderr) |
| 86 | + if code != 1 { |
| 87 | + t.Fatalf("exit code = %d, want 1", code) |
| 88 | + } |
| 89 | + if stderr.Len() == 0 { |
| 90 | + t.Fatalf("expected an error message on stderr") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestRunDemoGeneratesOutput(t *testing.T) { |
| 95 | + composeFile := writeStaticProject(t) |
| 96 | + outDir := filepath.Join(t.TempDir(), "demo") |
| 97 | + var stdout, stderr bytes.Buffer |
| 98 | + code := Run([]string{"demo", "-f", composeFile, "-o", outDir}, &stdout, &stderr) |
| 99 | + if code != 0 { |
| 100 | + t.Fatalf("exit code = %d, stderr = %q", code, stderr.String()) |
| 101 | + } |
| 102 | + if _, err := os.Stat(filepath.Join(outDir, "pocketstack.manifest.json")); err != nil { |
| 103 | + t.Fatalf("demo did not write a manifest: %v", err) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestRunVersionAndUnknownCommand(t *testing.T) { |
| 108 | + var stdout, stderr bytes.Buffer |
| 109 | + if code := Run([]string{"version"}, &stdout, &stderr); code != 0 { |
| 110 | + t.Fatalf("version exit code = %d", code) |
| 111 | + } |
| 112 | + if strings.TrimSpace(stdout.String()) == "" { |
| 113 | + t.Fatalf("version produced no output") |
| 114 | + } |
| 115 | + |
| 116 | + stdout.Reset() |
| 117 | + stderr.Reset() |
| 118 | + if code := Run([]string{"frobnicate"}, &stdout, &stderr); code != 2 { |
| 119 | + t.Fatalf("unknown command exit code = %d, want 2", code) |
| 120 | + } |
| 121 | +} |
0 commit comments