# All files / src/test-utils storybook-helpers.ts

0% Statements 0/183
100% Branches 1/1
100% Functions 1/1
0% Lines 0/183

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

     /**
     * Mock data builders and helpers for Storybook stories.
     *
     * Provides factory functions that create realistic test data with
     * sensible defaults and optional overrides.
     */

    import type {
      Environment,
      Session,
      SessionEvent,
      Workspace,
      TaskData,
      TokenInfo,
      PersonaData,
      ScheduleData,
      CredentialProviderConfig,
      Codespace,
    } from "../hooks/types.js";
    import type { GraphNode, GraphLink } from "../hooks/types.js";

    export {
      MOCK_ENVIRONMENTS,
      MOCK_SESSIONS,
      MOCK_WORKSPACES,
      MOCK_TASKS,
      MOCK_TOKENS,
      MOCK_PERSONAS,
    } from "../mocks/mockData.js";

    // ─── Factory functions ──────────────────────────────────────────────────────

    let idCounter: number = 0;

    /** Generate a unique ID for test entities. */
    function nextId(prefix: string): string {
      return `${prefix}-${++idCounter}`;
    }

    /** Create an Environment with sensible defaults. */
    export function makeEnvironment(overrides: Partial<Environment> = {}): Environment {
      return {
        id: nextId("env"),
        displayName: "Test Env",
        adapterType: "local",
        adapterConfig: "{}",
        status: "connected",
        bootstrapped: true,
        githubAccountId: "",
        ...overrides,
      };
    }

    /** Create a Session with sensible defaults. */
    export function makeSession(overrides: Partial<Session> = {}): Session {
      return {
        id: nextId("sess"),
        environmentId: "env-1",
        runtime: "claude-code",
        status: "idle",
        prompt: "Test prompt",
        startedAt: "2026-01-01T00:00:00Z",
        ...overrides,
      };
    }

    /** Create a Workspace with sensible defaults. */
    export function makeWorkspace(overrides: Partial<Workspace> = {}): Workspace {
      return {
        id: nextId("ws"),
        name: "Test Workspace",
        description: "",
        repoUrl: "",
        linkedEnvironmentIds: ["env-1"],
        status: "active",
        workingDirectory: "",
        useWorktrees: false,
        defaultPersonaId: "",
        tokenBudget: 0,
        costBudgetMillicents: 0,
        createdAt: "2026-01-01T00:00:00Z",
        updatedAt: "2026-01-01T00:00:00Z",
        ...overrides,
      };
    }

    /** Create a TaskData with sensible defaults. */
    export function makeTask(overrides: Partial<TaskData> = {}): TaskData {
      return {
        id: nextId("task"),
        workspaceId: "ws-1",
        title: "Test Task",
        description: "",
        status: "not_started",
        branch: "main",
        latestSessionId: "",
        dependsOn: [],
        sortOrder: 0,
        createdAt: "2026-01-01T00:00:00Z",
        parentTaskId: "",
        depth: 0,
        childTaskIds: [],
        canDecompose: false,
        injectKnowledge: true,
        defaultPersonaId: "",
        workpad: "",
        tokenBudget: 0,
        costBudgetMillicents: 0,
        ...overrides,
      };
    }

    /** Create a TokenInfo with sensible defaults. */
    export function makeToken(overrides: Partial<TokenInfo> = {}): TokenInfo {
      return {
        name: "TEST_TOKEN",
        tokenType: "env_var",
        envVar: "TEST_TOKEN",
        filePath: "",
        expiresAt: "",
        ...overrides,
      };
    }

    /** Create a PersonaData with sensible defaults. */
    export function makePersona(overrides: Partial<PersonaData> = {}): PersonaData {
      return {
        id: nextId("persona"),
        name: "Test Persona",
        description: "A test persona",
        systemPrompt: "You are a test agent.",
        toolConfig: "",
        runtime: "claude-code",
        model: "sonnet",
        maxTurns: 0,
        mcpServers: "",
        createdAt: "2026-01-01T00:00:00Z",
        updatedAt: "2026-01-01T00:00:00Z",
        type: "agent",
        script: "",
        allowedMcpTools: [],
        ...overrides,
      };
    }

    /** Create a ScheduleData with sensible defaults. */
    export function makeSchedule(overrides: Partial<ScheduleData> = {}): ScheduleData {
      return {
        id: nextId("schedule"),
        title: "Test Schedule",
        description: "A test schedule",
        scheduleExpression: "5m",
        personaId: "persona-1",
        workspaceId: "",
        parentTaskId: "",
        enabled: true,
        lastRunAt: "",
        nextRunAt: new Date(Date.now() + 5 * 60_000).toISOString(),
        runCount: 0,
        createdAt: "2026-01-01T00:00:00Z",
        updatedAt: "2026-01-01T00:00:00Z",
        ...overrides,
      };
    }

    /** Create a SessionEvent with sensible defaults. */
    export function makeEvent(overrides: Partial<SessionEvent> = {}): SessionEvent {
      return {
        sessionId: "sess-1",
        eventType: "text",
        timestamp: "2026-01-01T00:00:00Z",
        content: "Test event content",
        ...overrides,
      };
    }

    /** Create a Codespace with sensible defaults. */
    export function makeCodespace(overrides: Partial<Codespace> = {}): Codespace {
      return {
        name: "test-codespace",
        repository: "owner/repo",
        state: "Available",
        gitStatus: "clean",
        ...overrides,
      };
    }

    /** Default credential provider config (all off). */
    export function makeCredentialProviders(
      overrides: Partial<CredentialProviderConfig> = {},
    ): CredentialProviderConfig {
      return {
        claude: "off",
        github: "off",
        copilot: "off",
        codex: "off",
        goose: "off",
        ...overrides,
      };
    }

    /** Create a GraphNode with sensible defaults. */
    export function makeGraphNode(overrides: Partial<GraphNode> = {}): GraphNode {
      return {
        id: nextId("kn"),
        label: "Test Knowledge Node",
        kind: "knowledge",
        category: "concept",
        content: "Test node content.",
        tags: [],
        createdAt: "2026-01-15T10:00:00Z",
        updatedAt: "2026-02-20T14:30:00Z",
        val: 1,
        ...overrides,
      };
    }

    /** Create a GraphLink with sensible defaults. */
    export function makeGraphLink(overrides: Partial<GraphLink> = {}): GraphLink {
      return {
        source: "kn-1",
        target: "kn-2",
        type: "relates_to",
        ...overrides,
      };
    }

    /** No-op callback for story args. */
    export const noop = (): void => {};

    // ─── Storybook decorators ───────────────────────────────────────────────────

    export { withMockGrackle, withMockGrackleRoute } from "./storybook-decorators.js";

    // ─── Aliases (some stories use "build" prefix) ──────────────────────────────

    export const buildEnvironment: typeof makeEnvironment = makeEnvironment;
    export const buildSession: typeof makeSession = makeSession;
    export const buildWorkspace: typeof makeWorkspace = makeWorkspace;
    export const buildTask: typeof makeTask = makeTask;
    export const buildToken: typeof makeToken = makeToken;
    export const buildPersona: typeof makePersona = makePersona;
    export const buildEvent: typeof makeEvent = makeEvent;
    export const buildCodespace: typeof makeCodespace = makeCodespace;
    export const buildCredentialProviderConfig: typeof makeCredentialProviders =
      makeCredentialProviders;
    export const buildGraphNode: typeof makeGraphNode = makeGraphNode;
    export const buildGraphLink: typeof makeGraphLink = makeGraphLink;