138 lines
2.9 KiB
Go
138 lines
2.9 KiB
Go
package frame
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func availableV4LDeviceIndices(t *testing.T) []int {
|
|
t.Helper()
|
|
out, err := exec.Command("ls", "/dev/video*").Output()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var indices []int
|
|
for _, entry := range strings.Split(strings.TrimSpace(string(out)), "\n") {
|
|
entry = strings.TrimSpace(entry)
|
|
if entry == "" {
|
|
continue
|
|
}
|
|
var idx int
|
|
if n, err := fmt.Sscanf(entry, "/dev/video%d", &idx); err == nil && n == 1 {
|
|
indices = append(indices, idx)
|
|
}
|
|
}
|
|
return indices
|
|
}
|
|
|
|
func TestScan_Integration(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping camera scan in short mode")
|
|
}
|
|
available := availableV4LDeviceIndices(t)
|
|
|
|
indices, err := Scan(0, 15)
|
|
if err != nil {
|
|
if len(available) == 0 {
|
|
t.Skip("no /dev/video* devices found; skipping")
|
|
}
|
|
t.Fatalf("Scan returned error but V4L devices exist: %v", err)
|
|
}
|
|
|
|
t.Logf("Scan found %d device(s): %v", len(indices), indices)
|
|
|
|
seen := make(map[int]bool)
|
|
for _, idx := range indices {
|
|
if seen[idx] {
|
|
t.Errorf("duplicate index %d in Scan result", idx)
|
|
}
|
|
seen[idx] = true
|
|
}
|
|
}
|
|
|
|
func TestScan_EmptyRange(t *testing.T) {
|
|
_, err := Scan(5, 3)
|
|
if err == nil {
|
|
t.Error("expected error for empty range [5,3]")
|
|
}
|
|
}
|
|
|
|
func TestScan_SingleValue(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping camera scan in short mode")
|
|
}
|
|
indices, err := Scan(9999, 9999)
|
|
if err == nil {
|
|
t.Log("unexpectedly found a camera at index 9999")
|
|
return
|
|
}
|
|
if len(indices) != 0 {
|
|
t.Errorf("expected empty indices for high range, got %v", indices)
|
|
}
|
|
}
|
|
|
|
func TestScanSkip_EmptyRange(t *testing.T) {
|
|
_, err := ScanSkip(5, 3, 1)
|
|
if err == nil {
|
|
t.Error("expected error for empty range [5,3]")
|
|
}
|
|
}
|
|
|
|
func TestIsCaptureDevice(t *testing.T) {
|
|
if isCaptureDevice(-1) {
|
|
t.Error("expected false for negative index")
|
|
}
|
|
}
|
|
|
|
func TestParseVideoDevice(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
want int
|
|
ok bool
|
|
}{
|
|
{"/dev/video0", 0, true},
|
|
{"/dev/video42", 42, true},
|
|
{"/dev/video", 0, false},
|
|
{"/dev/video-1", -1, true},
|
|
{"/dev/videoabc", 0, false},
|
|
{"video0", 0, false},
|
|
{"", 0, false},
|
|
}
|
|
for _, tt := range tests {
|
|
var idx int
|
|
n, err := fmt.Sscanf(tt.input, "/dev/video%d", &idx)
|
|
got := err == nil && n == 1
|
|
if got != tt.ok {
|
|
t.Errorf("parse(%q): got ok=%v, want %v", tt.input, got, tt.ok)
|
|
}
|
|
if got && idx != tt.want {
|
|
t.Errorf("parse(%q): got %d, want %d", tt.input, idx, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAvailableDevices(t *testing.T) {
|
|
indices := availableV4LDeviceIndices(t)
|
|
t.Logf("Found %d V4L devices: %v", len(indices), indices)
|
|
|
|
for _, idx := range indices {
|
|
if idx < 0 {
|
|
t.Errorf("negative device index: %d", idx)
|
|
}
|
|
if _, err := strconv.Atoi(fmt.Sprintf("%d", idx)); err != nil {
|
|
t.Errorf("invalid device index format: %d", idx)
|
|
}
|
|
}
|
|
|
|
seen := make(map[int]bool)
|
|
for _, idx := range indices {
|
|
if seen[idx] {
|
|
t.Errorf("duplicate index %d", idx)
|
|
}
|
|
seen[idx] = true
|
|
}
|
|
}
|