Files
outline/server/models/Group.test.ts
T
Tom Moor 281b778b2d fix: Suspended users should not be included in cached member count (#12197)
* fix: Suspended users should not be included in cached member count for groups

* fix: Defer CounterCache hook registration until model is initialized

The previous test-only no-op hid a timing bug where setImmediate could
fire before the Sequelize instance had registered the related model,
causing "Model not initialized" failures. Poll until the model is
ready, and unref the pending immediate so it does not keep the event
loop alive in environments where the database is never initialized.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* perf: Reduce overhead of group member count invalidation

Select only the groupId column with raw queries and de-duplicate before
issuing Redis deletes, avoiding loading full GroupUser rows into memory
when a user belongs to many groups.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* chore: unref Redis healthcheck interval

Don't keep the Node event loop alive solely for the periodic ping; the
event loop should drain on its own when the application is shutting
down or a Jest worker is finishing.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor: Centralize counter cache key in RedisPrefixHelper

Avoid duplicating the "count:<Model>:<relation>:<id>" string between
the CounterCache decorator and the User suspension hook by routing
both through a single getCounterCacheKey helper.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix: Walk to parent transaction when scheduling cache invalidation

Nested savepoints commit independently of their outer transaction, so
afterCommit callbacks attached to the inner transaction may run after
the outer rolls back, or never run at all. Match the pattern used in
Collection, Event, and base/Model and walk to the parent transaction
so the cache invalidation fires after the real outer commit.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-29 11:24:44 -04:00

89 lines
2.7 KiB
TypeScript

import { buildGroup, buildGroupUser, buildUser } from "@server/test/factories";
describe("Group", () => {
describe("memberCount", () => {
it("returns 0 for a group with no members", async () => {
const group = await buildGroup();
expect(await group.memberCount).toEqual(0);
});
it("counts active members", async () => {
const group = await buildGroup();
await buildGroupUser({ groupId: group.id, teamId: group.teamId });
await buildGroupUser({ groupId: group.id, teamId: group.teamId });
expect(await group.memberCount).toEqual(2);
});
it("excludes suspended members", async () => {
const group = await buildGroup();
await buildGroupUser({ groupId: group.id, teamId: group.teamId });
const suspendedUser = await buildUser({
teamId: group.teamId,
suspendedAt: new Date(),
});
await buildGroupUser({
groupId: group.id,
teamId: group.teamId,
userId: suspendedUser.id,
});
expect(await group.memberCount).toEqual(1);
});
it("excludes soft-deleted members", async () => {
const group = await buildGroup();
await buildGroupUser({ groupId: group.id, teamId: group.teamId });
const deletedUser = await buildUser({ teamId: group.teamId });
await buildGroupUser({
groupId: group.id,
teamId: group.teamId,
userId: deletedUser.id,
});
await deletedUser.destroy();
expect(await group.memberCount).toEqual(1);
});
it("invalidates the cached count when a member is suspended", async () => {
const group = await buildGroup();
const groupUser = await buildGroupUser({
groupId: group.id,
teamId: group.teamId,
});
await buildGroupUser({ groupId: group.id, teamId: group.teamId });
// Prime the cache.
expect(await group.memberCount).toEqual(2);
const user = (await groupUser.$get("user"))!;
await user.update({ suspendedAt: new Date() });
expect(await group.memberCount).toEqual(1);
});
it("invalidates the cached count when a suspended member is restored", async () => {
const group = await buildGroup();
const suspendedUser = await buildUser({
teamId: group.teamId,
suspendedAt: new Date(),
});
await buildGroupUser({
groupId: group.id,
teamId: group.teamId,
userId: suspendedUser.id,
});
await buildGroupUser({ groupId: group.id, teamId: group.teamId });
// Prime the cache (suspended user is excluded).
expect(await group.memberCount).toEqual(1);
await suspendedUser.update({ suspendedAt: null });
expect(await group.memberCount).toEqual(2);
});
});
});