chore: refresh gramsrv public release

This commit is contained in:
A 2026-06-30 14:37:43 +08:00
parent 75cebe8dbf
commit 70b6820474
1274 changed files with 378751 additions and 59919 deletions

View file

@ -84,3 +84,115 @@ func TestAuthSignUpWritesOfficialLoginMessagePostgres(t *testing.T) {
t.Fatalf("users = %+v, want official support user", list.Users)
}
}
func TestAuthSignInOfficialLoginMessagePreservesReadWatermarkPostgres(t *testing.T) {
pool := testPool(t)
ctx := context.Background()
phone := fmt.Sprintf("1555%d32", time.Now().UnixNano())
t.Cleanup(func() {
_, _ = pool.Exec(ctx, "DELETE FROM users WHERE phone = $1", phone)
})
users := NewUserStore(pool)
dialogs := NewDialogStore(pool)
messages := NewMessageStore(pool)
svc := appauth.NewService(
users,
NewAuthorizationStore(pool),
memory.NewCodeStore(),
nil,
nil,
"12345",
appauth.WithLoginMessages(messages, dialogs),
)
var authKeyID [8]byte
var authKeyBody [256]byte
if _, err := rand.Read(authKeyID[:]); err != nil {
t.Fatal(err)
}
if _, err := rand.Read(authKeyBody[:]); err != nil {
t.Fatal(err)
}
if err := NewAuthKeyStore(pool).Save(ctx, store.AuthKeyData{ID: authKeyID, Value: authKeyBody}); err != nil {
t.Fatalf("save auth key: %v", err)
}
t.Cleanup(func() {
_, _ = pool.Exec(ctx, "DELETE FROM auth_keys WHERE auth_key_id = $1", authKeyIDToInt64(authKeyID))
})
hash, err := svc.SendCode(ctx, phone)
if err != nil {
t.Fatalf("SendCode signup: %v", err)
}
u, first, err := svc.SignUp(ctx, domain.Authorization{AuthKeyID: authKeyID}, phone, hash, "PgLogin", "Read")
if err != nil {
t.Fatalf("SignUp: %v", err)
}
peer := domain.Peer{Type: domain.PeerTypeUser, ID: domain.OfficialSystemUserID}
read, err := messages.ReadHistory(ctx, domain.ReadHistoryRequest{
OwnerUserID: u.ID,
Peer: peer,
MaxID: domain.MaxMessageBoxID,
Date: int(time.Now().Unix()),
})
if err != nil {
t.Fatalf("ReadHistory first login message: %v", err)
}
if read.MaxID != first.ID || read.StillUnreadCount != 0 {
t.Fatalf("read first login message = %+v, want max_id %d unread 0", read, first.ID)
}
assertOfficialDialog := func(wantTop, wantRead, wantUnread int) {
t.Helper()
var top, readMax, unread, computed int
if err := pool.QueryRow(ctx, `
WITH target AS (
SELECT top_message_id, read_inbox_max_id, unread_count
FROM dialogs
WHERE user_id = $1 AND peer_type = 'user' AND peer_id = $2
)
SELECT
target.top_message_id,
target.read_inbox_max_id,
target.unread_count,
(
SELECT count(*)::int
FROM message_boxes m
WHERE m.owner_user_id = $1
AND m.peer_type = 'user'
AND m.peer_id = $2
AND NOT m.outgoing
AND NOT m.deleted
AND m.box_id > target.read_inbox_max_id
) AS computed_unread
FROM target`, u.ID, domain.OfficialSystemUserID).Scan(&top, &readMax, &unread, &computed); err != nil {
t.Fatalf("query official dialog state: %v", err)
}
if top != wantTop || readMax != wantRead || unread != wantUnread || computed != wantUnread {
t.Fatalf("official dialog top=%d read=%d unread=%d computed=%d, want top=%d read=%d unread=%d",
top, readMax, unread, computed, wantTop, wantRead, wantUnread)
}
}
hash, err = svc.SendCode(ctx, phone)
if err != nil {
t.Fatalf("SendCode signin second: %v", err)
}
_, second, needSignUp, err := svc.SignIn(ctx, domain.Authorization{AuthKeyID: authKeyID}, phone, hash, "12345")
if err != nil || needSignUp {
t.Fatalf("SignIn second needSignUp=%v err=%v", needSignUp, err)
}
assertOfficialDialog(second.ID, first.ID, 1)
hash, err = svc.SendCode(ctx, phone)
if err != nil {
t.Fatalf("SendCode signin third: %v", err)
}
_, third, needSignUp, err := svc.SignIn(ctx, domain.Authorization{AuthKeyID: authKeyID}, phone, hash, "12345")
if err != nil || needSignUp {
t.Fatalf("SignIn third needSignUp=%v err=%v", needSignUp, err)
}
assertOfficialDialog(third.ID, first.ID, 2)
}