419 lines
13 KiB
Go
419 lines
13 KiB
Go
package seriatim
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSubprocessRunnerSuccessWithReportArgsAndEnv(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("helper wrapper script uses /bin/sh")
|
|
}
|
|
|
|
t.Setenv("GO_WANT_SERIATIM_HELPER", "1")
|
|
t.Setenv("SERIATIM_HELPER_MODE", "success")
|
|
|
|
recordPath := filepath.Join(t.TempDir(), "record.json")
|
|
t.Setenv("SERIATIM_HELPER_RECORD_PATH", recordPath)
|
|
|
|
wrapper := writeHelperWrapper(t)
|
|
coalesce := 3.0
|
|
owg := 1.0
|
|
owrw := 1.0
|
|
bmd := 2.0
|
|
fmd := 1.25
|
|
runner, err := NewSubprocessRunner(SubprocessRunnerConfig{
|
|
Binary: wrapper,
|
|
Timeout: mustParseDuration(t, "2s"),
|
|
OutputSchema: "seriatim-intermediate",
|
|
CoalesceGap: &coalesce,
|
|
Report: true,
|
|
Env: EnvConfig{
|
|
OverlapWordRunGap: &owg,
|
|
OverlapWordRunReorderWindow: &owrw,
|
|
BackchannelMaxDuration: &bmd,
|
|
FillerMaxDuration: &fmd,
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewSubprocessRunner() error = %v", err)
|
|
}
|
|
|
|
dir := t.TempDir()
|
|
req := MergeRequest{
|
|
GeneratedConfigPath: filepath.Join(dir, "seriatim.generated.yml"),
|
|
InputTranscriptPaths: []string{filepath.Join(dir, "a.json"), filepath.Join(dir, "b.json")},
|
|
OutputMergedTranscriptPath: filepath.Join(dir, "merged.json"),
|
|
ReportPath: filepath.Join(dir, "seriatim.report.json"),
|
|
SpeakersPath: filepath.Join(dir, "speakers.yml"),
|
|
AutocorrectPath: filepath.Join(dir, "autocorrect.yml"),
|
|
StdoutLogPath: filepath.Join(dir, "seriatim.stdout.log"),
|
|
StderrLogPath: filepath.Join(dir, "seriatim.stderr.log"),
|
|
}
|
|
writeSeriatimFile(t, req.InputTranscriptPaths[0], `{"a":1}`)
|
|
writeSeriatimFile(t, req.InputTranscriptPaths[1], `{"b":1}`)
|
|
writeSeriatimFile(t, req.SpeakersPath, "speakers: []\n")
|
|
writeSeriatimFile(t, req.AutocorrectPath, "autocorrect: []\n")
|
|
|
|
res, err := runner.Run(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("Run() error = %v", err)
|
|
}
|
|
if res.MergedTranscriptPath != req.OutputMergedTranscriptPath {
|
|
t.Fatalf("MergedTranscriptPath = %q, want %q", res.MergedTranscriptPath, req.OutputMergedTranscriptPath)
|
|
}
|
|
if res.ReportPath != req.ReportPath {
|
|
t.Fatalf("ReportPath = %q, want %q", res.ReportPath, req.ReportPath)
|
|
}
|
|
if res.OutputSchema != "seriatim-intermediate" {
|
|
t.Fatalf("OutputSchema = %q, want seriatim-intermediate", res.OutputSchema)
|
|
}
|
|
if res.InvokedBinary != wrapper {
|
|
t.Fatalf("InvokedBinary = %q, want %q", res.InvokedBinary, wrapper)
|
|
}
|
|
if res.ExitCode != 0 {
|
|
t.Fatalf("ExitCode = %d, want 0", res.ExitCode)
|
|
}
|
|
if res.Duration <= 0 {
|
|
t.Fatalf("Duration = %s, want >0", res.Duration)
|
|
}
|
|
if res.Metadata == nil || res.Metadata["adapter"] != "seriatim_subprocess" {
|
|
t.Fatalf("Metadata = %#v, want adapter marker", res.Metadata)
|
|
}
|
|
|
|
assertJSONFile(t, req.OutputMergedTranscriptPath)
|
|
assertJSONFile(t, req.ReportPath)
|
|
|
|
if _, err := os.Stat(req.GeneratedConfigPath); err != nil {
|
|
t.Fatalf("generated config missing: %v", err)
|
|
}
|
|
if _, err := os.Stat(req.StdoutLogPath); err != nil {
|
|
t.Fatalf("stdout log missing: %v", err)
|
|
}
|
|
if _, err := os.Stat(req.StderrLogPath); err != nil {
|
|
t.Fatalf("stderr log missing: %v", err)
|
|
}
|
|
|
|
rec := readHelperRecord(t, recordPath)
|
|
wantArgs := []string{
|
|
"merge",
|
|
"--input-file", req.InputTranscriptPaths[0],
|
|
"--input-file", req.InputTranscriptPaths[1],
|
|
"--output-file", req.OutputMergedTranscriptPath,
|
|
"--report-file", req.ReportPath,
|
|
"--speakers", req.SpeakersPath,
|
|
"--autocorrect", req.AutocorrectPath,
|
|
"--output-schema", "seriatim-intermediate",
|
|
"--coalesce-gap", "3",
|
|
}
|
|
if strings.Join(rec.Args, "\n") != strings.Join(wantArgs, "\n") {
|
|
t.Fatalf("args = %#v, want %#v", rec.Args, wantArgs)
|
|
}
|
|
if rec.Env["SERIATIM_OVERLAP_WORD_RUN_GAP"] != "1" {
|
|
t.Fatalf("SERIATIM_OVERLAP_WORD_RUN_GAP = %q, want 1", rec.Env["SERIATIM_OVERLAP_WORD_RUN_GAP"])
|
|
}
|
|
if rec.Env["SERIATIM_OVERLAP_WORD_RUN_REORDER_WINDOW"] != "1" {
|
|
t.Fatalf("SERIATIM_OVERLAP_WORD_RUN_REORDER_WINDOW = %q, want 1", rec.Env["SERIATIM_OVERLAP_WORD_RUN_REORDER_WINDOW"])
|
|
}
|
|
if rec.Env["SERIATIM_BACKCHANNEL_MAX_DURATION"] != "2" {
|
|
t.Fatalf("SERIATIM_BACKCHANNEL_MAX_DURATION = %q, want 2", rec.Env["SERIATIM_BACKCHANNEL_MAX_DURATION"])
|
|
}
|
|
if rec.Env["SERIATIM_FILLER_MAX_DURATION"] != "1.25" {
|
|
t.Fatalf("SERIATIM_FILLER_MAX_DURATION = %q, want 1.25", rec.Env["SERIATIM_FILLER_MAX_DURATION"])
|
|
}
|
|
}
|
|
|
|
func TestSubprocessRunnerSubprocessFailure(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("helper wrapper script uses /bin/sh")
|
|
}
|
|
t.Setenv("GO_WANT_SERIATIM_HELPER", "1")
|
|
t.Setenv("SERIATIM_HELPER_MODE", "fail")
|
|
t.Setenv("SERIATIM_HELPER_RECORD_PATH", filepath.Join(t.TempDir(), "record.json"))
|
|
|
|
runner := mustRunner(t, writeHelperWrapper(t), false)
|
|
req := mergeReqForTest(t, false)
|
|
_, err := runner.Run(context.Background(), req)
|
|
if err == nil {
|
|
t.Fatal("Run() error = nil, want non-nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "run seriatim merge") {
|
|
t.Fatalf("error = %q, want subprocess context", err.Error())
|
|
}
|
|
if !strings.Contains(err.Error(), "exit code") {
|
|
t.Fatalf("error = %q, want exit code context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestSubprocessRunnerMissingOutputFails(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("helper wrapper script uses /bin/sh")
|
|
}
|
|
t.Setenv("GO_WANT_SERIATIM_HELPER", "1")
|
|
t.Setenv("SERIATIM_HELPER_MODE", "missing_output")
|
|
t.Setenv("SERIATIM_HELPER_RECORD_PATH", filepath.Join(t.TempDir(), "record.json"))
|
|
|
|
runner := mustRunner(t, writeHelperWrapper(t), false)
|
|
req := mergeReqForTest(t, false)
|
|
_, err := runner.Run(context.Background(), req)
|
|
if err == nil {
|
|
t.Fatal("Run() error = nil, want non-nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "validate seriatim merged output") {
|
|
t.Fatalf("error = %q, want output validation context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestSubprocessRunnerInvalidOutputJSONFails(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("helper wrapper script uses /bin/sh")
|
|
}
|
|
t.Setenv("GO_WANT_SERIATIM_HELPER", "1")
|
|
t.Setenv("SERIATIM_HELPER_MODE", "invalid_output")
|
|
t.Setenv("SERIATIM_HELPER_RECORD_PATH", filepath.Join(t.TempDir(), "record.json"))
|
|
|
|
runner := mustRunner(t, writeHelperWrapper(t), false)
|
|
req := mergeReqForTest(t, false)
|
|
_, err := runner.Run(context.Background(), req)
|
|
if err == nil {
|
|
t.Fatal("Run() error = nil, want non-nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "parse json") {
|
|
t.Fatalf("error = %q, want parse json context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestSubprocessRunnerInvalidReportJSONFails(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("helper wrapper script uses /bin/sh")
|
|
}
|
|
t.Setenv("GO_WANT_SERIATIM_HELPER", "1")
|
|
t.Setenv("SERIATIM_HELPER_MODE", "invalid_report")
|
|
t.Setenv("SERIATIM_HELPER_RECORD_PATH", filepath.Join(t.TempDir(), "record.json"))
|
|
|
|
runner := mustRunner(t, writeHelperWrapper(t), true)
|
|
req := mergeReqForTest(t, true)
|
|
_, err := runner.Run(context.Background(), req)
|
|
if err == nil {
|
|
t.Fatal("Run() error = nil, want non-nil")
|
|
}
|
|
if !strings.Contains(err.Error(), "validate seriatim report output") {
|
|
t.Fatalf("error = %q, want report validation context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestSubprocessRunnerConstructorValidation(t *testing.T) {
|
|
_, err := NewSubprocessRunnerFromConfigValues("", "10m", "seriatim-intermediate", nil, true, EnvConfig{})
|
|
if err == nil {
|
|
t.Fatal("expected binary validation error")
|
|
}
|
|
_, err = NewSubprocessRunnerFromConfigValues("seriatim", "bad", "seriatim-intermediate", nil, true, EnvConfig{})
|
|
if err == nil {
|
|
t.Fatal("expected timeout parse error")
|
|
}
|
|
_, err = NewSubprocessRunner(SubprocessRunnerConfig{
|
|
Binary: "seriatim",
|
|
Timeout: mustParseDuration(t, "1s"),
|
|
OutputSchema: "bad-schema",
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected output schema validation error")
|
|
}
|
|
}
|
|
|
|
type helperRecord struct {
|
|
Args []string `json:"args"`
|
|
Env map[string]string `json:"env"`
|
|
}
|
|
|
|
func TestSeriatimSubprocessHelper(t *testing.T) {
|
|
if os.Getenv("GO_WANT_SERIATIM_HELPER") != "1" {
|
|
return
|
|
}
|
|
|
|
args := os.Args
|
|
start := -1
|
|
for i := range args {
|
|
if args[i] == "--" {
|
|
start = i + 1
|
|
break
|
|
}
|
|
}
|
|
if start < 0 || start >= len(args) {
|
|
_, _ = os.Stderr.WriteString("missing -- args separator\n")
|
|
os.Exit(2)
|
|
}
|
|
mergeArgs := args[start:]
|
|
|
|
outputPath := flagValue(mergeArgs, "--output-file")
|
|
reportPath := flagValue(mergeArgs, "--report-file")
|
|
recordPath := os.Getenv("SERIATIM_HELPER_RECORD_PATH")
|
|
if strings.TrimSpace(recordPath) != "" {
|
|
rec := helperRecord{
|
|
Args: mergeArgs,
|
|
Env: map[string]string{
|
|
"SERIATIM_OVERLAP_WORD_RUN_GAP": os.Getenv("SERIATIM_OVERLAP_WORD_RUN_GAP"),
|
|
"SERIATIM_OVERLAP_WORD_RUN_REORDER_WINDOW": os.Getenv("SERIATIM_OVERLAP_WORD_RUN_REORDER_WINDOW"),
|
|
"SERIATIM_BACKCHANNEL_MAX_DURATION": os.Getenv("SERIATIM_BACKCHANNEL_MAX_DURATION"),
|
|
"SERIATIM_FILLER_MAX_DURATION": os.Getenv("SERIATIM_FILLER_MAX_DURATION"),
|
|
},
|
|
}
|
|
data, _ := json.Marshal(rec)
|
|
_ = os.MkdirAll(filepath.Dir(recordPath), 0o755)
|
|
_ = os.WriteFile(recordPath, data, 0o644)
|
|
}
|
|
|
|
mode := os.Getenv("SERIATIM_HELPER_MODE")
|
|
switch mode {
|
|
case "success":
|
|
writeSeriatimHelperFile(outputPath, `{"merged":true}`)
|
|
if reportPath != "" {
|
|
writeSeriatimHelperFile(reportPath, `{"report":true}`)
|
|
}
|
|
_, _ = os.Stdout.WriteString("seriatim helper success stdout\n")
|
|
_, _ = os.Stderr.WriteString("seriatim helper success stderr\n")
|
|
os.Exit(0)
|
|
case "fail":
|
|
_, _ = os.Stderr.WriteString("seriatim helper failure\n")
|
|
os.Exit(9)
|
|
case "missing_output":
|
|
if reportPath != "" {
|
|
writeSeriatimHelperFile(reportPath, `{"report":true}`)
|
|
}
|
|
os.Exit(0)
|
|
case "invalid_output":
|
|
writeSeriatimHelperFile(outputPath, `not-json`)
|
|
if reportPath != "" {
|
|
writeSeriatimHelperFile(reportPath, `{"report":true}`)
|
|
}
|
|
os.Exit(0)
|
|
case "invalid_report":
|
|
writeSeriatimHelperFile(outputPath, `{"merged":true}`)
|
|
if reportPath != "" {
|
|
writeSeriatimHelperFile(reportPath, `not-json`)
|
|
}
|
|
os.Exit(0)
|
|
default:
|
|
_, _ = os.Stderr.WriteString(fmt.Sprintf("unknown helper mode %q\n", mode))
|
|
os.Exit(2)
|
|
}
|
|
}
|
|
|
|
func writeHelperWrapper(t *testing.T) string {
|
|
t.Helper()
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatalf("os.Executable() error = %v", err)
|
|
}
|
|
path := filepath.Join(t.TempDir(), "seriatim-helper-wrapper.sh")
|
|
content := "#!/bin/sh\nexec \"" + exe + "\" -test.run=TestSeriatimSubprocessHelper -- \"$@\"\n"
|
|
if err := os.WriteFile(path, []byte(content), 0o755); err != nil {
|
|
t.Fatalf("WriteFile(%q) error = %v", path, err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func mergeReqForTest(t *testing.T, withReport bool) MergeRequest {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
in1 := filepath.Join(dir, "a.json")
|
|
in2 := filepath.Join(dir, "b.json")
|
|
writeSeriatimFile(t, in1, `{"a":1}`)
|
|
writeSeriatimFile(t, in2, `{"b":1}`)
|
|
req := MergeRequest{
|
|
GeneratedConfigPath: filepath.Join(dir, "seriatim.generated.yml"),
|
|
InputTranscriptPaths: []string{in1, in2},
|
|
OutputMergedTranscriptPath: filepath.Join(dir, "merged.json"),
|
|
StdoutLogPath: filepath.Join(dir, "seriatim.stdout.log"),
|
|
StderrLogPath: filepath.Join(dir, "seriatim.stderr.log"),
|
|
}
|
|
if withReport {
|
|
req.ReportPath = filepath.Join(dir, "seriatim.report.json")
|
|
}
|
|
return req
|
|
}
|
|
|
|
func mustRunner(t *testing.T, binary string, report bool) *SubprocessRunner {
|
|
t.Helper()
|
|
coalesce := 3.0
|
|
r, err := NewSubprocessRunner(SubprocessRunnerConfig{
|
|
Binary: binary,
|
|
Timeout: mustParseDuration(t, "2s"),
|
|
OutputSchema: "seriatim-intermediate",
|
|
CoalesceGap: &coalesce,
|
|
Report: report,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewSubprocessRunner() error = %v", err)
|
|
}
|
|
return r
|
|
}
|
|
|
|
func mustParseDuration(t *testing.T, v string) time.Duration {
|
|
t.Helper()
|
|
d, err := time.ParseDuration(v)
|
|
if err != nil {
|
|
t.Fatalf("time.ParseDuration(%q) error = %v", v, err)
|
|
}
|
|
return d
|
|
}
|
|
|
|
func flagValue(args []string, name string) string {
|
|
for i := 0; i < len(args)-1; i++ {
|
|
if args[i] == name {
|
|
return args[i+1]
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func writeSeriatimFile(t *testing.T, path, contents string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatalf("MkdirAll(%q): %v", path, err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
|
|
t.Fatalf("WriteFile(%q): %v", path, err)
|
|
}
|
|
}
|
|
|
|
func writeSeriatimHelperFile(path, contents string) {
|
|
if strings.TrimSpace(path) == "" {
|
|
return
|
|
}
|
|
_ = os.MkdirAll(filepath.Dir(path), 0o755)
|
|
_ = os.WriteFile(path, []byte(contents), 0o644)
|
|
}
|
|
|
|
func assertJSONFile(t *testing.T, path string) {
|
|
t.Helper()
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("ReadFile(%q): %v", path, err)
|
|
}
|
|
var v any
|
|
if err := json.Unmarshal(data, &v); err != nil {
|
|
t.Fatalf("json unmarshal %q: %v", path, err)
|
|
}
|
|
}
|
|
|
|
func readHelperRecord(t *testing.T, path string) helperRecord {
|
|
t.Helper()
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("ReadFile(%q): %v", path, err)
|
|
}
|
|
var rec helperRecord
|
|
if err := json.Unmarshal(data, &rec); err != nil {
|
|
t.Fatalf("json unmarshal helper record: %v", err)
|
|
}
|
|
return rec
|
|
}
|