feat(t242): delete unused freight orders

This commit is contained in:
QiuSW
2026-07-29 16:10:45 +08:00
parent c80a5c26ed
commit e5f403ff7d
20 changed files with 915 additions and 27 deletions
@@ -1301,6 +1301,102 @@ func TestFreightDetailCreatesProcurementTaskWithCSRF(t *testing.T) {
}
}
func TestFreightDeleteRequiresConfirmationAndHandlesConflict(t *testing.T) {
service := &fakeFreightService{
fakeService: &fakeService{},
orderDetail: FreightOrderDetail{
Order: FreightOrder{
ID: testTaskID,
ExternalStockID: "12",
SourceCode: "SOURCE-12",
},
},
}
logged := make([]string, 0)
router := newTestRouterWithLogger(
t,
service,
func(value string) {
logged = append(logged, value)
},
)
detail := performRequest(
t,
router,
http.MethodGet,
"/freight/"+testTaskID,
nil,
"",
)
if detail.Code != http.StatusOK ||
!strings.Contains(detail.Body.String(), "删除本地货运单") ||
!strings.Contains(detail.Body.String(), "data-freight-delete-dialog") {
t.Fatalf("delete controls status/body = %d / %s", detail.Code, detail.Body)
}
cookie := csrfCookie(t, detail)
withoutCSRF := performRequest(
t,
router,
http.MethodPost,
"/freight/"+testTaskID+"/delete",
strings.NewReader("confirm_delete=1"),
"application/x-www-form-urlencoded",
)
if withoutCSRF.Code != http.StatusForbidden {
t.Fatalf("delete without CSRF status = %d", withoutCSRF.Code)
}
values := url.Values{
"csrf_token": {cookie.Value},
"confirm_delete": {"1"},
}
request := httptest.NewRequest(
http.MethodPost,
"/freight/"+testTaskID+"/delete",
strings.NewReader(values.Encode()),
)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.AddCookie(cookie)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
if response.Code != http.StatusSeeOther ||
response.Header().Get("Location") != "/freight?notice=deleted" ||
service.deleteInput.OrderID != testTaskID ||
len(logged) != 1 ||
!strings.Contains(logged[0], "freight_order_deleted order_id=") {
t.Fatalf(
"delete response/input/log = %d / %q / %+v / %#v",
response.Code,
response.Header().Get("Location"),
service.deleteInput,
logged,
)
}
service.deleteErr = ErrFreightOrderInUse
request = httptest.NewRequest(
http.MethodPost,
"/freight/"+testTaskID+"/delete",
strings.NewReader(values.Encode()),
)
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.AddCookie(cookie)
response = httptest.NewRecorder()
router.ServeHTTP(response, request)
if response.Code != http.StatusSeeOther ||
response.Header().Get("Location") !=
"/freight/"+testTaskID+"?notice=delete-conflict" ||
len(logged) != 1 {
t.Fatalf(
"conflicting delete status/location/log = %d / %q / %#v",
response.Code,
response.Header().Get("Location"),
logged,
)
}
}
func TestFreightPresentationFormatsMoneyAndImageStates(t *testing.T) {
value := int64(12905)
if got := formatMinorCurrency(&value, "TWD"); got != "TWD 129.05" {
@@ -1348,6 +1444,8 @@ type fakeFreightService struct {
*fakeService
orders []FreightOrder
orderDetail FreightOrderDetail
deleteInput DeleteFreightOrderInput
deleteErr error
sync FreightSync
createInput CreateFreightSyncInput
createResult FreightSync
@@ -1448,6 +1546,14 @@ func (service *fakeFreightService) GetFreightOrder(
return service.orderDetail, service.err
}
func (service *fakeFreightService) DeleteFreightOrder(
_ context.Context,
input DeleteFreightOrderInput,
) error {
service.deleteInput = input
return service.deleteErr
}
func (service *fakeFreightService) GetFreightSync(
context.Context,
string,