|
| 1 | +// npx vitest run src/__tests__/task-organization.test.ts |
| 2 | + |
| 3 | +import { |
| 4 | + MAX_PINNED_TARGETS, |
| 5 | + createEmptyTaskOrganizationState, |
| 6 | + pinnedItemSchema, |
| 7 | + manualTaskFolderSchema, |
| 8 | + taskOrganizationStateSchema, |
| 9 | + taskOrganizationMutationSchema, |
| 10 | + taskOrganizationMutationRequestSchema, |
| 11 | + taskOrganizationMutationResultSchema, |
| 12 | + taskOrganizationTargetSchema, |
| 13 | +} from "../task-organization.js" |
| 14 | + |
| 15 | +describe("taskOrganizationTargetSchema", () => { |
| 16 | + it("accepts a task target", () => { |
| 17 | + expect(taskOrganizationTargetSchema.safeParse({ kind: "task", taskId: "t1" }).success).toBe(true) |
| 18 | + }) |
| 19 | + |
| 20 | + it("accepts an autoGroup target", () => { |
| 21 | + expect(taskOrganizationTargetSchema.safeParse({ kind: "autoGroup", rootTaskId: "root" }).success).toBe(true) |
| 22 | + }) |
| 23 | + |
| 24 | + it("accepts a folder target", () => { |
| 25 | + expect(taskOrganizationTargetSchema.safeParse({ kind: "folder", folderId: "f1" }).success).toBe(true) |
| 26 | + }) |
| 27 | + |
| 28 | + it("rejects an unknown target kind", () => { |
| 29 | + expect(taskOrganizationTargetSchema.safeParse({ kind: "unknown", taskId: "t1" }).success).toBe(false) |
| 30 | + }) |
| 31 | + |
| 32 | + it("rejects a task target missing its taskId", () => { |
| 33 | + expect(taskOrganizationTargetSchema.safeParse({ kind: "task" }).success).toBe(false) |
| 34 | + }) |
| 35 | +}) |
| 36 | + |
| 37 | +describe("pinnedItemSchema", () => { |
| 38 | + it("accepts a valid pinned item", () => { |
| 39 | + expect( |
| 40 | + pinnedItemSchema.safeParse({ target: { kind: "task", taskId: "t1" }, pinnedAt: 1000 }).success, |
| 41 | + ).toBe(true) |
| 42 | + }) |
| 43 | + |
| 44 | + it("rejects a pinned item with a non-numeric timestamp", () => { |
| 45 | + expect( |
| 46 | + pinnedItemSchema.safeParse({ target: { kind: "task", taskId: "t1" }, pinnedAt: "now" }).success, |
| 47 | + ).toBe(false) |
| 48 | + }) |
| 49 | +}) |
| 50 | + |
| 51 | +describe("manualTaskFolderSchema", () => { |
| 52 | + it("accepts a valid folder", () => { |
| 53 | + expect( |
| 54 | + manualTaskFolderSchema.safeParse({ |
| 55 | + folderId: "f1", |
| 56 | + name: "Folder", |
| 57 | + taskIds: ["t1", "t2"], |
| 58 | + createdAt: 1000, |
| 59 | + updatedAt: 1000, |
| 60 | + }).success, |
| 61 | + ).toBe(true) |
| 62 | + }) |
| 63 | + |
| 64 | + it("rejects an empty folder name", () => { |
| 65 | + expect( |
| 66 | + manualTaskFolderSchema.safeParse({ |
| 67 | + folderId: "f1", |
| 68 | + name: "", |
| 69 | + taskIds: [], |
| 70 | + createdAt: 1000, |
| 71 | + updatedAt: 1000, |
| 72 | + }).success, |
| 73 | + ).toBe(false) |
| 74 | + }) |
| 75 | + |
| 76 | + it("rejects a folder name longer than 80 characters", () => { |
| 77 | + expect( |
| 78 | + manualTaskFolderSchema.safeParse({ |
| 79 | + folderId: "f1", |
| 80 | + name: "x".repeat(81), |
| 81 | + taskIds: [], |
| 82 | + createdAt: 1000, |
| 83 | + updatedAt: 1000, |
| 84 | + }).success, |
| 85 | + ).toBe(false) |
| 86 | + }) |
| 87 | +}) |
| 88 | + |
| 89 | +describe("taskOrganizationStateSchema", () => { |
| 90 | + it("accepts a valid version-1 state", () => { |
| 91 | + const state = { |
| 92 | + schemaVersion: 1, |
| 93 | + revision: 0, |
| 94 | + folders: [], |
| 95 | + pins: [], |
| 96 | + updatedAt: 1000, |
| 97 | + } |
| 98 | + expect(taskOrganizationStateSchema.safeParse(state).success).toBe(true) |
| 99 | + }) |
| 100 | + |
| 101 | + it("accepts a future schema version so the store can handle it gracefully", () => { |
| 102 | + const state = { |
| 103 | + schemaVersion: 99, |
| 104 | + revision: 1, |
| 105 | + folders: [], |
| 106 | + pins: [], |
| 107 | + updatedAt: 1000, |
| 108 | + } |
| 109 | + expect(taskOrganizationStateSchema.safeParse(state).success).toBe(true) |
| 110 | + }) |
| 111 | + |
| 112 | + it("rejects a negative revision", () => { |
| 113 | + const state = { |
| 114 | + schemaVersion: 1, |
| 115 | + revision: -1, |
| 116 | + folders: [], |
| 117 | + pins: [], |
| 118 | + updatedAt: 1000, |
| 119 | + } |
| 120 | + expect(taskOrganizationStateSchema.safeParse(state).success).toBe(false) |
| 121 | + }) |
| 122 | + |
| 123 | + it("rejects more than MAX_PINNED_TARGETS pins", () => { |
| 124 | + const pins = Array.from({ length: MAX_PINNED_TARGETS + 1 }, (_, i) => ({ |
| 125 | + target: { kind: "task", taskId: `t${i}` }, |
| 126 | + pinnedAt: 1000, |
| 127 | + })) |
| 128 | + const state = { |
| 129 | + schemaVersion: 1, |
| 130 | + revision: 0, |
| 131 | + folders: [], |
| 132 | + pins, |
| 133 | + updatedAt: 1000, |
| 134 | + } |
| 135 | + expect(taskOrganizationStateSchema.safeParse(state).success).toBe(false) |
| 136 | + }) |
| 137 | +}) |
| 138 | + |
| 139 | +describe("taskOrganizationMutationSchema", () => { |
| 140 | + it("accepts every mutation kind", () => { |
| 141 | + const mutations = [ |
| 142 | + { |
| 143 | + kind: "createFolder", |
| 144 | + folderId: "f1", |
| 145 | + name: "A", |
| 146 | + source: { kind: "task", taskId: "t1" }, |
| 147 | + destination: { kind: "task", taskId: "t2" }, |
| 148 | + }, |
| 149 | + { |
| 150 | + kind: "createFolderFromSelection", |
| 151 | + folderId: "f1", |
| 152 | + name: "A", |
| 153 | + targets: [ |
| 154 | + { kind: "task", taskId: "t1" }, |
| 155 | + { kind: "task", taskId: "t2" }, |
| 156 | + ], |
| 157 | + }, |
| 158 | + { kind: "deleteFolders", folderIds: ["f1", "f2"] }, |
| 159 | + { kind: "renameFolder", folderId: "f1", name: "Renamed" }, |
| 160 | + { kind: "deleteFolder", folderId: "f1" }, |
| 161 | + { kind: "moveToFolder", source: { kind: "task", taskId: "t1" }, folderId: "f1" }, |
| 162 | + { kind: "removeFromFolder", source: { kind: "task", taskId: "t1" }, folderId: "f1" }, |
| 163 | + { kind: "setPinned", target: { kind: "task", taskId: "t1" }, pinned: true }, |
| 164 | + ] |
| 165 | + for (const mutation of mutations) { |
| 166 | + expect(taskOrganizationMutationSchema.safeParse(mutation).success).toBe(true) |
| 167 | + } |
| 168 | + }) |
| 169 | + |
| 170 | + it("rejects an unknown mutation kind", () => { |
| 171 | + expect(taskOrganizationMutationSchema.safeParse({ kind: "explode" }).success).toBe(false) |
| 172 | + }) |
| 173 | + |
| 174 | + it("rejects createFolderFromSelection with fewer than two targets", () => { |
| 175 | + expect( |
| 176 | + taskOrganizationMutationSchema.safeParse({ |
| 177 | + kind: "createFolderFromSelection", |
| 178 | + folderId: "f1", |
| 179 | + name: "A", |
| 180 | + targets: [{ kind: "task", taskId: "t1" }], |
| 181 | + }).success, |
| 182 | + ).toBe(false) |
| 183 | + }) |
| 184 | + |
| 185 | + it("rejects deleteFolders with an empty folderIds array", () => { |
| 186 | + expect(taskOrganizationMutationSchema.safeParse({ kind: "deleteFolders", folderIds: [] }).success).toBe(false) |
| 187 | + }) |
| 188 | +}) |
| 189 | + |
| 190 | +describe("taskOrganizationMutationRequestSchema", () => { |
| 191 | + it("accepts a valid request", () => { |
| 192 | + expect( |
| 193 | + taskOrganizationMutationRequestSchema.safeParse({ |
| 194 | + requestId: "req-1", |
| 195 | + baseRevision: 3, |
| 196 | + mutation: { kind: "setPinned", target: { kind: "task", taskId: "t1" }, pinned: true }, |
| 197 | + }).success, |
| 198 | + ).toBe(true) |
| 199 | + }) |
| 200 | + |
| 201 | + it("rejects a negative baseRevision", () => { |
| 202 | + expect( |
| 203 | + taskOrganizationMutationRequestSchema.safeParse({ |
| 204 | + requestId: "req-1", |
| 205 | + baseRevision: -1, |
| 206 | + mutation: { kind: "setPinned", target: { kind: "task", taskId: "t1" }, pinned: true }, |
| 207 | + }).success, |
| 208 | + ).toBe(false) |
| 209 | + }) |
| 210 | +}) |
| 211 | + |
| 212 | +describe("taskOrganizationMutationResultSchema", () => { |
| 213 | + it("accepts a success result without an error", () => { |
| 214 | + expect( |
| 215 | + taskOrganizationMutationResultSchema.safeParse({ |
| 216 | + requestId: "req-1", |
| 217 | + success: true, |
| 218 | + committedRevision: 4, |
| 219 | + }).success, |
| 220 | + ).toBe(true) |
| 221 | + }) |
| 222 | + |
| 223 | + it("accepts a failure result with a typed error code", () => { |
| 224 | + expect( |
| 225 | + taskOrganizationMutationResultSchema.safeParse({ |
| 226 | + requestId: "req-1", |
| 227 | + success: false, |
| 228 | + committedRevision: 3, |
| 229 | + error: { code: "TASK_ORG/CONFLICT/002", message: "Organization state has changed. Please retry." }, |
| 230 | + }).success, |
| 231 | + ).toBe(true) |
| 232 | + }) |
| 233 | + |
| 234 | + it("rejects an unknown error code", () => { |
| 235 | + expect( |
| 236 | + taskOrganizationMutationResultSchema.safeParse({ |
| 237 | + requestId: "req-1", |
| 238 | + success: false, |
| 239 | + committedRevision: 3, |
| 240 | + error: { code: "TASK_ORG/UNKNOWN/999", message: "nope" }, |
| 241 | + }).success, |
| 242 | + ).toBe(false) |
| 243 | + }) |
| 244 | +}) |
| 245 | + |
| 246 | +describe("createEmptyTaskOrganizationState", () => { |
| 247 | + it("creates an empty version-1 state with a fixed clock", () => { |
| 248 | + const state = createEmptyTaskOrganizationState(() => 1234) |
| 249 | + expect(state).toEqual({ |
| 250 | + schemaVersion: 1, |
| 251 | + revision: 0, |
| 252 | + folders: [], |
| 253 | + pins: [], |
| 254 | + updatedAt: 1234, |
| 255 | + }) |
| 256 | + }) |
| 257 | + |
| 258 | + it("uses Date.now when no clock is provided", () => { |
| 259 | + const before = Date.now() |
| 260 | + const state = createEmptyTaskOrganizationState() |
| 261 | + const after = Date.now() |
| 262 | + expect(state.updatedAt).toBeGreaterThanOrEqual(before) |
| 263 | + expect(state.updatedAt).toBeLessThanOrEqual(after) |
| 264 | + }) |
| 265 | +}) |
0 commit comments