| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/boot" |
| 8 | "reasonix/internal/control" |
| 9 | "reasonix/internal/extension" |
| 10 | "reasonix/internal/skill/skillwatch" |
| 11 | ) |
| 12 | |
| 13 | func TestRuntimeDoctorEmptyApp(t *testing.T) { |
| 14 | var a *App |
| 15 | report := a.RuntimeDoctor() |
| 16 | if report.Text == "" { |
| 17 | t.Fatal("expected doctor text") |
| 18 | } |
| 19 | // Nil app still returns process-wide metrics/recoverability. |
| 20 | if !report.AllowResume { |
| 21 | t.Fatal("empty process should allow resume") |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestRuntimeDoctorIncludesSkillWatchResources(t *testing.T) { |
| 26 | ctrl := &control.Controller{} |
| 27 | watch := skillwatch.NewService(skillwatch.Options{}) |
| 28 | t.Cleanup(func() { _ = watch.Close() }) |
| 29 | tab := &WorkspaceTab{ID: "tab-watch", Ctrl: ctrl} |
| 30 | a := &App{tabs: map[string]*WorkspaceTab{tab.ID: tab}, activeTabID: tab.ID} |
| 31 | a.setTabLastBuildResult(tab, &boot.BuildResult{Controller: ctrl, Owner: extension.NewRuntimeOwner(), SkillWatchService: watch}) |
| 32 | report := a.RuntimeDoctor() |
| 33 | if report.SkillWatch == nil { |
| 34 | t.Fatal("skill watch diagnostics missing") |
| 35 | } |
| 36 | if report.SkillWatch.PhysicalWatches != 0 || report.SkillWatch.LogicalSubscriptions != 0 { |
| 37 | t.Fatalf("unexpected idle skill watch counters: %+v", report.SkillWatch) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestRuntimeDoctorConcurrentWithBuildResultUpdate(t *testing.T) { |
| 42 | ctrl := &control.Controller{} |
| 43 | tab := &WorkspaceTab{ID: "tab-1", Ctrl: ctrl} |
| 44 | a := &App{ |
| 45 | tabs: map[string]*WorkspaceTab{tab.ID: tab}, |
| 46 | activeTabID: tab.ID, |
| 47 | } |
| 48 | first := &boot.BuildResult{Controller: ctrl, Owner: extension.NewRuntimeOwner()} |
| 49 | second := &boot.BuildResult{Controller: ctrl, Owner: extension.NewRuntimeOwner()} |
| 50 | a.setTabLastBuildResult(tab, first) |
| 51 | |
| 52 | start := make(chan struct{}) |
| 53 | var wg sync.WaitGroup |
| 54 | wg.Go(func() { |
| 55 | <-start |
| 56 | for range 100 { |
| 57 | a.setTabLastBuildResult(tab, second) |
| 58 | a.setTabLastBuildResult(tab, first) |
| 59 | } |
| 60 | }) |
| 61 | close(start) |
| 62 | for range 100 { |
| 63 | report := a.RuntimeDoctor() |
| 64 | if report.Text == "" { |
| 65 | t.Fatal("expected doctor text") |
| 66 | } |
| 67 | } |
| 68 | wg.Wait() |
| 69 | } |
| 70 | |
| 71 | func TestTabBuildResultMustMatchCurrentController(t *testing.T) { |
| 72 | current := &control.Controller{} |
| 73 | stale := &control.Controller{} |
| 74 | tab := &WorkspaceTab{ID: "tab-1", Ctrl: current} |
| 75 | a := &App{ |
| 76 | tabs: map[string]*WorkspaceTab{tab.ID: tab}, |
| 77 | activeTabID: tab.ID, |
| 78 | } |
| 79 | staleResult := &boot.BuildResult{Controller: stale, Owner: extension.NewRuntimeOwner()} |
| 80 | a.setTabLastBuildResult(tab, staleResult) |
| 81 | if got := a.tabBuildResultForController(tab, current); got != nil { |
| 82 | t.Fatal("stale build result was reused for a replacement controller") |
| 83 | } |
| 84 | |
| 85 | currentResult := &boot.BuildResult{Controller: current, Owner: extension.NewRuntimeOwner()} |
| 86 | a.setTabLastBuildResult(tab, currentResult) |
| 87 | if got := a.tabBuildResultForController(tab, current); got != currentResult { |
| 88 | t.Fatal("current controller build result was not reused") |
| 89 | } |
| 90 | } |
| 91 |