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.6 KB
Raw Blame
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import { createAccountRepository } from '../../repositories/accountRepository.ts';
import { DatabaseError, NotFoundError } from '../../lib/errors.ts';
import { fakeSupabase, accountRow } from '../fixtures/fakeSupabase.ts';
import type { SupabaseClient } from '../../types/app.ts';

const ROWS = [
    accountRow({ id: 'a1', platform: 'youtube', title: 'Mine' }),
    accountRow({ id: 'a2', platform: 'instagram', username: 'theirs' }, 'user-2'),
    // a platform Twiligent no longer supports; its rows stay in the table until they are cleaned up
    accountRow({ id: 'a3', platform: 'retired', username: 'old' }),
];

describe('account repository', () => {
    test('user methods only see the user\'s rows; system methods see every row', async () => {
        const accounts = createAccountRepository(fakeSupabase(ROWS));
        assert.deepEqual((await accounts.listForUser('user-1')).map(account => account.id), ['a1']);
        assert.deepEqual((await accounts.listByPlatform('instagram')).map(account => account.id), ['a2']);
        assert.equal(await accounts.findForUser('a2', 'user-1'), null);
        assert.deepEqual(await accounts.findById('a2'), { id: 'a2', platform: 'instagram', username: 'theirs' });
    });

    test('rows of a platform Twiligent no longer supports are never read', async () => {
        const accounts = createAccountRepository(fakeSupabase(ROWS));
        assert.deepEqual((await accounts.listForUser('user-1')).map(account => account.id), ['a1']);
        assert.equal(await accounts.findForUser('a3', 'user-1'), null);
        assert.equal(await accounts.findById('a3'), null);
    });

    test('an update is one merge in the database that keeps the platform and the fields it does not name', async () => {
        const db = fakeSupabase(ROWS);
        const accounts = createAccountRepository(db);
        await accounts.updateForUser('a1', { subscriberCount: 5, platform: 'instagram', title: undefined }, 'user-1');
        assert.deepEqual(await accounts.findForUser('a1', 'user-1'), { id: 'a1', platform: 'youtube', title: 'Mine', subscriberCount: 5 });
        assert.deepEqual(db.calls[0], {
            type: 'rpc', table: 'merge_account_data', filters: [],
            values: { p_id: 'a1', p_user_id: 'user-1', p_patch: { subscriberCount: 5, title: undefined } },
        });
    });

    test('the cron form of an update reaches any user\'s account', async () => {
        const db = fakeSupabase(ROWS);
        await createAccountRepository(db).updateById('a2', { accessToken: 'new' });
        assert.equal((await createAccountRepository(db).findById('a2'))?.accessToken, 'new');
    });

    test('updating an account the user does not own is not found', async () => {
        const accounts = createAccountRepository(fakeSupabase(ROWS));
        await assert.rejects(accounts.updateForUser('a2', { title: 'x' }, 'user-1'), NotFoundError);
    });

    test('a failing query becomes a DatabaseError that hides the driver message', async () => {
        const broken = { from() { throw new Error('relation "accounts" does not exist'); } } as unknown as SupabaseClient;
        const failure = await createAccountRepository(broken).listForUser('user-1').catch((err: unknown) => err);
        assert.ok(failure instanceof DatabaseError);
        assert.equal(failure.operation, 'accounts.listForUser');
        assert.equal(failure.message, 'The database request failed. Try again in a moment.');
        assert.equal((failure.cause as Error).message, 'relation "accounts" does not exist');
    });
});