Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.9 KB
Raw Blame
/// <reference types="vitest/globals" />

import { vi } from 'vitest';

const rawRequest = vi.fn();
const deleteWebhook = vi.fn();

vi.mock('@linear/sdk', () => ({
  LinearClient: class {
    client = { rawRequest };
    deleteWebhook = deleteWebhook;
  },
  LinearDocument: {},
}));

import '../../index';
import { linearNewIssue } from '../triggers/new-issue';

function buildContext(store: { put: ReturnType<typeof vi.fn> }) {
  return {
    auth: { type: 'SECRET_TEXT', secret_text: 'lin_api_test' },
    webhookUrl: 'https://example.com/hook',
    propsValue: { team_id: 'team-1' },
    store: { ...store, get: vi.fn(), delete: vi.fn() },
  };
}

describe('trigger onEnable (F2)', () => {
  beforeEach(() => {
    rawRequest.mockReset();
    deleteWebhook.mockReset();
  });

  test('stores the webhook id when Linear creates the webhook', async () => {
    rawRequest.mockResolvedValue({ data: { webhookCreate: { success: true, webhook: { id: 'wh-1' } } } });
    const put = vi.fn().mockResolvedValue(undefined);
    await linearNewIssue.onEnable(buildContext({ put }));
    expect(put).toHaveBeenCalledWith('_new_issue_trigger', { webhookId: 'wh-1' });
    expect(rawRequest).toHaveBeenCalledTimes(1);
    expect(rawRequest.mock.calls[0][0]).toContain('webhookCreate');
    expect(rawRequest.mock.calls[0][1]).toMatchObject({ input: { teamId: 'team-1', resourceTypes: ['Issue'] } });
  });

  test('throws a clear admin-key message when Linear refuses', async () => {
    rawRequest.mockRejectedValue(Object.assign(new Error('Forbidden'), { type: 'Forbidden' }));
    await expect(linearNewIssue.onEnable(buildContext({ put: vi.fn() }))).rejects.toThrow('workspace admin');
  });

  test('throws instead of silently continuing when success is false', async () => {
    rawRequest.mockResolvedValue({ data: { webhookCreate: { success: false, webhook: null } } });
    const put = vi.fn();
    await expect(linearNewIssue.onEnable(buildContext({ put }))).rejects.toThrow('refused to create the webhook');
    expect(put).not.toHaveBeenCalled();
  });

  test('deletes the created webhook when storing its id fails', async () => {
    rawRequest.mockResolvedValue({ data: { webhookCreate: { success: true, webhook: { id: 'wh-2' } } } });
    deleteWebhook.mockResolvedValue({ success: true });
    const put = vi.fn().mockRejectedValue(new Error('store down'));
    await expect(linearNewIssue.onEnable(buildContext({ put }))).rejects.toThrow('store down');
    expect(deleteWebhook).toHaveBeenCalledWith('wh-2');
  });
});

describe('trigger onDisable', () => {
  beforeEach(() => {
    deleteWebhook.mockReset();
  });

  function disableContext() {
    const values = new Map<string, unknown>([['_new_issue_trigger', { webhookId: 'wh-9' }]]);
    return {
      values,
      context: {
        auth: { type: 'SECRET_TEXT', secret_text: 'lin_api_test' },
        webhookUrl: 'https://example.com/hook',
        propsValue: {},
        store: {
          put: vi.fn(),
          get: vi.fn(async (key: string) => values.get(key)),
          delete: vi.fn(async (key: string) => {
            values.delete(key);
          }),
        },
      },
    };
  }

  test('treats a webhook Linear already deleted as gone and clears the store', async () => {
    deleteWebhook.mockRejectedValue(new Error('Entity not found: Webhook - Could not find referenced Webhook.'));
    const { values, context } = disableContext();
    await linearNewIssue.onDisable(context);
    expect(deleteWebhook).toHaveBeenCalledWith('wh-9');
    expect(values.has('_new_issue_trigger')).toBe(false);
  });

  test('rethrows any other error and keeps the stored webhook', async () => {
    deleteWebhook.mockRejectedValue(new Error('Linear rate limit reached.'));
    const { values, context } = disableContext();
    await expect(linearNewIssue.onDisable(context)).rejects.toThrow('rate limit');
    expect(values.get('_new_issue_trigger')).toEqual({ webhookId: 'wh-9' });
  });
});