mirror of
https://github.com/Yuyi-Oak/BlueArchiveToolkit.git
synced 2026-09-18 12:45:17 +08:00
feat(glossary): 实现 Rust Glossary V1
This commit is contained in:
@@ -823,6 +823,179 @@ func TestLocalizedRollbackSendsExpectedRelease(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlossaryTypedContract(t *testing.T) {
|
||||
limit := uint64(20)
|
||||
client := newTestClient(t, func(t *testing.T, req testRequest) testResponse {
|
||||
switch req.Method {
|
||||
case "translation.glossary.summary":
|
||||
var params GlossarySummaryParams
|
||||
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
|
||||
t.Fatalf("decode summary params: %v", err)
|
||||
}
|
||||
if params.GlossaryPath != "/var/lib/bat/glossary.sqlite" {
|
||||
t.Fatalf("summary params=%#v", params)
|
||||
}
|
||||
return testResponse{Result: testEnvelope{
|
||||
OK: true, Status: "ok", RequestID: "req-glossary-summary",
|
||||
Data: map[string]any{
|
||||
"available": true,
|
||||
"path": "/var/lib/bat/glossary.sqlite",
|
||||
"schema_version": 1,
|
||||
"summary": map[string]any{
|
||||
"schema_version": 1,
|
||||
"term_count": 2,
|
||||
"approved_count": 1,
|
||||
"draft_count": 1,
|
||||
},
|
||||
},
|
||||
}}
|
||||
case "translation.glossary.query":
|
||||
var params GlossaryQueryParams
|
||||
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
|
||||
t.Fatalf("decode query params: %v", err)
|
||||
}
|
||||
if params.SourceText != "Sensei" ||
|
||||
params.GlossaryPath != "/var/lib/bat/glossary.sqlite" ||
|
||||
params.Limit == nil || *params.Limit != limit {
|
||||
t.Fatalf("query params=%#v", params)
|
||||
}
|
||||
return testResponse{Result: testEnvelope{
|
||||
OK: true, Status: "ok", RequestID: "req-glossary-query",
|
||||
Data: map[string]any{
|
||||
"available": true,
|
||||
"path": "/var/lib/bat/glossary.sqlite",
|
||||
"terms": []any{map[string]any{
|
||||
"term_id": "term-sensei",
|
||||
"source_term": "Sensei",
|
||||
"recommended_translation": "老师",
|
||||
"priority": 10,
|
||||
"review_status": "approved",
|
||||
"source": map[string]any{
|
||||
"source_kind": "manual",
|
||||
"observed_unix_seconds": 100,
|
||||
},
|
||||
}},
|
||||
},
|
||||
}}
|
||||
case "translation.glossary.diagnose":
|
||||
var params GlossaryDiagnoseParams
|
||||
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
|
||||
t.Fatalf("decode diagnose params: %v", err)
|
||||
}
|
||||
if params.SourceText != "Sensei" || params.Context["destination"] != "Bundle/dialogue.bundle" {
|
||||
t.Fatalf("diagnose params=%#v", params)
|
||||
}
|
||||
return testResponse{Result: testEnvelope{
|
||||
OK: true, Status: "ok", RequestID: "req-glossary-diagnose",
|
||||
Data: map[string]any{
|
||||
"available": true,
|
||||
"path": "/var/lib/bat/glossary.sqlite",
|
||||
"source_text": "Sensei",
|
||||
"evaluation": map[string]any{
|
||||
"constraints": []any{map[string]any{
|
||||
"term_id": "term-sensei",
|
||||
"matched_source": "Sensei",
|
||||
"recommended_translation": "老师",
|
||||
}},
|
||||
"diagnostics": []any{},
|
||||
"blocked": false,
|
||||
},
|
||||
},
|
||||
}}
|
||||
case "translation.glossary.add":
|
||||
var params GlossaryTermMutationParams
|
||||
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
|
||||
t.Fatalf("decode add params: %v", err)
|
||||
}
|
||||
if params.TermID != "term-sensei" || params.Source.SourceKind != "manual" ||
|
||||
params.Source.ObservedUnixSeconds != 100 {
|
||||
t.Fatalf("add params=%#v", params)
|
||||
}
|
||||
return testResponse{Result: testEnvelope{
|
||||
OK: true, Status: "ok", RequestID: "req-glossary-add",
|
||||
Data: map[string]any{
|
||||
"available": true,
|
||||
"path": "/var/lib/bat/glossary.sqlite",
|
||||
"term": map[string]any{
|
||||
"term_id": "term-sensei",
|
||||
"source_term": "Sensei",
|
||||
"recommended_translation": "老师",
|
||||
"review_status": "draft",
|
||||
"source": map[string]any{
|
||||
"source_kind": "manual",
|
||||
"observed_unix_seconds": 100,
|
||||
},
|
||||
},
|
||||
},
|
||||
}}
|
||||
case "translation.glossary.approve":
|
||||
var params GlossaryReviewParams
|
||||
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
|
||||
t.Fatalf("decode approve params: %v", err)
|
||||
}
|
||||
if params.TermID != "term-sensei" || params.Reviewer != "reviewer" {
|
||||
t.Fatalf("approve params=%#v", params)
|
||||
}
|
||||
return testResponse{Result: testEnvelope{
|
||||
OK: true, Status: "ok", RequestID: "req-glossary-approve",
|
||||
Data: map[string]any{
|
||||
"available": true,
|
||||
"path": "/var/lib/bat/glossary.sqlite",
|
||||
"term": map[string]any{"term_id": "term-sensei", "review_status": "approved"},
|
||||
},
|
||||
}}
|
||||
default:
|
||||
t.Fatalf("unexpected method %q", req.Method)
|
||||
return testResponse{}
|
||||
}
|
||||
})
|
||||
|
||||
summary, err := client.GlossarySummary(context.Background(), GlossarySummaryParams{
|
||||
GlossaryPath: "/var/lib/bat/glossary.sqlite",
|
||||
})
|
||||
if err != nil || summary.Summary == nil || summary.Summary.ApprovedCount != 1 {
|
||||
t.Fatalf("summary=%#v err=%v", summary, err)
|
||||
}
|
||||
query, err := client.GlossaryQuery(context.Background(), GlossaryQueryParams{
|
||||
GlossaryPath: "/var/lib/bat/glossary.sqlite",
|
||||
SourceText: "Sensei",
|
||||
Limit: &limit,
|
||||
})
|
||||
if err != nil || len(query.Terms) != 1 || query.Terms[0].ReviewStatus != GlossaryStatusApproved {
|
||||
t.Fatalf("query=%#v err=%v", query, err)
|
||||
}
|
||||
diagnose, err := client.GlossaryDiagnose(context.Background(), GlossaryDiagnoseParams{
|
||||
GlossaryPath: "/var/lib/bat/glossary.sqlite",
|
||||
SourceText: "Sensei",
|
||||
Context: TranslationMemoryContext{"destination": "Bundle/dialogue.bundle"},
|
||||
})
|
||||
if err != nil || !json.Valid(diagnose.Evaluation) {
|
||||
t.Fatalf("diagnose=%#v err=%v", diagnose, err)
|
||||
}
|
||||
add, err := client.GlossaryAdd(context.Background(), GlossaryTermMutationParams{
|
||||
GlossaryPath: "/var/lib/bat/glossary.sqlite",
|
||||
TermID: "term-sensei",
|
||||
SourceTerm: "Sensei",
|
||||
RecommendedTranslation: "老师",
|
||||
ReviewStatus: "draft",
|
||||
Source: GlossarySourceRecord{
|
||||
SourceKind: "manual",
|
||||
ObservedUnixSeconds: 100,
|
||||
},
|
||||
})
|
||||
if err != nil || add.Term.TermID != "term-sensei" {
|
||||
t.Fatalf("add=%#v err=%v", add, err)
|
||||
}
|
||||
approved, err := client.GlossaryApprove(context.Background(), GlossaryReviewParams{
|
||||
GlossaryPath: "/var/lib/bat/glossary.sqlite",
|
||||
TermID: "term-sensei",
|
||||
Reviewer: "reviewer",
|
||||
})
|
||||
if err != nil || approved.Term.ReviewStatus != GlossaryStatusApproved {
|
||||
t.Fatalf("approve=%#v err=%v", approved, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplicationErrorReturnsAPIError(t *testing.T) {
|
||||
client := newTestClient(t, func(t *testing.T, req testRequest) testResponse {
|
||||
if req.Method != "task.status" {
|
||||
|
||||
Reference in New Issue
Block a user