| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/config" |
| 10 | "reasonix/internal/control" |
| 11 | "reasonix/internal/event" |
| 12 | ) |
| 13 | |
| 14 | type exactPromptController struct { |
| 15 | control.SessionAPI |
| 16 | sessionID string |
| 17 | calls []control.PromptIdentity |
| 18 | } |
| 19 | |
| 20 | func (c *exactPromptController) RuntimeStateSnapshot() event.RuntimeStateSnapshot { |
| 21 | return event.RuntimeStateSnapshot{SchemaVersion: 1, SessionID: c.sessionID} |
| 22 | } |
| 23 | |
| 24 | func (c *exactPromptController) ResolvePromptExact(identity control.PromptIdentity, _ control.PromptAnswer) error { |
| 25 | c.calls = append(c.calls, identity) |
| 26 | return nil |
| 27 | } |
| 28 | |
| 29 | func TestServeExactPromptChecksSessionBeforeController(t *testing.T) { |
| 30 | bc := NewBroadcaster() |
| 31 | ctrl := &exactPromptController{SessionAPI: control.New(control.Options{Sink: bc}), sessionID: "session-a"} |
| 32 | srv := httptest.NewServer(New(ctrl, bc, config.ServeConfig{}).Handler()) |
| 33 | defer srv.Close() |
| 34 | post := func(sessionID string) int { |
| 35 | resp, err := http.Post(srv.URL+"/resolve-prompt", "application/json", strings.NewReader( |
| 36 | `{"sessionId":"`+sessionID+`","promptId":"p1","turnId":"t1","runtimeEpoch":"r1","kind":"ask","answer":{"questions":[]}}`, |
| 37 | )) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | defer resp.Body.Close() |
| 42 | return resp.StatusCode |
| 43 | } |
| 44 | if got := post("session-old"); got != http.StatusConflict || len(ctrl.calls) != 0 { |
| 45 | t.Fatalf("stale exact prompt status/calls = %d/%d", got, len(ctrl.calls)) |
| 46 | } |
| 47 | if got := post("session-a"); got != http.StatusNoContent || len(ctrl.calls) != 1 || ctrl.calls[0].RuntimeEpoch != "r1" { |
| 48 | t.Fatalf("current exact prompt status/calls = %d/%+v", got, ctrl.calls) |
| 49 | } |
| 50 | } |
| 51 |