| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "testing" |
| 11 | |
| 12 | "reasonix/internal/boot" |
| 13 | "reasonix/internal/config" |
| 14 | "reasonix/internal/control" |
| 15 | ) |
| 16 | |
| 17 | func TestModelSettingsApplyFailureAndIdempotentReceipt(t *testing.T) { |
| 18 | old := control.New(control.Options{ModelRef: "p/m", ModelSettingsSourceRevision: "old"}) |
| 19 | bc := NewBroadcaster() |
| 20 | s := New(old, bc, config.ServeConfig{AuthMode: "none"}) |
| 21 | defer s.Close() |
| 22 | builds, fail := 0, true |
| 23 | s.buildControllerWithOptions = func(_ context.Context, ref string, opts boot.Options) (*control.Controller, error) { |
| 24 | builds++ |
| 25 | if fail { |
| 26 | return nil, fmt.Errorf("injected build failure") |
| 27 | } |
| 28 | return control.New(control.Options{ModelRef: ref, Sink: opts.Sink, ModelSettingsSourceRevision: opts.ModelSettings.Revision}), nil |
| 29 | } |
| 30 | request := map[string]any{"version": 1, "ref": "p/m", "settings": config.ModelRuntimeSettings{Revision: "new", Providers: []config.ProviderEntry{{Name: "p", Kind: "openai", BaseURL: "http://localhost", Model: "m"}}, Credentials: map[string]string{"p": "virtual"}}} |
| 31 | body, _ := json.Marshal(request) |
| 32 | apply := func() *httptest.ResponseRecorder { |
| 33 | r := httptest.NewRequest(http.MethodPost, "/model-settings", bytes.NewReader(body)) |
| 34 | w := httptest.NewRecorder() |
| 35 | s.applyModelSettings(w, r) |
| 36 | return w |
| 37 | } |
| 38 | if response := apply(); response.Code != http.StatusInternalServerError || s.ctl() != old || s.managedModels != nil { |
| 39 | t.Fatalf("failed apply changed old runtime: %d", response.Code) |
| 40 | } |
| 41 | fail = false |
| 42 | if response := apply(); response.Code != http.StatusOK { |
| 43 | t.Fatalf("apply: %d %s", response.Code, response.Body) |
| 44 | } |
| 45 | current := s.ctl() |
| 46 | if current == old { |
| 47 | t.Fatal("snapshot not applied") |
| 48 | } |
| 49 | if response := apply(); response.Code != http.StatusOK || s.ctl() != current || builds != 2 { |
| 50 | t.Fatalf("duplicate request rebuilt runtime: %d builds=%d", response.Code, builds) |
| 51 | } |
| 52 | r := httptest.NewRequest(http.MethodPost, "/submit", nil) |
| 53 | r.Header.Set(expectedModelSettingsHeader, "old") |
| 54 | w := httptest.NewRecorder() |
| 55 | if s.validateExpectedSessionLocked(w, r) || w.Code != http.StatusConflict { |
| 56 | t.Fatal("stale model generation admitted") |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestModelSettingsStatusRetainsDetachedOwnership(t *testing.T) { |
| 61 | current := control.New(control.Options{ModelRef: "p/m", ModelSettingsSourceRevision: "new"}) |
| 62 | old := control.New(control.Options{ModelRef: "p/m", ModelSettingsSourceRevision: "old"}) |
| 63 | defer current.Close() |
| 64 | defer old.Close() |
| 65 | s := &Server{ctrl: current, detached: map[string]*detachedSession{"detached": {ctrl: old}}} |
| 66 | status := s.modelSettingsStatusLocked() |
| 67 | if status.Revision != "new" || len(status.OwnedRevisions) != 2 || status.UnversionedOwners { |
| 68 | t.Fatalf("ownership: %+v", status) |
| 69 | } |
| 70 | } |
| 71 |