# All files / src/test fixtures.ts

96.77% Statements 30/31
80% Branches 8/10
100% Functions 1/1
96.77% Lines 30/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317

    48x
    48x
    48x

    48x
    48x
    48x

    48x

    48x

    48x
    53x

    53x

    53x
    53x

    67x

    67x

    67x
    67x
    158x

    158x

    158x
    93x

    48x
    93x
    98x
    98x

    98x

    48x

    8x

    8x

    8x

     /**
     * Test Fixtures for E2E Testing
     *
     * Provides fixture helpers that seed complete task hierarchies
     * for integration and E2E tests.
     */

    import { nanoid } from 'nanoid';
    import type { DrizzleDatabase } from '../db/index.js';
    import {
      DrizzleInitiativeRepository,
      DrizzlePhaseRepository,
      DrizzleTaskRepository,
    } from '../db/repositories/drizzle/index.js';
    import { taskDependencies } from '../db/schema.js';

    // =============================================================================
    // Fixture Interfaces
    // =============================================================================

    /**
     * Task fixture definition.
     */
    export interface TaskFixture {
      /** Unique identifier for this task (used for dependency references) */
      id: string;
      /** Task name */
      name: string;
      /** Task priority */
      priority?: 'low' | 'medium' | 'high';
      /** Task category */
      category?: 'execute' | 'research' | 'discuss' | 'plan' | 'detail' | 'refine' | 'verify' | 'merge' | 'review';
      /** Names of other tasks in same fixture this task depends on */
      dependsOn?: string[];
    }

    /**
     * Task group fixture definition (replaces Plan).
     * Tasks are grouped by parent task in the new model.
     */
    export interface TaskGroupFixture {
      /** Group name (becomes a detail task) */
      name: string;
      /** Tasks in this group */
      tasks: TaskFixture[];
    }

    /**
     * Phase fixture definition.
     */
    export interface PhaseFixture {
      /** Phase name */
      name: string;
      /** Task groups in this phase (each group becomes a parent detail task) */
      taskGroups: TaskGroupFixture[];
    }

    /**
     * Initiative fixture definition (top-level).
     */
    export interface InitiativeFixture {
      /** Initiative name */
      name: string;
      /** Phases in this initiative */
      phases: PhaseFixture[];
    }

    /**
     * Result of seeding a fixture.
     * Maps names to IDs for all created entities.
     */
    export interface SeededFixture {
      /** ID of the created initiative */
      initiativeId: string;
      /** Map of phase names to IDs */
      phases: Map<string, string>;
      /** Map of task group names to parent task IDs */
      taskGroups: Map<string, string>;
      /** Map of task names to IDs */
      tasks: Map<string, string>;
    }

    // =============================================================================
    // Seed Function
    // =============================================================================

    /**
     * Seed a complete task hierarchy from a fixture definition.
     *
     * Creates initiative, phases, detail tasks (as parent), and child tasks.
     * Resolves task dependencies by name to actual task IDs.
     *
     * @param db - Drizzle database instance
     * @param fixture - The fixture definition to seed
     * @returns SeededFixture with all created entity IDs
     */
    export async function seedFixture(
      db: DrizzleDatabase,
      fixture: InitiativeFixture
    ): Promise<SeededFixture> {
      // Create repositories
      const initiativeRepo = new DrizzleInitiativeRepository(db);
      const phaseRepo = new DrizzlePhaseRepository(db);
      const taskRepo = new DrizzleTaskRepository(db);

      // Result maps
      const phasesMap = new Map<string, string>();
      const taskGroupsMap = new Map<string, string>();
      const tasksMap = new Map<string, string>();

      // Collect all task dependencies to resolve after creation
      const pendingDependencies: Array<{ taskId: string; dependsOnNames: string[] }> = [];

      // Create initiative
      const initiative = await initiativeRepo.create({
        name: fixture.name,
        status: 'active',
      });

      // Create phases
      for (const phaseFixture of fixture.phases) {
        const phase = await phaseRepo.create({
          initiativeId: initiative.id,
          name: phaseFixture.name,
          status: 'pending',
        });
        phasesMap.set(phaseFixture.name, phase.id);

        // Create task groups as parent detail tasks
        let taskOrder = 0;
        for (const groupFixture of phaseFixture.taskGroups) {
          // Create parent detail task
          const parentTask = await taskRepo.create({
            phaseId: phase.id,
            initiativeId: initiative.id,
            name: groupFixture.name,
            description: `Test task group: ${groupFixture.name}`,
            category: 'detail',
            type: 'auto',
            priority: 'medium',
            status: 'completed', // Detail tasks are completed once child tasks are created
            order: taskOrder++,
          });
          taskGroupsMap.set(groupFixture.name, parentTask.id);

          // Create child tasks linked to parent
          let childOrder = 0;
          for (const taskFixture of groupFixture.tasks) {
            const task = await taskRepo.create({
              parentTaskId: parentTask.id,
              phaseId: phase.id,
              initiativeId: initiative.id,
              name: taskFixture.name,
              description: `Test task: ${taskFixture.name}`,
              category: taskFixture.category ?? 'execute',
              type: 'auto',
              priority: taskFixture.priority ?? 'medium',
              status: 'pending',
              order: childOrder++,
            });
            tasksMap.set(taskFixture.id, task.id);

            // Collect dependencies to resolve later
            if (taskFixture.dependsOn && taskFixture.dependsOn.length > 0) {
              pendingDependencies.push({
                taskId: task.id,
                dependsOnNames: taskFixture.dependsOn,
              });
            }
          }
        }
      }

      // Resolve and insert task dependencies
      for (const { taskId, dependsOnNames } of pendingDependencies) {
        for (const depName of dependsOnNames) {
          const dependsOnTaskId = tasksMap.get(depName);
          Iif (!dependsOnTaskId) {
            throw new Error(
              `Dependency resolution failed: task "${depName}" not found in fixture`
            );
          }

          // Insert into task_dependencies table
          await db.insert(taskDependencies).values({
            id: nanoid(),
            taskId,
            dependsOnTaskId,
            createdAt: new Date(),
          });
        }
      }

      return {
        initiativeId: initiative.id,
        phases: phasesMap,
        taskGroups: taskGroupsMap,
        tasks: tasksMap,
      };
    }

    // =============================================================================
    // Convenience Fixtures
    // =============================================================================

    /**
     * Simple fixture: 1 initiative -> 1 phase -> 1 task group -> 3 tasks.
     *
     * Task dependency structure:
     * - Task A: no dependencies
     * - Task B: depends on Task A
     * - Task C: depends on Task A
     */
    export const SIMPLE_FIXTURE: InitiativeFixture = {
      name: 'Simple Test Initiative',
      phases: [
        {
          name: 'Phase 1',
          taskGroups: [
            {
              name: 'Task Group 1',
              tasks: [
                { id: 'Task A', name: 'Task A', priority: 'high' },
                { id: 'Task B', name: 'Task B', priority: 'medium', dependsOn: ['Task A'] },
                { id: 'Task C', name: 'Task C', priority: 'medium', dependsOn: ['Task A'] },
              ],
            },
          ],
        },
      ],
    };

    /**
     * Parallel fixture: 1 initiative -> 1 phase -> 2 task groups (each with 2 independent tasks).
     *
     * Task structure:
     * - Group A: Task X, Task Y (independent)
     * - Group B: Task P, Task Q (independent)
     */
    export const PARALLEL_FIXTURE: InitiativeFixture = {
      name: 'Parallel Test Initiative',
      phases: [
        {
          name: 'Parallel Phase',
          taskGroups: [
            {
              name: 'Group A',
              tasks: [
                { id: 'Task X', name: 'Task X', priority: 'high' },
                { id: 'Task Y', name: 'Task Y', priority: 'medium' },
              ],
            },
            {
              name: 'Group B',
              tasks: [
                { id: 'Task P', name: 'Task P', priority: 'high' },
                { id: 'Task Q', name: 'Task Q', priority: 'low' },
              ],
            },
          ],
        },
      ],
    };

    /**
     * Complex fixture: 1 initiative -> 2 phases -> 4 task groups with cross-group dependencies.
     *
     * Structure:
     * - Phase 1: Group 1 (Task 1A, 1B), Group 2 (Task 2A depends on 1A)
     * - Phase 2: Group 3 (Task 3A depends on 1B), Group 4 (Task 4A depends on 2A and 3A)
     */
    export const COMPLEX_FIXTURE: InitiativeFixture = {
      name: 'Complex Test Initiative',
      phases: [
        {
          name: 'Phase 1',
          taskGroups: [
            {
              name: 'Group 1',
              tasks: [
                { id: 'Task 1A', name: 'Task 1A', priority: 'high' },
                { id: 'Task 1B', name: 'Task 1B', priority: 'medium' },
              ],
            },
            {
              name: 'Group 2',
              tasks: [
                { id: 'Task 2A', name: 'Task 2A', priority: 'high', dependsOn: ['Task 1A'] },
              ],
            },
          ],
        },
        {
          name: 'Phase 2',
          taskGroups: [
            {
              name: 'Group 3',
              tasks: [
                { id: 'Task 3A', name: 'Task 3A', priority: 'high', dependsOn: ['Task 1B'] },
              ],
            },
            {
              name: 'Group 4',
              tasks: [
                {
                  id: 'Task 4A',
                  name: 'Task 4A',
                  priority: 'high',
                  dependsOn: ['Task 2A', 'Task 3A'],
                },
              ],
            },
          ],
        },
      ],
    };