mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-09 18:57:43 +08:00
refactor: replace processed output comparison with baseline-based validation in SSE simulator
This commit is contained in:
@@ -65,18 +65,13 @@ func (h *Handler) captureRawSample(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
processedContentType := strings.TrimSpace(rec.Header().Get("Content-Type"))
|
||||
saved, err := rawsample.Persist(rawsample.PersistOptions{
|
||||
RootDir: config.RawStreamSampleRoot(),
|
||||
SampleID: sampleID,
|
||||
Source: "admin/dev/raw-samples/capture",
|
||||
Request: payload,
|
||||
Capture: captureSummaryFromEntry(captureEntry),
|
||||
UpstreamBody: []byte(captureEntry.ResponseBody),
|
||||
ProcessedBody: rec.Body.Bytes(),
|
||||
ProcessedKind: processedKindFromContentType(processedContentType),
|
||||
ProcessedStatusCode: rec.Code,
|
||||
ProcessedContentType: processedContentType,
|
||||
RootDir: config.RawStreamSampleRoot(),
|
||||
SampleID: sampleID,
|
||||
Source: "admin/dev/raw-samples/capture",
|
||||
Request: payload,
|
||||
Capture: captureSummaryFromEntry(captureEntry),
|
||||
UpstreamBody: []byte(captureEntry.ResponseBody),
|
||||
})
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()})
|
||||
@@ -88,8 +83,6 @@ func (h *Handler) captureRawSample(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Ds2-Sample-Dir", saved.Dir)
|
||||
w.Header().Set("X-Ds2-Sample-Meta", saved.MetaPath)
|
||||
w.Header().Set("X-Ds2-Sample-Upstream", saved.UpstreamPath)
|
||||
w.Header().Set("X-Ds2-Sample-Processed", saved.ProcessedPath)
|
||||
w.Header().Set("X-Ds2-Sample-Output", saved.OutputPath)
|
||||
w.WriteHeader(rec.Code)
|
||||
_, _ = io.Copy(w, bytes.NewReader(rec.Body.Bytes()))
|
||||
}
|
||||
@@ -183,18 +176,6 @@ func captureSummaryFromEntry(entry devcapture.Entry) rawsample.CaptureSummary {
|
||||
}
|
||||
}
|
||||
|
||||
func processedKindFromContentType(contentType string) string {
|
||||
ct := strings.ToLower(strings.TrimSpace(contentType))
|
||||
switch {
|
||||
case strings.Contains(ct, "text/event-stream"):
|
||||
return "stream"
|
||||
case strings.Contains(ct, "application/json"):
|
||||
return "json"
|
||||
default:
|
||||
return "stream"
|
||||
}
|
||||
}
|
||||
|
||||
func copyHeader(dst, src http.Header) {
|
||||
for k, vv := range src {
|
||||
dst.Del(k)
|
||||
|
||||
@@ -56,8 +56,8 @@ func TestCaptureRawSampleWritesPersistentSample(t *testing.T) {
|
||||
if got := rec.Header().Get("X-Ds2-Sample-Id"); got != "my-sample-01" {
|
||||
t.Fatalf("expected sample id header my-sample-01, got %q", got)
|
||||
}
|
||||
if got := rec.Header().Get("X-Ds2-Sample-Output"); got != filepath.Join(os.Getenv("DS2API_RAW_STREAM_SAMPLE_ROOT"), "my-sample-01", "openai.output.txt") {
|
||||
t.Fatalf("unexpected sample output header: %q", got)
|
||||
if got := rec.Header().Get("X-Ds2-Sample-Upstream"); got != filepath.Join(os.Getenv("DS2API_RAW_STREAM_SAMPLE_ROOT"), "my-sample-01", "upstream.stream.sse") {
|
||||
t.Fatalf("unexpected sample upstream header: %q", got)
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), `"content":"hello"`) {
|
||||
t.Fatalf("expected proxied openai output, got %s", rec.Body.String())
|
||||
@@ -85,11 +85,7 @@ func TestCaptureRawSampleWritesPersistentSample(t *testing.T) {
|
||||
if got := int(capture["response_bytes"].(float64)); got == 0 {
|
||||
t.Fatalf("expected capture bytes to be recorded, got %#v", capture)
|
||||
}
|
||||
processed, _ := meta["processed"].(map[string]any)
|
||||
if processed == nil {
|
||||
t.Fatalf("missing processed meta: %#v", meta)
|
||||
}
|
||||
if processed["file"] != "openai.stream.sse" {
|
||||
t.Fatalf("unexpected processed file: %#v", processed["file"])
|
||||
if _, ok := meta["processed"]; ok {
|
||||
t.Fatalf("unexpected processed meta: %#v", meta["processed"])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user