2026-07-29 10:58:28 +08:00
|
|
|
package webui
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"cmroubao/backend-api/internal/domain"
|
2026-07-29 12:11:56 +08:00
|
|
|
"cmroubao/backend-api/internal/usecase"
|
2026-07-29 10:58:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestMapFreightPreflightErrorUsesStablePublicErrors(t *testing.T) {
|
|
|
|
|
testCases := []struct {
|
|
|
|
|
err error
|
|
|
|
|
want error
|
|
|
|
|
}{
|
|
|
|
|
{domain.ErrFreightSourceOCRInvalid, ErrOCRServiceInvalid},
|
|
|
|
|
{domain.ErrFreightSourceNotConfigured, ErrERPNotConfigured},
|
2026-07-29 12:11:56 +08:00
|
|
|
{domain.ErrFreightSourceSessionNeeded, ErrERPSessionNeeded},
|
|
|
|
|
{domain.ErrFreightSourceNotFound, ErrERPFreightNotFound},
|
2026-07-29 10:58:28 +08:00
|
|
|
{domain.ErrFreightSourceLoginRejected, ErrERPLoginRejected},
|
|
|
|
|
{domain.ErrFreightSourceProtocol, ErrERPProtocol},
|
|
|
|
|
{domain.ErrFreightSourceUnavailable, ErrERPUnavailable},
|
|
|
|
|
}
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
|
actual := mapFreightPreflightError(testCase.err)
|
|
|
|
|
if !errors.Is(actual, testCase.want) || !errors.Is(actual, testCase.err) {
|
|
|
|
|
t.Fatalf("mapFreightPreflightError(%v) = %v", testCase.err, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-29 11:24:04 +08:00
|
|
|
|
|
|
|
|
func TestMapFreightCreateErrorPreservesPreflightErrors(t *testing.T) {
|
|
|
|
|
for _, testCase := range []struct {
|
|
|
|
|
err error
|
|
|
|
|
want error
|
|
|
|
|
}{
|
|
|
|
|
{domain.ErrFreightSourceOCRInvalid, ErrOCRServiceInvalid},
|
|
|
|
|
{domain.ErrFreightSourceNotConfigured, ErrERPNotConfigured},
|
2026-07-29 12:11:56 +08:00
|
|
|
{domain.ErrFreightSourceSessionNeeded, ErrERPSessionNeeded},
|
|
|
|
|
{domain.ErrFreightSourceNotFound, ErrERPFreightNotFound},
|
2026-07-29 11:24:04 +08:00
|
|
|
{domain.ErrFreightSourceLoginRejected, ErrERPLoginRejected},
|
|
|
|
|
{domain.ErrFreightSourceProtocol, ErrERPProtocol},
|
|
|
|
|
{domain.ErrFreightSourceUnavailable, ErrERPUnavailable},
|
|
|
|
|
} {
|
|
|
|
|
actual := mapFreightCreateError(testCase.err)
|
|
|
|
|
if !errors.Is(actual, testCase.want) || !errors.Is(actual, testCase.err) {
|
|
|
|
|
t.Fatalf("mapFreightCreateError(%v) = %v", testCase.err, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-29 12:11:56 +08:00
|
|
|
|
|
|
|
|
func TestMapFreightCreateErrorUsesStableSyncErrors(t *testing.T) {
|
|
|
|
|
for _, testCase := range []struct {
|
|
|
|
|
code string
|
|
|
|
|
want error
|
|
|
|
|
}{
|
|
|
|
|
{"FREIGHT_SYNC_BUSY", ErrFreightSyncBusy},
|
|
|
|
|
{"FREIGHT_SYNC_TIMEOUT", ErrFreightSyncTimeout},
|
|
|
|
|
} {
|
|
|
|
|
source := &usecase.Error{
|
|
|
|
|
Kind: usecase.ErrorKindUnavailable,
|
|
|
|
|
Code: testCase.code,
|
|
|
|
|
Fields: map[string]string{},
|
|
|
|
|
}
|
|
|
|
|
actual := mapFreightCreateError(source)
|
|
|
|
|
if !errors.Is(actual, testCase.want) || !errors.Is(actual, source) {
|
|
|
|
|
t.Fatalf("mapFreightCreateError(%s) = %v", testCase.code, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|