From a563de7967950da54300a007cd9145d608223fd0 Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Thu, 23 Apr 2026 03:13:58 +0200 Subject: [PATCH] fix(cat): correct create_task and create_organization handler column errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit create_task inserted invalid enum values that Postgres would reject: - status: 'pending' → current_status: 'idle' (correct column + valid value) - task_type: 'personal' → 'one_time' (valid: one_time|recurring_*|as_needed) - category: 'general' → 'other' (valid: cleaning|maintenance|admin|...|other) - priority: 'medium' → 'normal' (valid: low|normal|high|urgent) - removed due_date (not a column on the tasks table) create_organization had two fatal issues: - type (non-existent column) → label (valid: circle|family|dao|company|...) - missing slug (UNIQUE NOT NULL) — now generated from name + random suffix Also adds 4 regression tests covering both handlers. Co-Authored-By: Claude Sonnet 4.6 --- .../unit/cat/action-executor-columns.test.ts | 70 +++++++++++++++++++ src/services/cat/action-executor.ts | 27 +++++-- 2 files changed, 90 insertions(+), 7 deletions(-) diff --git a/__tests__/unit/cat/action-executor-columns.test.ts b/__tests__/unit/cat/action-executor-columns.test.ts index 8ff004ebd..db49e5fde 100644 --- a/__tests__/unit/cat/action-executor-columns.test.ts +++ b/__tests__/unit/cat/action-executor-columns.test.ts @@ -15,6 +15,7 @@ import { CatActionExecutor } from '@/services/cat/action-executor'; import { ENTITY_REGISTRY } from '@/config/entity-registry'; +import { DATABASE_TABLES } from '@/config/database-tables'; // ── Mock CatPermissionService ───────────────────────────────────────────────── // Auto-allow every action: allowed=true, requiresConfirmation=false @@ -333,4 +334,73 @@ describe('Cat action-executor — correct DB column names', () => { expect(result.error).toMatch(/no valid fields/i); }); }); + + // ── create_task ───────────────────────────────────────────────────────────── + describe('create_task', () => { + it('uses current_status (not status) with valid enum values', async () => { + const supabase = buildMockSupabase(); + const result = await run(supabase, 'create_task', { + title: 'Fix the sink', + description: 'Leaking pipe under kitchen sink', + }); + + expect(result.status).toBe('completed'); + const insert = getEntityInsert(supabase, DATABASE_TABLES.TASKS); + expect(insert).toBeDefined(); + + // Correct column name + expect(insert!.current_status).toBe('idle'); + // Wrong column name must NOT appear + expect((insert as Record).status).toBeUndefined(); + + // Valid enum values only + expect(insert!.task_type).toBe('one_time'); // not 'personal' + expect(insert!.category).toBe('other'); // not 'general' + expect(insert!.priority).toBe('normal'); // not 'medium' + expect(insert!.created_by).toBe(USER_ID); + }); + }); + + // ── create_organization ───────────────────────────────────────────────────── + describe('create_organization', () => { + it('uses label (not type) and generates a slug', async () => { + const supabase = buildMockSupabase(); + const result = await run(supabase, 'create_organization', { + name: 'Bitcoin Builders', + description: 'Building on Bitcoin', + label: 'company', + }); + + expect(result.status).toBe('completed'); + const insert = getEntityInsert(supabase, ENTITY_REGISTRY.group.tableName); + expect(insert).toBeDefined(); + + // Correct column used + expect(insert!.label).toBe('company'); + // Wrong column must NOT appear + expect((insert as Record).type).toBeUndefined(); + + // Slug must be generated (non-empty, derived from name) + expect(typeof insert!.slug).toBe('string'); + expect((insert!.slug as string).length).toBeGreaterThan(0); + expect((insert!.slug as string)).toMatch(/^bitcoin-builders-/); + + expect(insert!.name).toBe('Bitcoin Builders'); + expect(insert!.created_by).toBe(USER_ID); + }); + + it('defaults label to circle when not provided', async () => { + const supabase = buildMockSupabase(); + await run(supabase, 'create_organization', { name: 'My Group' }); + const insert = getEntityInsert(supabase, ENTITY_REGISTRY.group.tableName); + expect(insert!.label).toBe('circle'); + }); + + it('accepts legacy type param as fallback for label', async () => { + const supabase = buildMockSupabase(); + await run(supabase, 'create_organization', { name: 'Coop', type: 'cooperative' }); + const insert = getEntityInsert(supabase, ENTITY_REGISTRY.group.tableName); + expect(insert!.label).toBe('cooperative'); + }); + }); }); diff --git a/src/services/cat/action-executor.ts b/src/services/cat/action-executor.ts index 6e7662211..86045154c 100644 --- a/src/services/cat/action-executor.ts +++ b/src/services/cat/action-executor.ts @@ -372,17 +372,19 @@ const ACTION_HANDLERS: Partial> = { // ---------- PRODUCTIVITY ACTIONS ---------- create_task: async (supabase, userId, _actorId, params) => { + // DB columns: current_status (not status), task_type enum: one_time|recurring_scheduled|recurring_as_needed, + // category enum: cleaning|maintenance|admin|inventory|it|kitchen|workshop|logistics|other, + // priority enum: low|normal|high|urgent (not medium) const { data, error } = await supabase .from(DATABASE_TABLES.TASKS) .insert({ created_by: userId, title: params.title, description: (params.description as string | null) || null, - priority: (params.priority as string) || 'medium', - due_date: (params.due_date as string | null) || null, - status: 'pending', - task_type: 'personal', - category: 'general', + priority: (params.priority as string) || 'normal', + task_type: 'one_time', + category: 'other', + current_status: 'idle', }) .select() .single(); @@ -396,13 +398,24 @@ const ACTION_HANDLERS: Partial> = { // ---------- ORGANIZATION ACTIONS ---------- create_organization: async (supabase, userId, _actorId, params) => { + // groups table has: name, slug (UNIQUE NOT NULL), label (not type), created_by + // label enum: circle|family|dao|company|nonprofit|cooperative|guild|network_state + const name = params.name as string; + const slug = name + .toLowerCase() + .replace(/[^a-z0-9]+/g, '-') + .replace(/^-|-$/g, '') + .slice(0, 60) + + '-' + Math.random().toString(36).slice(2, 7); + // Create the group (organization) const { data: group, error: groupError } = await supabase .from(ENTITY_REGISTRY.group.tableName) .insert({ - name: params.name, + name, + slug, description: params.description || null, - type: params.type || 'organization', + label: (params.label as string | null) ?? (params.type as string | null) ?? 'circle', created_by: userId, }) .select()