@@ -5,15 +5,25 @@ import (
55 "io"
66 "net/http"
77 "net/http/httptest"
8+ "path/filepath"
89 "strings"
910 "testing"
1011
1112 "ds2api/internal/auth"
13+ "ds2api/internal/chathistory"
1214 dsclient "ds2api/internal/deepseek/client"
1315)
1416
1517type claudeCurrentInputAuth struct {}
1618
19+ type claudeHistoryConfig struct {
20+ aliases map [string ]string
21+ }
22+
23+ func (m claudeHistoryConfig ) ModelAliases () map [string ]string { return m .aliases }
24+ func (claudeHistoryConfig ) CurrentInputFileEnabled () bool { return false }
25+ func (claudeHistoryConfig ) CurrentInputFileMinChars () int { return 0 }
26+
1727func (claudeCurrentInputAuth ) Determine (* http.Request ) (* auth.RequestAuth , error ) {
1828 return & auth.RequestAuth {
1929 DeepSeekToken : "direct-token" ,
@@ -22,6 +32,50 @@ func (claudeCurrentInputAuth) Determine(*http.Request) (*auth.RequestAuth, error
2232 }, nil
2333}
2434
35+ func TestClaudeDirectRecordsResponseHistory (t * testing.T ) {
36+ ds := & claudeCurrentInputDS {}
37+ historyStore := chathistory .New (filepath .Join (t .TempDir (), "history.json" ))
38+ h := & Handler {
39+ Store : claudeHistoryConfig {aliases : map [string ]string {"claude-sonnet-4-6" : "deepseek-v4-flash" }},
40+ Auth : claudeCurrentInputAuth {},
41+ DS : ds ,
42+ ChatHistory : historyStore ,
43+ }
44+ reqBody := `{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hello from claude"}],"max_tokens":1024}`
45+ req := httptest .NewRequest (http .MethodPost , "/v1/messages" , strings .NewReader (reqBody ))
46+ req .Header .Set ("Content-Type" , "application/json" )
47+ rec := httptest .NewRecorder ()
48+
49+ h .Messages (rec , req )
50+
51+ if rec .Code != http .StatusOK {
52+ t .Fatalf ("expected 200, got %d body=%s" , rec .Code , rec .Body .String ())
53+ }
54+ snapshot , err := historyStore .Snapshot ()
55+ if err != nil {
56+ t .Fatalf ("snapshot history: %v" , err )
57+ }
58+ if len (snapshot .Items ) != 1 {
59+ t .Fatalf ("expected one history item, got %d" , len (snapshot .Items ))
60+ }
61+ item , err := historyStore .Get (snapshot .Items [0 ].ID )
62+ if err != nil {
63+ t .Fatalf ("get history item: %v" , err )
64+ }
65+ if item .Surface != "claude.messages" {
66+ t .Fatalf ("unexpected surface: %q" , item .Surface )
67+ }
68+ if item .Model != "claude-sonnet-4-6" {
69+ t .Fatalf ("unexpected model: %q" , item .Model )
70+ }
71+ if item .UserInput != "hello from claude" {
72+ t .Fatalf ("unexpected user input: %q" , item .UserInput )
73+ }
74+ if item .Content != "ok" {
75+ t .Fatalf ("expected raw upstream content, got %q" , item .Content )
76+ }
77+ }
78+
2579func (claudeCurrentInputAuth ) Release (* auth.RequestAuth ) {}
2680
2781type claudeCurrentInputDS struct {
@@ -53,10 +107,12 @@ func (d *claudeCurrentInputDS) CallCompletion(_ context.Context, _ *auth.Request
53107
54108func TestClaudeDirectAppliesCurrentInputFile (t * testing.T ) {
55109 ds := & claudeCurrentInputDS {}
110+ historyStore := chathistory .New (filepath .Join (t .TempDir (), "history.json" ))
56111 h := & Handler {
57- Store : mockClaudeConfig {aliases : map [string ]string {"claude-sonnet-4-6" : "deepseek-v4-flash" }},
58- Auth : claudeCurrentInputAuth {},
59- DS : ds ,
112+ Store : mockClaudeConfig {aliases : map [string ]string {"claude-sonnet-4-6" : "deepseek-v4-flash" }},
113+ Auth : claudeCurrentInputAuth {},
114+ DS : ds ,
115+ ChatHistory : historyStore ,
60116 }
61117 reqBody := `{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hello from claude"}],"max_tokens":1024}`
62118 req := httptest .NewRequest (http .MethodPost , "/v1/messages" , strings .NewReader (reqBody ))
@@ -82,4 +138,21 @@ func TestClaudeDirectAppliesCurrentInputFile(t *testing.T) {
82138 if ! strings .Contains (prompt , "Continue from the latest state in the attached DS2API_HISTORY.txt context." ) {
83139 t .Fatalf ("expected continuation prompt, got %q" , prompt )
84140 }
141+ snapshot , err := historyStore .Snapshot ()
142+ if err != nil {
143+ t .Fatalf ("snapshot history: %v" , err )
144+ }
145+ if len (snapshot .Items ) != 1 {
146+ t .Fatalf ("expected one history item, got %d" , len (snapshot .Items ))
147+ }
148+ full , err := historyStore .Get (snapshot .Items [0 ].ID )
149+ if err != nil {
150+ t .Fatalf ("get history item: %v" , err )
151+ }
152+ if full .HistoryText != string (ds .uploads [0 ].Data ) {
153+ t .Fatalf ("expected uploaded current input file to be persisted in history text" )
154+ }
155+ if len (full .Messages ) != 1 || ! strings .Contains (full .Messages [0 ].Content , "Continue from the latest state in the attached DS2API_HISTORY.txt context." ) {
156+ t .Fatalf ("expected persisted message to match upstream continuation prompt, got %#v" , full .Messages )
157+ }
85158}
0 commit comments