|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { |
| 4 | + buildEntityContext, |
| 5 | + type ConversationEntity, |
| 6 | + type Task, |
| 7 | +} from "./index"; |
| 8 | + |
| 9 | +function buildTask(overrides: Partial<Task> = {}): Task { |
| 10 | + return { |
| 11 | + id: "task-1", |
| 12 | + userId: "user-1", |
| 13 | + sourceInboxItemId: "inbox-1", |
| 14 | + lastInboxItemId: "inbox-1", |
| 15 | + title: "Gym session", |
| 16 | + lifecycleState: "pending_schedule", |
| 17 | + externalCalendarEventId: null, |
| 18 | + externalCalendarId: null, |
| 19 | + scheduledStartAt: null, |
| 20 | + scheduledEndAt: null, |
| 21 | + calendarSyncStatus: "in_sync", |
| 22 | + calendarSyncUpdatedAt: null, |
| 23 | + rescheduleCount: 0, |
| 24 | + lastFollowupAt: null, |
| 25 | + followupReminderSentAt: null, |
| 26 | + completedAt: null, |
| 27 | + archivedAt: null, |
| 28 | + priority: "medium", |
| 29 | + urgency: "medium", |
| 30 | + ...overrides, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +function buildEntity( |
| 35 | + overrides: Partial<ConversationEntity>, |
| 36 | +): ConversationEntity { |
| 37 | + return { |
| 38 | + id: "entity-1", |
| 39 | + conversationId: "conversation-1", |
| 40 | + label: "Entity label", |
| 41 | + status: "active", |
| 42 | + createdAt: "2026-04-01T15:00:00.000Z", |
| 43 | + updatedAt: "2026-04-01T15:00:00.000Z", |
| 44 | + kind: "task", |
| 45 | + data: { |
| 46 | + taskId: "task-1", |
| 47 | + title: "Gym session", |
| 48 | + lifecycleState: "pending_schedule", |
| 49 | + scheduledStartAt: null, |
| 50 | + scheduledEndAt: null, |
| 51 | + }, |
| 52 | + ...overrides, |
| 53 | + } as ConversationEntity; |
| 54 | +} |
| 55 | + |
| 56 | +describe("entity context", () => { |
| 57 | + it("builds filtered known entities and derives focus, proposal, and clarification helpers", () => { |
| 58 | + const context = buildEntityContext({ |
| 59 | + entityRegistry: [ |
| 60 | + buildEntity({ |
| 61 | + id: "task-entity-1", |
| 62 | + label: "Gym session", |
| 63 | + kind: "task", |
| 64 | + data: { |
| 65 | + taskId: "task-1", |
| 66 | + title: "Gym session", |
| 67 | + lifecycleState: "scheduled", |
| 68 | + scheduledStartAt: "2026-04-02T01:00:00.000Z", |
| 69 | + scheduledEndAt: "2026-04-02T02:00:00.000Z", |
| 70 | + }, |
| 71 | + }), |
| 72 | + buildEntity({ |
| 73 | + id: "proposal-1", |
| 74 | + label: "Schedule gym tomorrow at 6pm", |
| 75 | + kind: "proposal_option", |
| 76 | + status: "presented", |
| 77 | + data: { |
| 78 | + route: "conversation_then_mutation", |
| 79 | + replyText: "Would you like me to schedule it at 6pm?", |
| 80 | + confirmationRequired: true, |
| 81 | + targetEntityId: "task-entity-1", |
| 82 | + mutationInputSource: null, |
| 83 | + originatingTurnText: "Schedule gym tomorrow at 6pm", |
| 84 | + missingFields: ["scheduleFields.time"], |
| 85 | + fieldSnapshot: {}, |
| 86 | + }, |
| 87 | + }), |
| 88 | + buildEntity({ |
| 89 | + id: "clarification-1", |
| 90 | + label: "What time?", |
| 91 | + kind: "clarification", |
| 92 | + data: { |
| 93 | + prompt: "What time should I schedule it?", |
| 94 | + reason: null, |
| 95 | + open: true, |
| 96 | + }, |
| 97 | + }), |
| 98 | + buildEntity({ |
| 99 | + id: "block-1", |
| 100 | + label: "Write blog post", |
| 101 | + kind: "scheduled_block", |
| 102 | + data: { |
| 103 | + blockId: "block-db-1", |
| 104 | + taskId: "task-2", |
| 105 | + title: "Write blog post", |
| 106 | + startAt: "2026-04-02T03:00:00.000Z", |
| 107 | + endAt: "2026-04-02T04:00:00.000Z", |
| 108 | + externalCalendarId: "primary", |
| 109 | + }, |
| 110 | + }), |
| 111 | + buildEntity({ |
| 112 | + id: "reminder-1", |
| 113 | + label: "Review taxes", |
| 114 | + kind: "reminder", |
| 115 | + status: "active", |
| 116 | + data: { |
| 117 | + taskId: "task-3", |
| 118 | + title: "Review taxes", |
| 119 | + reminderKind: "reminder", |
| 120 | + number: 1, |
| 121 | + }, |
| 122 | + }), |
| 123 | + // These should be filtered out: |
| 124 | + buildEntity({ |
| 125 | + id: "done-task", |
| 126 | + label: "Completed task", |
| 127 | + kind: "task", |
| 128 | + data: { |
| 129 | + taskId: "task-done", |
| 130 | + title: "Completed task", |
| 131 | + lifecycleState: "done", |
| 132 | + scheduledStartAt: null, |
| 133 | + scheduledEndAt: null, |
| 134 | + }, |
| 135 | + }), |
| 136 | + buildEntity({ |
| 137 | + id: "resolved-proposal", |
| 138 | + label: "Old proposal", |
| 139 | + kind: "proposal_option", |
| 140 | + status: "resolved", |
| 141 | + data: { |
| 142 | + route: "conversation_then_mutation", |
| 143 | + replyText: "Old proposal", |
| 144 | + confirmationRequired: true, |
| 145 | + targetEntityId: null, |
| 146 | + mutationInputSource: null, |
| 147 | + fieldSnapshot: {}, |
| 148 | + }, |
| 149 | + }), |
| 150 | + buildEntity({ |
| 151 | + id: "closed-clarification", |
| 152 | + label: "Closed clarification", |
| 153 | + kind: "clarification", |
| 154 | + data: { |
| 155 | + prompt: "Closed clarification", |
| 156 | + reason: null, |
| 157 | + open: false, |
| 158 | + }, |
| 159 | + }), |
| 160 | + ], |
| 161 | + tasks: [ |
| 162 | + buildTask({ id: "task-1", title: "Gym session duplicate" }), // matches registry by taskId, should be deduplicated |
| 163 | + buildTask({ |
| 164 | + id: "task-2", |
| 165 | + title: "Weekly review", |
| 166 | + lifecycleState: "awaiting_followup", |
| 167 | + externalCalendarEventId: "event-1", |
| 168 | + externalCalendarId: "primary", |
| 169 | + scheduledStartAt: "2026-04-02T05:00:00.000Z", |
| 170 | + scheduledEndAt: "2026-04-02T06:00:00.000Z", |
| 171 | + }), |
| 172 | + ], |
| 173 | + discourseState: { |
| 174 | + focus_entity_id: "task-entity-1", |
| 175 | + currently_editable_entity_id: null, |
| 176 | + last_user_mentioned_entity_ids: [], |
| 177 | + last_presented_items: [], |
| 178 | + pending_clarifications: [], |
| 179 | + mode: "planning", |
| 180 | + }, |
| 181 | + }); |
| 182 | + |
| 183 | + // Sorted by expectedType then label then id |
| 184 | + expect(context.knownEntities).toEqual([ |
| 185 | + { id: "clarification-1", label: "What time should I schedule it?", expectedType: "clarification", state: "open" }, |
| 186 | + { id: "proposal-1", label: "Schedule gym tomorrow at 6pm", expectedType: "proposal", state: "presented" }, |
| 187 | + { id: "reminder-1", label: "Review taxes", expectedType: "reminder", state: "active" }, |
| 188 | + { id: "block-1", label: "Write blog post", expectedType: "scheduled_block", state: "scheduled" }, |
| 189 | + { id: "task-entity-1", label: "Gym session", expectedType: "task", state: "scheduled" }, |
| 190 | + { id: "task-2", label: "Weekly review", expectedType: "task", state: "awaiting_followup" }, |
| 191 | + ]); |
| 192 | + expect(context.focusedEntityId).toBe("task-entity-1"); |
| 193 | + expect(context.activeProposal).toEqual({ |
| 194 | + id: "proposal-1", |
| 195 | + summary: "Schedule gym tomorrow at 6pm", |
| 196 | + missingFields: ["scheduleFields.time"], |
| 197 | + }); |
| 198 | + expect(context.openClarification).toEqual({ |
| 199 | + id: "clarification-1", |
| 200 | + prompt: "What time should I schedule it?", |
| 201 | + }); |
| 202 | + }); |
| 203 | +}); |
0 commit comments