Files
coder/cli/ping_internal_test.go
Nick Vigilante d469763bc0 feat: add JSON output to coder ping (#30148)
`coder ping` printed per-pong lines and a summary table with no
structured output, so monitoring scripts had to parse text.

It now accepts `-o table|json` (default `table`, today's output) through
the shared CLI output formatter.
JSON is a single document at the end with each attempt (sequence, time,
latency, direct or DERP region, error) and the summary.

**Behavior change:** Additive.
Default output is unchanged.
`-o json` requires `--num` and fails early without it, because pings
otherwise continue until interrupted.
In JSON mode, the per-pong and diagnostic lines that normally go to
stdout go to stderr, so stdout contains only the JSON document.

<details>
<summary>Decisions</summary>

- One document at the end rather than streaming, because it's simpler to
parse.
- Require `--num` with `-o json`. In the default continuous mode, a
single document would leave stdout empty until Ctrl+C and hold every
pong in memory. Newline-delimited JSON per pong is a possible follow-up
if continuous machine-readable output is needed.
- Durations are reported as `*_ms` floats because `time.Duration`
doesn't marshal to a portable unit.
- Output goes through the shared `cliui.NewOutputFormatter` (table and
JSON), matching `coder speedtest`. This also adds the standard `-c,
--column` flag for the summary table.

</details>

<details>
<summary>CI note</summary>

`Storybook` failed once on 61b39ef70b inside Pixel (`build finished with
status failed`). This PR changes no frontend files; the job passed on
rerun.

`test-go-pg (ubuntu-latest)` failed on 8e6276921e in [run
38948569950](https://github.com/coder/coder/actions/runs/38948569950/job/117134059011)
on `TestChatTurnStopsAtStepLimit/Default` in `coderd/x/chatd`. This PR
doesn't touch `coderd/x/chatd`, so the failed job was re-run.

</details>

Closes [CLI-32](https://linear.app/codercom/issue/CLI-32)

_This PR was generated by Coder Agents on behalf of @nickvigilante._
2026-10-26 13:17:51 -04:00

106 lines
2.1 KiB
Go

package cli
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"tailscale.com/ipn/ipnstate"
)
func TestBuildSummary(t *testing.T) {
t.Parallel()
t.Run("Ok", func(t *testing.T) {
t.Parallel()
input := []*ipnstate.PingResult{
{
Err: "",
LatencySeconds: 0.1,
},
{
Err: "",
LatencySeconds: 0.3,
},
{
Err: "",
LatencySeconds: 0.2,
},
{
Err: "ping error",
LatencySeconds: 0.4,
},
}
actual := pingSummary{
Workspace: "test",
}
for _, r := range input {
actual.addResult(r)
}
actual.finalize()
require.Equal(t, time.Duration(0.1*float64(time.Second)), *actual.Min)
require.Equal(t, time.Duration(0.2*float64(time.Second)), *actual.Avg)
require.Equal(t, time.Duration(0.3*float64(time.Second)), *actual.Max)
require.Equal(t, time.Duration(0.009999999*float64(time.Second)), *actual.Variance)
require.Equal(t, actual.Successful, 3)
})
t.Run("One", func(t *testing.T) {
t.Parallel()
input := []*ipnstate.PingResult{
{
LatencySeconds: 0.2,
},
}
actual := &pingSummary{
Workspace: "test",
}
for _, r := range input {
actual.addResult(r)
}
actual.finalize()
require.Equal(t, actual.Successful, 1)
require.Equal(t, time.Duration(0.2*float64(time.Second)), *actual.Min)
require.Equal(t, time.Duration(0.2*float64(time.Second)), *actual.Avg)
require.Equal(t, time.Duration(0.2*float64(time.Second)), *actual.Max)
require.Nil(t, actual.Variance)
})
t.Run("NoLatency", func(t *testing.T) {
t.Parallel()
input := []*ipnstate.PingResult{
{
Err: "ping error",
},
{
Err: "ping error",
LatencySeconds: 0.2,
},
}
expected := &pingSummary{
Workspace: "test",
Total: 2,
Successful: 0,
Min: nil,
Avg: nil,
Max: nil,
Variance: nil,
latencySum: 0,
runningAvg: 0,
m2: 0,
}
actual := &pingSummary{
Workspace: "test",
}
for _, r := range input {
actual.addResult(r)
}
actual.finalize()
require.Equal(t, expected, actual)
})
}