test(ai-roles): cover role-resolution, CASL gate, model override; hide disabled badge
Release-cycle test audit found the role feature's security-critical paths untested. Adds real unit tests (against the actual functions): - resolveRoleForRequest invariants: role comes from chat.roleId not body.roleId (no per-turn swap), lookup scoped to workspace.id, disabled/soft-deleted role -> null, new-chat uses body.roleId, stale chatId falls back. - CASL admin gate: non-admin create/update/delete -> Forbidden and service not called; admin delegates with workspace.id; list() is member-reachable. - roleModelOverride: unknown driver dropped (never reaches getChatModel's throwing default), valid override passes through, blanks ignored. - getChatModel override success path (cross-driver fetch + decrypt; chatModel- only reuse), and service update/remove cross-workspace 'not found' guards + modelConfig tri-state. Tiny fix: findByCreator badge left-join now also requires enabled=true, so a disabled role (downgraded to universal by resolveRoleForRequest) no longer shows a misleading chat-list badge. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -32,12 +32,16 @@ export class AiChatRepo {
|
||||
// Left-join the bound role for the badge (emoji + name). Joined, not
|
||||
// denormalized — the chat list is not a hot path. A soft-deleted role
|
||||
// resolves to NULL so the badge disappears, matching the stream's behavior.
|
||||
// A DISABLED role (enabled=false) is likewise excluded: resolveRoleForRequest
|
||||
// downgrades such a chat to the universal assistant, so the badge must not
|
||||
// advertise a role that is not actually applied.
|
||||
const query = this.db
|
||||
.selectFrom('aiChats')
|
||||
.leftJoin('aiAgentRoles', (join) =>
|
||||
join
|
||||
.onRef('aiAgentRoles.id', '=', 'aiChats.roleId')
|
||||
.on('aiAgentRoles.deletedAt', 'is', null),
|
||||
.on('aiAgentRoles.deletedAt', 'is', null)
|
||||
.on('aiAgentRoles.enabled', '=', true),
|
||||
)
|
||||
.selectAll('aiChats')
|
||||
.select([
|
||||
|
||||
Reference in New Issue
Block a user