import { describe, expect, it, beforeAll } from 'vitest';
import zapier from 'zapier-platform-core';

import App from '../index.js';
const appTester = zapier.createAppTester(App);

// Only defining the env vars here so the tests out of the box.
// You should create a `.env` file and populate it with your actual Trello
// client ID and secret (called "API Key" and "API Secret" by Trello), which you
// can find on https://trello.com/app-key.
// The `.env` file should look like
/*
    CLIENT_ID=<trello_api_key>
    CLIENT_SECRET=<trello_api_secret>
*/
// then you can delete the following 2 lines
process.env.CLIENT_ID = process.env.CLIENT_ID || '<trello_api_key>';
process.env.CLIENT_SECRET = process.env.CLIENT_SECRET || '<trello_api_secret>';

describe('oauth1 app', () => {
  beforeAll(() => {
    // It's a good idea to store your Client ID and Secret in the environment rather than in code.
    if (!(process.env.CLIENT_ID && process.env.CLIENT_SECRET)) {
      throw new Error(
        `Before running the tests, make sure CLIENT_ID and CLIENT_SECRET are available in the environment.`
      );
    }
  });

  it('fetch a request token', async () => {
    const bundle = {
      inputData: {
        // You should add your redirect URI to https://trello.com/app-key
        redirect_uri: 'https://zapier.com',
      },
    };
    const tokens = await appTester(
      App.authentication.oauth1Config.getRequestToken,
      bundle
    );
    expect(tokens).toHaveProperty('oauth_token');
    expect(tokens).toHaveProperty('oauth_token_secret');
  });

  it('generates an authorize URL', async () => {
    const bundle = {
      // In production, these will be generated by Zapier and set automatically
      inputData: {
        oauth_token: '4444',
        redirect_uri: 'https://zapier.com/',
      },
    };

    const authorizeUrl = await appTester(
      App.authentication.oauth1Config.authorizeUrl,
      bundle
    );

    expect(authorizeUrl).toBe(
      'https://trello.com/1/OAuthAuthorizeToken?oauth_token=4444&name=Zapier%2FTrello%20OAuth1%20Test'
    );
  });
});
