fix im p0 backend issues

This commit is contained in:
richjohnrambo
2026-09-07 15:12:14 +08:00
parent b92154bece
commit 7e8fc8237b
7 changed files with 371 additions and 13 deletions
+28 -4
View File
@@ -4870,6 +4870,17 @@ func TestServeIMFavoriteBatchCreateCountsExisting(t *testing.T) {
}
}
func TestFavoriteItemIdempotencyKeyIncludesMessageIdentity(t *testing.T) {
first := favoriteItemIdempotencyKey("same-key", "grpAlpha", 1)
second := favoriteItemIdempotencyKey("same-key", "grpAlpha", 2)
if first == "" || second == "" || first == second {
t.Fatalf("favorite item idempotency key must be stable per source message: %q %q", first, second)
}
if favoriteItemIdempotencyKey("", "grpAlpha", 1) != "" {
t.Fatal("empty request idempotency key should stay empty")
}
}
func TestServeIMFavoriteBatchCreateRejectsUnreadableSource(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
@@ -5694,10 +5705,10 @@ func TestServeIMTopicReadActionsNormalizeP2PUserTopic(t *testing.T) {
oldStore := store.Store
mockStore := mock_store.NewMockPersistentStorageInterface(ctrl)
tokenAuth := mock_auth.NewMockAuthHandler(ctrl)
mockStore.EXPECT().GetLogicalAuthHandler("token").Return(tokenAuth).Times(2)
tokenAuth.EXPECT().IsInitialized().Return(true).Times(2)
mockStore.EXPECT().GetLogicalAuthHandler("token").Return(tokenAuth).Times(3)
tokenAuth.EXPECT().IsInitialized().Return(true).Times(3)
tokenAuth.EXPECT().Authenticate(auth.AuthContext{}, []byte("token-1")).
Return(&auth.Rec{TenantID: 7, Uid: actor, AuthLevel: auth.LevelAuth}, nil, nil).Times(2)
Return(&auth.Rec{TenantID: 7, Uid: actor, AuthLevel: auth.LevelAuth}, nil, nil).Times(3)
store.Store = mockStore
oldSubs := store.Subs
oldMessages := store.Messages
@@ -5717,10 +5728,11 @@ func TestServeIMTopicReadActionsNormalizeP2PUserTopic(t *testing.T) {
Topic: normalizedTopic,
ModeWant: types.ModeCPublic,
ModeGiven: types.ModeCPublic,
}, nil).Times(2)
}, nil).Times(3)
textMsg := types.Message{TenantID: 7, SeqId: 9, Topic: normalizedTopic, From: actor.String(), Head: types.KVMap{"type": "text"}, Content: map[string]any{"text": "好的"}}
imageMsg := types.Message{TenantID: 7, SeqId: 10, Topic: normalizedTopic, From: actor.String(), Head: types.KVMap{"type": "image"}, Content: map[string]any{"file_id": "file-1", "mime": "image/png"}}
messages.EXPECT().Search(types.TenantID(7), normalizedTopic, actor, "好的", "text", "", 50).Return([]types.Message{textMsg}, "", nil)
messages.EXPECT().GetAll(types.TenantID(7), normalizedTopic, actor, gomock.Any()).Return([]types.Message{textMsg}, nil)
messages.EXPECT().Search(types.TenantID(7), normalizedTopic, actor, "", "image", "", 50).Return([]types.Message{imageMsg}, "", nil)
searchReq := httptest.NewRequest(http.MethodGet, "/v1/im/topics/"+peer.UserId()+"/messages/search?q=%E5%A5%BD%E7%9A%84&type=text", nil)
@@ -5731,6 +5743,18 @@ func TestServeIMTopicReadActionsNormalizeP2PUserTopic(t *testing.T) {
t.Fatalf("search status: got %d body=%s", searchRec.Code, searchRec.Body.String())
}
contextReq := httptest.NewRequest(http.MethodGet, "/v1/im/topics/"+peer.UserId()+"/messages/9/context", nil)
contextReq.Header.Set("Authorization", "Bearer token-1")
contextRec := httptest.NewRecorder()
serveIMTopicMessageAction(contextRec, contextReq)
if contextRec.Code != http.StatusOK {
t.Fatalf("context status: got %d body=%s", contextRec.Code, contextRec.Body.String())
}
contextData := decodeIMHTTPResponse(t, contextRec).Data.(map[string]any)
if contextData["topic"] != normalizedTopic {
t.Fatalf("context topic was not normalized: %#v", contextData)
}
mediaReq := httptest.NewRequest(http.MethodGet, "/v1/im/topics/"+peer.UserId()+"/media?type=image", nil)
mediaReq.Header.Set("Authorization", "Bearer token-1")
mediaRec := httptest.NewRecorder()