Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

3.4 KB
Raw Blame
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
  TESTER_HINT, instagramErrorMessage, readInstagramCompletion, readOAuthReturn, accountMetrics, accountCardHandle, visibleSections, channelFieldProblem } from '../../../src/features/accounts/accountManagerData.ts';

const params = (query: string) => new URLSearchParams(query);

test('known Instagram errors have their own message', () => {
  assert.equal(instagramErrorMessage('already_added'), 'This Instagram account is already connected.');
  assert.ok(instagramErrorMessage('access_denied').endsWith(TESTER_HINT));
});

test('unknown Instagram errors name the code and add the tester hint', () => {
  assert.equal(instagramErrorMessage('weird'), `Instagram connection failed: weird. ${TESTER_HINT}`);
});

test('a return without OAuth or add parameters does nothing', () => {
  assert.equal(readOAuthReturn(params('')), null);
  assert.equal(readOAuthReturn(params('ig_error=&add=')), null);
});

test('a successful connection is reported without an error', () => {
  assert.deepEqual(readOAuthReturn(params('ig_connected=true')), { connected: true, reconnected: false, errorMessage: null, openAdd: false });
});

test('connecting an account that was already there is reported as a reconnection', () => {
  assert.deepEqual(readOAuthReturn(params('ig_connected=reconnected')), { connected: true, reconnected: true, errorMessage: null, openAdd: false });
});

test('connected values other than true are dropped silently', () => {
  assert.deepEqual(readOAuthReturn(params('ig_connected=false')), { connected: false, reconnected: false, errorMessage: null, openAdd: false });
});

test('errors and the add flag are read together', () => {
  const result = readOAuthReturn(params('ig_error=cancelled&add=1'))!;
  assert.equal(result.connected, false);
  assert.equal(result.errorMessage, 'Instagram sign-in was cancelled. Try again when you are ready.');
  assert.equal(result.openAdd, true);
});

test('each platform card shows its own three metrics', () => {
  assert.deepEqual(accountMetrics('youtube', { subscriberCount: 1, viewCount: 2, videoCount: 3 }), [['Subscribers', 1], ['Views', 2], ['Videos', 3]]);
  assert.deepEqual(accountMetrics('instagram', { followersCount: 4 }).map(([label]) => label), ['Followers', 'Following', 'Posts']);
});

test('card handles use the platform handle', () => {
  assert.equal(accountCardHandle({ platform: 'instagram', username: 'ribbon' }), '@ribbon');
  assert.equal(accountCardHandle({ customUrl: '@dusk' }), '@dusk');
});

test('only platforms with accounts get a section, in a fixed order', () => {
  assert.deepEqual(visibleSections({ youtube: [], instagram: [{ id: 1 }] }).map(section => section.key), ['instagram']);
  assert.deepEqual(visibleSections({ youtube: [{ id: 2 }], instagram: [{ id: 1 }] }).map(section => section.key), ['youtube', 'instagram']);
});

test('adding a channel with nothing typed points at the channel field', () => {
  assert.equal(channelFieldProblem(' ')?.field, 'channel');
  assert.equal(channelFieldProblem('@channel'), null);
});

test('a finished Instagram sign-in is read from the fragment, and only with both the code and the state', () => {
  assert.deepEqual(readInstagramCompletion('#ig_code=abc%23_&ig_state=s.sig'), { code: 'abc#_', state: 's.sig' });
  assert.equal(readInstagramCompletion('#ig_code=abc'), null);
  assert.equal(readInstagramCompletion(''), null);
  assert.equal(readInstagramCompletion('#section'), null);
});