From 4a7b8a074ac29a1e4b71cd5be0c3dbce306f4e9a Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Wed, 28 May 2025 15:33:53 +0100 Subject: [PATCH] check current active state before starting/stopping recording return appropriate errors if required update tests to reflect changes --- record.go | 23 ++++++++++++++++++++++- record_test.go | 44 ++++++++++++++++++++++++++------------------ 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/record.go b/record.go index e322d3f..a5978e6 100644 --- a/record.go +++ b/record.go @@ -22,7 +22,19 @@ type RecordStartCmd struct{} // size = 0x0 // Run executes the command to start recording. func (cmd *RecordStartCmd) Run(ctx *context) error { - _, err := ctx.Client.Record.StartRecord() + status, err := ctx.Client.Record.GetRecordStatus() + if err != nil { + return err + } + + if status.OutputActive { + if status.OutputPaused { + return fmt.Errorf("recording is already in progress and paused") + } + return fmt.Errorf("recording is already in progress") + } + + _, err = ctx.Client.Record.StartRecord() if err != nil { return err } @@ -35,6 +47,15 @@ type RecordStopCmd struct{} // size = 0x0 // Run executes the command to stop recording. func (cmd *RecordStopCmd) Run(ctx *context) error { + status, err := ctx.Client.Record.GetRecordStatus() + if err != nil { + return err + } + + if !status.OutputActive { + return fmt.Errorf("recording is not in progress") + } + resp, err := ctx.Client.Record.StopRecord() if err != nil { return err diff --git a/record_test.go b/record_test.go index 5cddd2b..a28af05 100644 --- a/record_test.go +++ b/record_test.go @@ -31,19 +31,23 @@ func TestRecordStart(t *testing.T) { cmdStart := &RecordStartCmd{} err = cmdStart.Run(context) + if active { + if err == nil { + t.Fatalf("Expected error when starting recording while active, got nil") + } + if !strings.Contains(err.Error(), "Recording is already in progress") { + t.Fatalf("Expected error message to contain 'Recording is already in progress', got '%s'", err.Error()) + } + return + } + if err != nil { t.Fatalf("Failed to start recording: %v", err) } - time.Sleep(1 * time.Second) // Wait for a second to ensure recording has started - if active { - if out.String() != "Recording is already in progress.\n" { - t.Fatalf("Expected output to be 'Recording is already in progress.', got '%s'", out.String()) - } - } else { - if !strings.Contains(out.String(), "Recording started successfully.") { - t.Fatalf("Expected output to contain 'Recording started successfully.', got '%s'", out.String()) - } + if out.String() != "Recording started successfully.\n" { + t.Fatalf("Expected output to contain 'Recording started successfully.', got '%s'", out.String()) } + time.Sleep(1 * time.Second) // Wait for the recording to start } func TestRecordStop(t *testing.T) { @@ -70,19 +74,23 @@ func TestRecordStop(t *testing.T) { cmdStop := &RecordStopCmd{} err = cmdStop.Run(context) + if !active { + if err == nil { + t.Fatalf("Expected error when stopping recording while inactive, got nil") + } + if !strings.Contains(err.Error(), "recording is not in progress") { + t.Fatalf("Expected error message to contain 'recording is not in progress', got '%s'", err.Error()) + } + return + } + if err != nil { t.Fatalf("Failed to stop recording: %v", err) } - time.Sleep(1 * time.Second) // Wait for a second to ensure recording has stopped - if !active { - if out.String() != "No recording is currently in progress.\n" { - t.Fatalf("Expected output to be 'No recording is currently in progress.', got '%s'", out.String()) - } - } else { - if !strings.Contains(out.String(), "Recording stopped successfully. Output file:") { - t.Fatalf("Expected output to contain 'Recording stopped successfully. Output file:', got '%s'", out.String()) - } + if !strings.Contains(out.String(), "Recording stopped successfully. Output file: ") { + t.Fatalf("Expected output to contain 'Recording stopped successfully. Output file: ', got '%s'", out.String()) } + time.Sleep(1 * time.Second) // Wait for the recording to stop } func TestRecordToggle(t *testing.T) {