Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 765aa8d3e1 | |||
| 326a8d6890 |
@@ -0,0 +1,133 @@
|
||||
# MXL Pattern Generator Architecture Review
|
||||
|
||||
Reviewed on 2026-09-16 against commit `f37f480`.
|
||||
|
||||
## Summary
|
||||
|
||||
The current stack is a good fit for a test-pattern generator:
|
||||
|
||||
- Go is well suited to CLI handling, MXL lifecycle management, timing, and synthetic audio generation.
|
||||
- `go-mxl` provides the correct data model: discrete grains for video and continuous samples for audio.
|
||||
- wgpu/WGSL is a reasonable portable GPU abstraction for producing v210 video.
|
||||
- CPU-rendered text is appropriate because text changes infrequently and font rendering does not need to be implemented in a shader.
|
||||
|
||||
The project is still prototype-quality in its orchestration and configuration layers. Before adding substantial audio functionality, flow-definition handling, lifecycle management, and the failing tests should be corrected.
|
||||
|
||||
## Recommended audio architecture
|
||||
|
||||
Video and audio should run in separate goroutines. Each goroutine should own its own MXL writer and timing loop because video and audio have different units and rates:
|
||||
|
||||
```text
|
||||
run
|
||||
|-- shared context, signal handling, and error propagation
|
||||
|-- video goroutine
|
||||
| OpenGrain -> render -> overlay -> Commit
|
||||
`-- audio goroutine
|
||||
OpenSamples -> synthesize -> Commit
|
||||
```
|
||||
|
||||
This is also consistent with `go-mxl`: a writer is not safe for concurrent use, so a writer should belong to exactly one goroutine.
|
||||
|
||||
The goroutines should not synchronize by sending a notification for every video frame. That would make audio timing depend on GPU latency and Go scheduler jitter. Instead, both loops should use the MXL clock independently:
|
||||
|
||||
```go
|
||||
videoIndex := mxl.CurrentIndex(videoRate)
|
||||
audioIndex := mxl.CurrentIndex(audioRate)
|
||||
```
|
||||
|
||||
After committing data, each loop advances its own index and uses `mxl.NsUntilIndex` for pacing. Both loops should share a `context.Context`; an error in either flow should cancel the other flow and be returned to the caller.
|
||||
|
||||
For audio, start with CPU generation and approximately 10 ms batches (480 samples at 48 kHz). A sine wave is inexpensive to calculate, so GPU audio generation would add complexity without a useful performance benefit.
|
||||
|
||||
Audio phase should be derived from the absolute sample index:
|
||||
|
||||
```go
|
||||
phase := 2 * math.Pi * frequency *
|
||||
float64(firstSample+i) / sampleRate
|
||||
```
|
||||
|
||||
This preserves continuity across batches and after a timing resynchronization.
|
||||
|
||||
Video and audio should expose different interfaces because a video frame and a range of audio samples are fundamentally different units:
|
||||
|
||||
```go
|
||||
type VideoGenerator interface {
|
||||
GenerateFrame(dst []byte, frameIndex uint64) error
|
||||
Close() error
|
||||
}
|
||||
|
||||
type AudioGenerator interface {
|
||||
GenerateSamples(channels [][]byte, firstSample, sampleCount uint64) error
|
||||
Close() error
|
||||
}
|
||||
```
|
||||
|
||||
For `audio/float32`, the audio package may use `[]float32` internally and keep byte encoding at the MXL boundary.
|
||||
|
||||
## Issue status
|
||||
|
||||
| # | Issue | Status | Current observation |
|
||||
|---|---|---|---|
|
||||
| 1 | The validated `--domain` value was ignored in favor of `/dev/shm/mxl`. | **Fixed** | Logging, `mxl.NewInstance`, and reuse messages now use `args.domain`. |
|
||||
| 2 | An external video flow definition does not populate runtime width, height, FPS, or UUID. | **Open** | The JSON is read as an opaque string while `vi` remains zero-valued. `NewWGPUGenerator` therefore receives `0, 0`. Parse and validate the definition before generator creation, or introduce a typed configuration source. |
|
||||
| 3 | `checkArgs` received `appArgs` by value, so generated UUIDs were discarded. | **Partially fixed** | `checkArgs` now receives `*appArgs`. However, the generated video UUID is still overwritten by a hard-coded UUID in `main`; remove that assignment before release. |
|
||||
| 4 | The default pattern was `bars`, which did not exist. | **Fixed** | The default is now `ebu75`, which exists in the pattern registry. |
|
||||
| 5 | `NewFlowDefJSON(TYPE_AUDIO, ...)` produces a video/v210 definition. | **Open** | The function accepts `TYPE_AUDIO`, but all emitted format and media fields remain video-specific. Create separate typed video and audio definition builders. |
|
||||
| 6 | The wgpu path was described as zero-copy although it performs GPU readback and a CPU copy. | **Deferred — fix after audio** | Every frame is copied from GPU storage to a mapped host buffer and then copied into the MXL payload. The path is synchronous and serial. This may require substantial benchmarking and architectural work, so audio implementation takes priority. Update the documentation now, but defer optimization or redesign until audio is complete. |
|
||||
| 7 | The test suite had three failures. | **Open** | `TestNewTextOverlay`, `TestWGPUMoveSquare`, and `TestWGPUGenerator` still fail. |
|
||||
| 8 | The Makefile clean target uses `fm -f` instead of `rm -f`. | **Open** | The typo remains in the `clean` target. |
|
||||
|
||||
## Additional implementation priorities
|
||||
|
||||
1. Fix the existing tests or update incorrect expectations after confirming the intended color values and overlay positioning.
|
||||
2. Split video and audio flow-definition types. Avoid an integer `feedType` accepted by a function that has a video-only parameter list.
|
||||
3. Parse external flow definitions into typed configuration before creating generators. Validate format, media type, dimensions, rate, and channel count.
|
||||
4. Remove the hard-coded video UUID.
|
||||
5. Introduce a `run(...) error` orchestration function using shared cancellation and error propagation instead of calling `log.Fatalf` throughout the media loop.
|
||||
6. Add a CPU audio generator and continuous-flow writer loop using `OpenSamples`, `ChannelFragments`, and `Commit`.
|
||||
7. After audio is complete, measure end-to-end frame time and missed deadlines at 1080p50/60 and UHD. The current wgpu path may be adequate, but it is neither zero-copy nor asynchronous. Treat GPU readback optimization as a separate, potentially large task.
|
||||
|
||||
## Suggested package layout
|
||||
|
||||
```text
|
||||
cmd/mxl-pattern/
|
||||
main.go
|
||||
|
||||
internal/config/
|
||||
config.go
|
||||
flow.go
|
||||
|
||||
internal/video/
|
||||
generator.go
|
||||
wgpu.go
|
||||
overlay.go
|
||||
runner.go
|
||||
|
||||
internal/audio/
|
||||
generator.go
|
||||
sine.go
|
||||
runner.go
|
||||
|
||||
internal/app/
|
||||
run.go
|
||||
```
|
||||
|
||||
The exact directory names are less important than keeping CLI parsing, typed configuration, media generation, and MXL writing as separate responsibilities.
|
||||
|
||||
## Verification
|
||||
|
||||
The following command was used:
|
||||
|
||||
```sh
|
||||
GOCACHE=/tmp/go-mxl-gen-cache go test ./...
|
||||
```
|
||||
|
||||
Package compilation succeeds, but the generator package fails these tests:
|
||||
|
||||
- `TestNewTextOverlay`: expected a centered text box, but its center was reported as 45 instead of approximately 960.
|
||||
- `TestWGPUMoveSquare`: tick 79 produced `590/512/512` instead of expected `893/176/543`.
|
||||
- `TestWGPUGenerator`: pixel `(0,0)` produced Y=414 instead of expected Y=721.
|
||||
|
||||
## Conclusion
|
||||
|
||||
Keep the chosen Go + go-mxl + wgpu stack. Implement audio on the CPU in its own goroutine and give video and audio separate writers, indices, and pacing loops. Coordinate them through a shared context and the common MXL timebase, not through per-frame messages. The immediate blockers are external-flow configuration, audio flow-definition support, and the currently failing tests.
|
||||
@@ -17,4 +17,4 @@ test:
|
||||
|
||||
clean:
|
||||
go clean
|
||||
fm -f ./build/mxl-gen
|
||||
rm -f ./build/mxl-gen
|
||||
|
||||
+163
-113
@@ -4,10 +4,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -47,6 +51,11 @@ type appArgs struct {
|
||||
audioUUID string
|
||||
}
|
||||
|
||||
type parseResult struct {
|
||||
args appArgs
|
||||
shouldRun bool
|
||||
}
|
||||
|
||||
var frameRates = map[string]mxl.Rational{
|
||||
"23.97": {Num: 24000, Den: 1001},
|
||||
"24": {Num: 24, Den: 1},
|
||||
@@ -67,119 +76,126 @@ var samplingRates = map[string]mxl.Rational{
|
||||
"192": {Num: 192000, Den: 1},
|
||||
}
|
||||
|
||||
func printHelp(fs *pflag.FlagSet) {
|
||||
fmt.Printf("%s %s\n", APP_NAME, APP_VER)
|
||||
fmt.Println("Usage: mxl-gen -d <domain> [-v <flowDef.json>] [-a <flowDef.json>] [options]")
|
||||
fmt.Println(" or: mxl-gen -d <domain> [--with <width px>] [--height <height px>] [--fps <framerate>] \\")
|
||||
fmt.Println(" [-c <channels amount>] [-f <sample rate>]")
|
||||
fmt.Println(" or: mxl-gen -d <domain> with default params")
|
||||
fmt.Println("Video and audio feeds params will be ignored, if flow definition file provided.")
|
||||
fmt.Println()
|
||||
func printHelp(w io.Writer, fs *pflag.FlagSet) {
|
||||
fmt.Fprintf(w, "%s %s\n", APP_NAME, APP_VER)
|
||||
fmt.Fprintln(w, "Usage: mxl-gen -d <domain> [-v <flowDef.json>] [-a <flowDef.json>] [options]")
|
||||
fmt.Fprintln(w, " or: mxl-gen -d <domain> [--width <width px>] [--height <height px>] [--fps <framerate>] \\")
|
||||
fmt.Fprintln(w, " [-c <channels amount>] [-f <sample rate>]")
|
||||
fmt.Fprintln(w, " or: mxl-gen -d <domain> with default params")
|
||||
fmt.Fprintln(w, "Video and audio feed parameters are ignored when a flow definition file is provided.")
|
||||
fmt.Fprintln(w)
|
||||
fs.SetOutput(w)
|
||||
fs.PrintDefaults()
|
||||
}
|
||||
|
||||
func printUsage() {
|
||||
fmt.Fprintln(os.Stderr, "Usage: mxl-gen -d <domain> [-v <flowDef.json>] [-a <flowDef.json>] [options]")
|
||||
fmt.Fprintln(os.Stderr, "Try 'mxl-gen -h' for more information.")
|
||||
func printUsage(w io.Writer) {
|
||||
fmt.Fprintln(w, "Usage: mxl-gen -d <domain> [-v <flowDef.json>] [-a <flowDef.json>] [options]")
|
||||
fmt.Fprintln(w, "Try 'mxl-gen -h' for more information.")
|
||||
}
|
||||
|
||||
func checkArgs(args *appArgs) {
|
||||
printUsageAndExit := func() {
|
||||
printUsage()
|
||||
os.Exit(2)
|
||||
func validateDomain(domain string) error {
|
||||
if domain == "" {
|
||||
return fmt.Errorf("domain is required")
|
||||
}
|
||||
// domain
|
||||
if args.domain == "" {
|
||||
fmt.Fprintf(os.Stderr, "Domain is required\n")
|
||||
printUsageAndExit()
|
||||
fi, err := os.Stat(domain)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid MXL domain %q: %w", domain, err)
|
||||
}
|
||||
fi, err := os.Stat(args.domain)
|
||||
if err != nil || !fi.IsDir() {
|
||||
fmt.Fprintf(os.Stderr, "Invalid MXL domain: %s\n", args.domain)
|
||||
fmt.Fprintf(os.Stderr, "Reason: %v\n", err)
|
||||
printUsageAndExit()
|
||||
if !fi.IsDir() {
|
||||
return fmt.Errorf("invalid MXL domain %q: not a directory", domain)
|
||||
}
|
||||
if ok, err := mxl.IsTmpFs(args.domain); err != nil || !ok {
|
||||
fmt.Fprintf(os.Stderr, "Invalid MXL domain: %s\n", args.domain)
|
||||
fmt.Fprintln(os.Stderr, "Domain must be directory in tmps.")
|
||||
printUsageAndExit()
|
||||
}
|
||||
// FlowDef
|
||||
checkFlowDef := func(label, flowDef string) {
|
||||
fi, err := os.Stat(flowDef)
|
||||
if err != nil || fi.IsDir() {
|
||||
fmt.Fprintf(os.Stderr, "%s flow definition .json file is not accesible\n", label)
|
||||
printUsageAndExit()
|
||||
if ok, err := mxl.IsTmpFs(domain); err != nil || !ok {
|
||||
if err != nil {
|
||||
return fmt.Errorf("check MXL domain %q: %w", domain, err)
|
||||
}
|
||||
return fmt.Errorf("invalid MXL domain %q: directory must be on tmpfs", domain)
|
||||
}
|
||||
videoFlowDefProvided, audioFlowDefProvided := false, false
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateFlowDefPath(label, path string) error {
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s flow definition %q is not accessible: %w", label, path, err)
|
||||
}
|
||||
if fi.IsDir() {
|
||||
return fmt.Errorf("%s flow definition %q is a directory", label, path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateVideoArgs(args *appArgs) error {
|
||||
if args.videoFlowDefFile != "" {
|
||||
checkFlowDef("Video", args.videoFlowDefFile)
|
||||
videoFlowDefProvided = true
|
||||
return nil
|
||||
}
|
||||
if args.audioFlowDefFile != "" {
|
||||
checkFlowDef("Audio", args.audioFlowDefFile)
|
||||
audioFlowDefProvided = true
|
||||
if args.videoWidth == 0 || args.videoWidth%6 != 0 {
|
||||
// v210 stores 6 pixels in each 16-byte block.
|
||||
return fmt.Errorf("video width must be greater than zero and divisible by 6")
|
||||
}
|
||||
if args.videoHeight == 0 {
|
||||
return fmt.Errorf("video height must be greater than zero")
|
||||
}
|
||||
if _, exists := frameRates[args.videoFPS]; !exists {
|
||||
return fmt.Errorf("unsupported video FPS %q (supported: %s); use a flow definition for other rates",
|
||||
args.videoFPS, sortedMapKeys(frameRates))
|
||||
}
|
||||
if _, exists := patterns[args.pattern]; !exists {
|
||||
return fmt.Errorf("unknown video pattern %q (use --list-patterns to see available patterns)", args.pattern)
|
||||
}
|
||||
if args.videoUUID == "" {
|
||||
args.videoUUID = uuid.NewString()
|
||||
return nil
|
||||
}
|
||||
if _, err := uuid.Parse(args.videoUUID); err != nil {
|
||||
return fmt.Errorf("invalid video UUID %q: %w", args.videoUUID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if !videoFlowDefProvided {
|
||||
if args.videoWidth == 0 || args.videoWidth%6 != 0 {
|
||||
// width%6 == 0 - because of v210 (6 pixels per 16-byte block)
|
||||
fmt.Fprintf(os.Stderr, "Video width must be > 0 and divisible by 6\n")
|
||||
printUsageAndExit()
|
||||
}
|
||||
if args.videoHeight == 0 {
|
||||
fmt.Fprintf(os.Stderr, "Video height must be > 0\n")
|
||||
printUsageAndExit()
|
||||
}
|
||||
if _, exists := frameRates[args.videoFPS]; !exists {
|
||||
fmt.Fprintf(os.Stderr, "FPS %s is not in available list.\n", args.videoFPS)
|
||||
fmt.Fprintln(os.Stderr, "If you need more complex solution, use flow definition .json instead.")
|
||||
fmt.Fprintln(os.Stderr, "Available list:")
|
||||
for key, _ := range frameRates {
|
||||
fmt.Fprintf(os.Stderr, " %s\n", key)
|
||||
}
|
||||
printUsageAndExit()
|
||||
}
|
||||
if args.videoUUID != "" {
|
||||
if _, err := uuid.Parse(args.videoUUID); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Video UUID %s is not valid.\n", args.videoUUID)
|
||||
printUsageAndExit()
|
||||
}
|
||||
} else {
|
||||
args.videoUUID = uuid.NewString()
|
||||
}
|
||||
if args.pattern != "" {
|
||||
if _, exists := patterns[args.pattern]; !exists {
|
||||
fmt.Fprintf(os.Stderr, "Pattern %s is not in available list.\n", args.pattern)
|
||||
listPatterns(os.Stderr)
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
func validateAudioArgs(args *appArgs) error {
|
||||
if args.audioFlowDefFile != "" || args.audioChannels == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, exists := samplingRates[args.audioSamplingFreq]; !exists {
|
||||
return fmt.Errorf("unsupported audio sample rate %q (supported: %s); use a flow definition for other rates",
|
||||
args.audioSamplingFreq, sortedMapKeys(samplingRates))
|
||||
}
|
||||
if args.audioUUID == "" {
|
||||
args.audioUUID = uuid.NewString()
|
||||
return nil
|
||||
}
|
||||
if err := uuid.Validate(args.audioUUID); err != nil {
|
||||
return fmt.Errorf("invalid audio UUID %q: %w", args.audioUUID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if !audioFlowDefProvided {
|
||||
if args.audioChannels == 0 {
|
||||
// TODO: ignore audio
|
||||
}
|
||||
if _, exists := samplingRates[args.audioSamplingFreq]; !exists {
|
||||
fmt.Fprintf(os.Stderr, "Sample rate %s is not in available list.\n", args.audioSamplingFreq)
|
||||
fmt.Fprintln(os.Stderr, "If you need more complex solution, use flow definition .json instead.")
|
||||
fmt.Fprintln(os.Stderr, "Available list:")
|
||||
for key, _ := range samplingRates {
|
||||
fmt.Fprintf(os.Stderr, " %s\n", key)
|
||||
}
|
||||
printUsageAndExit()
|
||||
}
|
||||
if args.audioUUID != "" {
|
||||
if err := uuid.Validate(args.audioUUID); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Audio UUID %s is not valid.\n", args.audioUUID)
|
||||
printUsageAndExit()
|
||||
}
|
||||
} else {
|
||||
args.audioUUID = uuid.NewString()
|
||||
func validateArgs(args *appArgs) error {
|
||||
checks := []func() error{
|
||||
func() error { return validateDomain(args.domain) },
|
||||
func() error { return validateFlowDefPath("video", args.videoFlowDefFile) },
|
||||
func() error { return validateFlowDefPath("audio", args.audioFlowDefFile) },
|
||||
func() error { return validateVideoArgs(args) },
|
||||
func() error { return validateAudioArgs(args) },
|
||||
}
|
||||
for _, check := range checks {
|
||||
if err := check(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sortedMapKeys[V any](values map[string]V) string {
|
||||
keys := make([]string, 0, len(values))
|
||||
for key := range values {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return strings.Join(keys, ", ")
|
||||
}
|
||||
|
||||
type pattern struct {
|
||||
@@ -252,21 +268,25 @@ var patterns = map[string]pattern{
|
||||
},
|
||||
}
|
||||
|
||||
func listPatterns(f *os.File) {
|
||||
fmt.Fprintln(f, "List of available video patterns:")
|
||||
var maxNameWidth int = 0
|
||||
for name, _ := range patterns {
|
||||
func listPatterns(w io.Writer) {
|
||||
fmt.Fprintln(w, "List of available video patterns:")
|
||||
names := make([]string, 0, len(patterns))
|
||||
maxNameWidth := 0
|
||||
for name := range patterns {
|
||||
names = append(names, name)
|
||||
l := len(name)
|
||||
if l > maxNameWidth {
|
||||
maxNameWidth = l
|
||||
}
|
||||
}
|
||||
for name, p := range patterns {
|
||||
fmt.Fprintf(f, " %-*s - %s\n", maxNameWidth, name, p.description)
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
p := patterns[name]
|
||||
fmt.Fprintf(w, " %-*s - %s\n", maxNameWidth, name, p.description)
|
||||
}
|
||||
}
|
||||
|
||||
func flagSetAddFlags(fs *pflag.FlagSet, args *appArgs) {
|
||||
func addFlags(fs *pflag.FlagSet, args *appArgs) {
|
||||
// common flags
|
||||
fs.BoolVarP(&args.showHelp, "help", "h", false, "Show help message and exit")
|
||||
// MXL flags
|
||||
@@ -308,27 +328,45 @@ func flagSetAddFlags(fs *pflag.FlagSet, args *appArgs) {
|
||||
fs.StringVar(&args.audioUUID, "audio-id", "", "Audio UUID. Will be created, if not provided [TODO]")
|
||||
}
|
||||
|
||||
func main() {
|
||||
func parseArgs(argv []string, stdout, stderr io.Writer) (parseResult, error) {
|
||||
var args appArgs
|
||||
flagSet := pflag.NewFlagSet("args", pflag.ContinueOnError)
|
||||
flagSet.SortFlags = false
|
||||
flagSet.Usage = func() { printUsage() }
|
||||
flagSetAddFlags(flagSet, &args)
|
||||
flagSet.SetOutput(stderr)
|
||||
flagSet.Usage = func() { printUsage(stderr) }
|
||||
addFlags(flagSet, &args)
|
||||
|
||||
if err := flagSet.Parse(os.Args[1:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
printUsage()
|
||||
os.Exit(2)
|
||||
if err := flagSet.Parse(argv); err != nil {
|
||||
return parseResult{}, err
|
||||
}
|
||||
if args.showHelp {
|
||||
printHelp(flagSet)
|
||||
return
|
||||
printHelp(stdout, flagSet)
|
||||
return parseResult{args: args}, nil
|
||||
}
|
||||
if args.listPatterns {
|
||||
listPatterns(os.Stderr)
|
||||
listPatterns(stdout)
|
||||
return parseResult{args: args}, nil
|
||||
}
|
||||
if flagSet.NArg() != 0 {
|
||||
return parseResult{}, fmt.Errorf("unexpected positional arguments: %v", flagSet.Args())
|
||||
}
|
||||
if err := validateArgs(&args); err != nil {
|
||||
return parseResult{}, err
|
||||
}
|
||||
return parseResult{args: args, shouldRun: true}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
parsed, err := parseArgs(os.Args[1:], os.Stdout, os.Stderr)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
printUsage(os.Stderr)
|
||||
os.Exit(2)
|
||||
}
|
||||
if !parsed.shouldRun {
|
||||
return
|
||||
}
|
||||
checkArgs(&args)
|
||||
args := parsed.args
|
||||
|
||||
type videoInfo struct {
|
||||
uuid string
|
||||
@@ -470,3 +508,15 @@ func main() {
|
||||
mxl.SleepNs(mxl.NsUntilIndex(idx, rate))
|
||||
}
|
||||
}
|
||||
|
||||
func run(ctx context.Context, args []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func runVideo(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func runAudio(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseArgsHelpStopsBeforeValidation(t *testing.T) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
||||
result, err := parseArgs([]string{"--help"}, &stdout, &stderr)
|
||||
if err != nil {
|
||||
t.Fatalf("parseArgs: %v", err)
|
||||
}
|
||||
if result.shouldRun {
|
||||
t.Fatal("shouldRun = true, want false")
|
||||
}
|
||||
if !result.args.showHelp {
|
||||
t.Fatal("showHelp = false, want true")
|
||||
}
|
||||
if !strings.Contains(stdout.String(), "Usage: mxl-gen") {
|
||||
t.Fatalf("help output does not contain usage: %q", stdout.String())
|
||||
}
|
||||
if stderr.Len() != 0 {
|
||||
t.Fatalf("stderr = %q, want empty", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseArgsListPatternsStopsBeforeValidation(t *testing.T) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
||||
result, err := parseArgs([]string{"--list-patterns"}, &stdout, &stderr)
|
||||
if err != nil {
|
||||
t.Fatalf("parseArgs: %v", err)
|
||||
}
|
||||
if result.shouldRun {
|
||||
t.Fatal("shouldRun = true, want false")
|
||||
}
|
||||
if !result.args.listPatterns {
|
||||
t.Fatal("listPatterns = false, want true")
|
||||
}
|
||||
if !strings.Contains(stdout.String(), "ebu75") {
|
||||
t.Fatalf("pattern output does not contain ebu75: %q", stdout.String())
|
||||
}
|
||||
if stderr.Len() != 0 {
|
||||
t.Fatalf("stderr = %q, want empty", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseArgsRejectsUnexpectedPositionalArguments(t *testing.T) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
||||
result, err := parseArgs([]string{"unexpected"}, &stdout, &stderr)
|
||||
if err == nil {
|
||||
t.Fatal("parseArgs returned nil error")
|
||||
}
|
||||
if result.shouldRun {
|
||||
t.Fatal("shouldRun = true, want false")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "unexpected positional arguments") {
|
||||
t.Fatalf("error = %q", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseArgsRejectsUnknownFlag(t *testing.T) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
||||
result, err := parseArgs([]string{"--not-a-flag"}, &stdout, &stderr)
|
||||
if err == nil {
|
||||
t.Fatal("parseArgs returned nil error")
|
||||
}
|
||||
if result.shouldRun {
|
||||
t.Fatal("shouldRun = true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAudioArgsSkipsDisabledAudio(t *testing.T) {
|
||||
args := appArgs{
|
||||
audioChannels: 0,
|
||||
audioSamplingFreq: "unsupported",
|
||||
audioUUID: "not-a-uuid",
|
||||
}
|
||||
|
||||
if err := validateAudioArgs(&args); err != nil {
|
||||
t.Fatalf("validateAudioArgs: %v", err)
|
||||
}
|
||||
if args.audioUUID != "not-a-uuid" {
|
||||
t.Fatalf("audio UUID changed while audio is disabled: %q", args.audioUUID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPatternsIsSorted(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
listPatterns(&output)
|
||||
|
||||
text := output.String()
|
||||
if strings.Index(text, "ebu100") > strings.Index(text, "ebu75") {
|
||||
t.Fatalf("patterns are not sorted: %q", text)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user