package webui import ( "context" "reflect" "testing" "time" "bee/audit/internal/app" "bee/audit/internal/platform" "bee/audit/internal/schema" ) func TestIntersectSortedInts(t *testing.T) { got := intersectSortedInts([]int{0, 1, 2, 3, 4, 5, 6, 7}, []int{5, 1, 9}) if want := []int{1, 5}; !reflect.DeepEqual(got, want) { t.Fatalf("intersectSortedInts=%v want %v", got, want) } if got := intersectSortedInts([]int{0, 1}, []int{9}); len(got) != 0 { t.Fatalf("want empty, got %v", got) } } func TestWaitForNvidiaReadyDoesNotTreatLoadedDriverAsEnumeration(t *testing.T) { oldWait, oldPoll := gpuReadyWait, gpuReadyPollInterval oldHealth, oldList := apiRuntimeHealthNow, apiListNvidiaGPUs gpuReadyWait, gpuReadyPollInterval = 5*time.Millisecond, time.Millisecond apiRuntimeHealthNow = func(*app.App) (schema.RuntimeHealth, error) { return schema.RuntimeHealth{DriverReady: true}, nil } apiListNvidiaGPUs = func(*app.App) ([]platform.NvidiaGPU, error) { return nil, nil } t.Cleanup(func() { gpuReadyWait, gpuReadyPollInterval = oldWait, oldPoll apiRuntimeHealthNow, apiListNvidiaGPUs = oldHealth, oldList }) _, gpus, ready := (&handler{opts: HandlerOptions{App: app.New(&platform.System{})}}).waitForNvidiaReady(context.Background()) if ready || len(gpus) != 0 { t.Fatalf("ready=%v gpus=%v; loaded module without enumerated GPUs must not be ready", ready, gpus) } } func TestWaitForNvidiaReadyReturnsFreshEnumeration(t *testing.T) { oldWait, oldPoll := gpuReadyWait, gpuReadyPollInterval oldHealth, oldList := apiRuntimeHealthNow, apiListNvidiaGPUs gpuReadyWait, gpuReadyPollInterval = 20*time.Millisecond, time.Millisecond apiRuntimeHealthNow = func(*app.App) (schema.RuntimeHealth, error) { return schema.RuntimeHealth{DriverReady: true, CUDAReady: true}, nil } calls := 0 apiListNvidiaGPUs = func(*app.App) ([]platform.NvidiaGPU, error) { calls++ if calls < 2 { return nil, nil } return []platform.NvidiaGPU{{Index: 3}}, nil } t.Cleanup(func() { gpuReadyWait, gpuReadyPollInterval = oldWait, oldPoll apiRuntimeHealthNow, apiListNvidiaGPUs = oldHealth, oldList }) health, gpus, ready := (&handler{opts: HandlerOptions{App: app.New(&platform.System{})}}).waitForNvidiaReady(context.Background()) if !ready || !health.CUDAReady || len(gpus) != 1 || gpus[0].Index != 3 { t.Fatalf("ready=%v health=%+v gpus=%v", ready, health, gpus) } } // On a host with no GPU and no TPM the plan is the base checks plus a note // that TPM was skipped, and no GPU tasks are invented. func TestPlanSATRunAllNoAcceleratorNoTPM(t *testing.T) { oldWait := gpuReadyWait gpuReadyWait = 10 * time.Millisecond t.Cleanup(func() { gpuReadyWait = oldWait }) h := &handler{opts: HandlerOptions{App: app.New(&platform.System{})}} specs, notes := h.planSATRunAll(context.Background(), satRunAllRequest{}) var targets []string for _, s := range specs { targets = append(targets, s.target) } want := []string{"cpu", "memory", "storage", "pcie-link"} if !reflect.DeepEqual(targets, want) { t.Fatalf("targets=%v want %v", targets, want) } if len(notes) == 0 { t.Fatalf("expected a note about TPM being skipped") } }