Skip to content

A local-first personal finance app

2.7 KB
Raw Blame
import { getDownloadError, getSyncError } from './errors';

const SCHEMA_MISMATCH_MESSAGE =
  'This budget could not be loaded because it uses a newer database schema than this version of Actual supports. Make sure you are using the latest version, then try again.';

const schemaMismatchMeta = {
  error: { message: 'no such column: cleanup_def', stack: '' },
  query: { sql: 'UPDATE categories SET cleanup_def = ? WHERE id = ?' },
};

const unrelatedMeta = {
  error: { message: 'UNIQUE constraint failed', stack: '' },
  query: { sql: 'INSERT INTO categories (id) VALUES (?)' },
};

describe('getDownloadError', () => {
  it('returns a version-mismatch message for missing-column schema errors', () => {
    expect(
      getDownloadError({ reason: 'invalid-schema', meta: schemaMismatchMeta }),
    ).toBe(SCHEMA_MISMATCH_MESSAGE);
  });

  it('returns a version-mismatch message for missing-table schema errors', () => {
    expect(
      getDownloadError({
        reason: 'invalid-schema',
        meta: { error: { message: 'no such table: foo', stack: '' } },
      }),
    ).toBe(SCHEMA_MISMATCH_MESSAGE);
  });

  it('does not report a schema mismatch for other invalid-schema failures', () => {
    expect(
      getDownloadError({ reason: 'invalid-schema', meta: unrelatedMeta }),
    ).not.toBe(SCHEMA_MISMATCH_MESSAGE);
  });

  it('does not report a schema mismatch when meta is missing', () => {
    expect(getDownloadError({ reason: 'invalid-schema' })).not.toBe(
      SCHEMA_MISMATCH_MESSAGE,
    );
  });
});

describe('getSyncError', () => {
  it('returns a version-mismatch message for missing-column schema errors', () => {
    expect(
      getSyncError('invalid-schema', 'budget-id', schemaMismatchMeta),
    ).toBe(SCHEMA_MISMATCH_MESSAGE);
  });

  it('does not report a schema mismatch for other invalid-schema failures', () => {
    expect(getSyncError('invalid-schema', 'budget-id', unrelatedMeta)).not.toBe(
      SCHEMA_MISMATCH_MESSAGE,
    );
  });

  it('does not report a schema mismatch when meta is missing', () => {
    expect(getSyncError('invalid-schema', 'budget-id')).not.toBe(
      SCHEMA_MISMATCH_MESSAGE,
    );
  });

  it('explains how to recover from an out-of-sync local budget', () => {
    expect(getSyncError('out-of-sync', 'budget-id')).toBe(
      'The local copy of budget "budget-id" is out of sync with the server. Delete the "budget-id" folder from your data directory and download the budget again (any changes not yet synced to the server will be lost).',
    );
  });

  it('falls back to the generic message for unknown errors', () => {
    expect(getSyncError('something-else', 'budget-id')).toBe(
      'We had an unknown problem opening "budget-id".',
    );
  });
});