Files
Loki/internal/loki/chat_shell_hygiene_test.go
T
MichaelandClaude Opus 5.5 2e0356cd6c Terminal : le vrai code de sortie, la sortie gardée au délai, sans ANSI
Repris de shell.ts / shell-tail.ts et diagnostics.ts d'OpenFox (2.0.160).

- « cmd | tail -N » : le code de sortie devenait celui de tail (0) et un
  build en échec passait pour réussi. Le tail est retiré de la commande et
  Loki garde lui-même les N dernières lignes : même sortie, vrai code.
- Délai dépassé : la sortie déjà produite est rendue (où en était le build,
  quel test bloquait) au lieu d'un « [timeout] » sec, avec le conseil de
  passer par bash_bg pour un serveur.
- Couleurs et séquences ANSI retirées : du bruit en tokens.
- La durée suit le code de sortie (« exit: 0 · 1.2s »).
- Mode Code : « cmd & » refusé, avec renvoi vers bash_bg — le process
  orphelin n'avait ni sortie lisible ni moyen d'être arrêté.
- Diagnostics LSP : erreurs d'abord, avec le compte total ; au-delà de la
  limite, des indices de style passaient devant l'erreur de compilation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 23:34:42 +02:00

59 lines
1.8 KiB
Go

package loki
import (
"context"
"runtime"
"strings"
"testing"
)
func TestSplitTailPipe(t *testing.T) {
cas := map[string]struct {
cmd string
n int
}{
"go build ./... 2>&1 | tail -20": {"go build ./... 2>&1", 20},
"make | tail -n 5": {"make", 5},
"make|tail -n40": {"make", 40},
"go test | tail": {"go test", 10},
"tail -f log.txt": {"tail -f log.txt", 0},
"cat a | tail -5 | grep x": {"cat a | tail -5 | grep x", 0},
"echo ok": {"echo ok", 0},
}
for in, want := range cas {
if cmd, n := splitTailPipe(in); cmd != want.cmd || n != want.n {
t.Errorf("%q → (%q, %d), attendu (%q, %d)", in, cmd, n, want.cmd, want.n)
}
}
if !trailingBackground("./serveur &") || trailingBackground("make && make test") || trailingBackground("ls 2>&1") {
t.Error("trailingBackground se trompe")
}
if got := stripANSI("\x1b[1;31mFAIL\x1b[0m ok"); got != "FAIL ok" {
t.Errorf("stripANSI = %q", got)
}
}
// « cmd | tail -N » rend le VRAI code de sortie, et seulement N lignes.
func TestShellTailGardeLeCodeDeSortie(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("shell POSIX")
}
withWorkspace(t)
out := runShell(context.Background(), "sh -c 'printf \"l1\nl2\nl3\n\"; exit 2' | tail -n 2", 10)
if !strings.HasPrefix(out, "exit: 2") || strings.Contains(out, "l1") || !strings.Contains(out, "l3") {
t.Fatalf("sortie = %q", out)
}
}
// Au délai dépassé, la sortie déjà produite est rendue.
func TestShellTimeoutGardeLaSortie(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("shell POSIX")
}
withWorkspace(t)
out := runShell(context.Background(), "echo avant; sleep 5", 1)
if !strings.Contains(out, "timeout") || !strings.Contains(out, "avant") {
t.Fatalf("sortie = %q", out)
}
}