Fix conversation automation counter validation

This commit is contained in:
jjbrunton committed 2026-08-09 14:27:20 +01:00
1 parent b73e305cf3
commit c21932389e
3 files changed
+13 -4

No files matched your search

+3 -2
View File
@@ -1,5 +1,6 @@
import { z } from 'zod';
import { ErrorResponseSchema, RejectedActionResponseSchema } from './common.js';
import { MAX_CONFIGURED_AUTOMATED_CONVERSATION_TURNS } from './config.js';
import { RedditAccountOwnerSchema } from './session.js';
export const ChatMediaSchema = z.strictObject({
@@ -72,8 +73,8 @@ export const ConversationDetailResponseSchema = ActiveChatSchema.extend({
sentAt: z.number(),
}).nullable(),
automation: z.strictObject({
turnsUsed: z.number().int().min(0).max(3),
remainingTurns: z.number().int().min(0).max(3),
turnsUsed: z.number().int().min(0).max(MAX_CONFIGURED_AUTOMATED_CONVERSATION_TURNS),
remainingTurns: z.number().int().min(0).max(MAX_CONFIGURED_AUTOMATED_CONVERSATION_TURNS),
status: z.enum(['active', 'image-received', 'human-required', 'boundary', 'user-overridden']),
handoffReason: z.string().nullable(),
}),
+4 -2
View File
@@ -1,5 +1,7 @@
import { z } from 'zod';
export const MAX_CONFIGURED_AUTOMATED_CONVERSATION_TURNS = 20;
export const ToneKeySchema = z.union([
z.literal('default'),
z.literal('dominant'),
@@ -27,8 +29,8 @@ export const RuntimeConfigSchema = z.strictObject({
});
export const ConversationBehaviourSchema = z.strictObject({
mode: z.union([z.literal('legacy'), z.literal('r4r'), z.literal('r4r-media'), z.literal('manual')]).default('legacy'),
maxAutomatedTurns: z.number().int().min(1).max(20).default(3),
rapportTurnsBeforeMediaAsk: z.number().int().min(1).max(19).default(3),
maxAutomatedTurns: z.number().int().min(1).max(MAX_CONFIGURED_AUTOMATED_CONVERSATION_TURNS).default(3),
rapportTurnsBeforeMediaAsk: z.number().int().min(1).max(MAX_CONFIGURED_AUTOMATED_CONVERSATION_TURNS - 1).default(3),
}).superRefine((behaviour, context) => {
if (behaviour.mode === 'r4r-media' && behaviour.rapportTurnsBeforeMediaAsk >= behaviour.maxAutomatedTurns) {
context.addIssue({
@@ -184,6 +184,12 @@ describe('dashboard endpoint contracts', () => {
automation: { turnsUsed: 1, remainingTurns: 2, status: 'active', handoffReason: null },
syncInProgress: null,
}).success).toBe(true);
expect(ConversationDetailResponseSchema.safeParse({
...chat,
triggeringPost: null,
automation: { turnsUsed: 4, remainingTurns: 2, status: 'active', handoffReason: null },
syncInProgress: null,
}).success).toBe(true);
expect(ChatsResponseSchema.safeParse([{ ...chat, messages: [{ sender: 'alice' }] }]).success).toBe(false);
expect(ChatSyncTriggerResponseSchema.safeParse({ success: true, kind: 'delta' }).success).toBe(true);
expect(ChatSyncTriggerResponseSchema.safeParse({ success: false, reason: 'Already running full sync' }).success).toBe(true);