Files
xk-hy-forward-go/internal/forward/testweb/prescription_index_test.go
2026-07-21 16:35:46 +08:00

144 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package testweb
import (
"os"
"path/filepath"
"strings"
"testing"
)
const sampleRecipeLog = `=== META ===
Time: 2026-07-13T08:00:00+08:00
ClientIP: 127.0.0.1
Method: POST
Path: /province/supervise/data
Kind: supervise
DurationMs: 250
Status: 200
=== 1. 入站请求transit → forward===
URL: POST 127.0.0.1:16001/province/supervise/data
Headers:
Content-Type: application/json
X-Service-Method: uploadRecipeIndicators
X-Forward-Token: secret
Body:
{"encrypted":"x"}
=== 2. 出站转发forward → 政务云)===
TargetURL: https://59.202.52.129:28212/province/supervise/data
Method: POST
Headers:
Content-Type: application/json
X-Service-Method: uploadRecipeIndicators
Body:
{"encrypted":"x"}
=== 3. 回复(政务云 → forward → transit===
UpstreamStatus: 200
UpstreamHeaders:
Content-Type: application/json
X-Ca-Request-Id: trace-recipe-1
Body:
{"msgCode":0,"msg":"ok"}
BodyJSON:
{
"msgCode": 0,
"msg": "ok"
}
`
func verificationLogBytes() []byte {
return []byte(strings.ReplaceAll(sampleRecipeLog, "uploadRecipeIndicators", "uploadRecipeVerificationIndicators"))
}
func TestClassifyPrescriptionLog_recipe(t *testing.T) {
rec, ok := classifyPrescriptionLog("2026-07-13 08/20260713080000_supervise_abcd.log", []byte(sampleRecipeLog))
if !ok {
t.Fatal("expected classified")
}
if rec.BizType != "recipe" || rec.Channel != "supervise" {
t.Fatalf("rec=%+v", rec)
}
if !rec.OK || rec.MsgCode != 0 || rec.TraceID != "trace-recipe-1" {
t.Fatalf("response fields=%+v", rec)
}
}
func TestClassifyPrescriptionLog_verification(t *testing.T) {
rec, ok := classifyPrescriptionLog("2026-07-13 08/20260713080000_supervise_abcd.log", verificationLogBytes())
if !ok || rec.BizType != "verification" {
t.Fatalf("rec=%+v ok=%v", rec, ok)
}
}
func TestClassifyPrescriptionLog_pdf(t *testing.T) {
rec, ok := classifyPrescriptionLog("2026-07-13 08/20260713080000_file_abcd.log", []byte(sampleLog))
if !ok || rec.BizType != "pdf" {
t.Fatalf("rec=%+v", rec)
}
if rec.FileID != "id1" || !rec.OK {
t.Fatalf("pdf response=%+v", rec)
}
}
func TestRebuildPrescriptionIndex(t *testing.T) {
tmp := t.TempDir()
hour := filepath.Join(tmp, "2026-07-13 08")
if err := os.MkdirAll(hour, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(hour, "20260713080000_file_abcd.log"), []byte(sampleLog), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(hour, "20260713080100_supervise_abcd.log"), []byte(sampleRecipeLog), 0o644); err != nil {
t.Fatal(err)
}
n, err := RebuildPrescriptionIndex(tmp)
if err != nil || n != 2 {
t.Fatalf("count=%d err=%v", n, err)
}
pdfList, err := ListPrescriptionRecords(tmp, "pdf", 10)
if err != nil || len(pdfList) != 1 {
t.Fatalf("pdf=%v err=%v", pdfList, err)
}
recipeList, err := ListPrescriptionRecords(tmp, "recipe", 10)
if err != nil || len(recipeList) != 1 {
t.Fatalf("recipe=%v err=%v", recipeList, err)
}
}
func TestListSuperviseLogs_and_resolve(t *testing.T) {
tmp := t.TempDir()
hour := filepath.Join(tmp, "2026-07-13 08")
if err := os.MkdirAll(hour, 0o755); err != nil {
t.Fatal(err)
}
name := "20260713080100_supervise_abcd.log"
if err := os.WriteFile(filepath.Join(hour, name), []byte(sampleRecipeLog), 0o644); err != nil {
t.Fatal(err)
}
list, err := ListSuperviseLogs(tmp)
if err != nil || len(list) != 1 || list[0].Kind != "supervise" {
t.Fatalf("list=%v err=%v", list, err)
}
rel := list[0].Rel
if _, err := resolveLogRelPath(tmp, rel); err != nil {
t.Fatal(err)
}
p, err := ParseAPILogFile(tmp, rel)
if err != nil {
t.Fatal(err)
}
if p.Inbound.Headers["X-Service-Method"] != MethodRecipe {
t.Fatalf("method=%q", p.Inbound.Headers["X-Service-Method"])
}
}