Files
LivingDexTracker/tests/unit/pokedexExportService.test.ts
T
Josh Creek fb95b43b30 fix(backup): pause revoked backups and tell users to reconnect
Google answers a revoked or expired refresh token with invalid_grant.
Every save then retried the dead token, and reconnecting never cleared
the old error, so it kept showing on Backup Settings afterwards.

- The Google Drive and Dropbox OAuth callbacks clear lastError when a
  provider is reconnected.
- An invalid_grant, or a missing refresh token, now pauses the
  integration with a readable "reconnect" message instead of retrying
  it on every catch update. Other failures still retry as before.
- A banner on every page and an alert on the Pokédex page point to
  Backup Settings, which shows a "Reconnect needed" badge. The Pokédex
  page re-checks backup status after each export, because saving a
  catch record also exports on the server and may pause a provider
  first.
- Offline sync status and the "Save all artwork" link move from every
  page to a new /offline-guide page, linked from the user menu and the
  home and welcome pages. Only the offline read-only banner stays
  sitewide.
- Unit tests cover every export path and the backup status store. BDD
  covers revocation and reconnecting for both providers, and the
  offline guide. The mock provider can now reject token refreshes, and
  mock control calls fail loudly if a stale mock is reused. Coverage
  thresholds are raised to the new baseline.
2026-09-14 18:46:09 +01:00

85 lines
2.6 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import {
buildCsv,
csvEscape,
isRevokedGrant,
sanitizeFileName,
shouldRefreshToken
} from '$lib/services/PokedexExportFormatting';
describe('Pokédex export formatting', () => {
it.each([
[null, ''],
[undefined, ''],
['plain', 'plain'],
['comma,value', '"comma,value"'],
['a "quote"', '"a ""quote"""'],
['two\nlines', '"two\nlines"']
])('escapes CSV value %j', (value, expected) => {
expect(csvEscape(value)).toBe(expected);
});
it('sanitizes provider filenames while preserving a CSV suffix', () => {
expect(sanitizeFileName(' My: Dex? ', 'fallback')).toBe('My- Dex-.csv');
expect(sanitizeFileName('already.csv', 'fallback')).toBe('already.csv');
expect(sanitizeFileName('***', 'fallback')).toBe('-.csv');
expect(sanitizeFileName(' ', 'fallback')).toBe('fallback');
});
it('builds a stable, escaped CSV with defaults for missing catch records', () => {
const csv = buildCsv([
{
pokedexEntry: {
_id: '25',
pokedexNumber: 25,
pokemon: 'Pikachu',
form: null
},
catchRecord: {
caught: true,
haveToEvolve: false,
inHome: true,
hasGigantamaxed: false,
personalNotes: 'Comma, and "quote"'
}
},
{
pokedexEntry: {
_id: '26',
pokedexNumber: 26,
pokemon: 'Raichu',
form: 'Alolan'
},
catchRecord: null
}
] as never);
expect(csv.split('\r\n')).toEqual([
'pokemonId,pokedexNumber,pokemon,form,caught,haveToEvolve,inHome,personalNotes',
'25,25,Pikachu,,true,false,true,"Comma, and ""quote"""',
'26,26,Raichu,Alolan,false,false,false,'
]);
});
it('recognises a revoked or expired refresh token', () => {
expect(isRevokedGrant(400, '{"error":"invalid_grant","error_description":"Bad Request"}')).toBe(
true
);
expect(isRevokedGrant(401, '{"error":"invalid_grant"}')).toBe(true);
expect(isRevokedGrant(400, '{"error":"invalid_client"}')).toBe(false);
expect(isRevokedGrant(500, '{"error":"invalid_grant"}')).toBe(false);
expect(isRevokedGrant(400, 'Bad Request')).toBe(false);
expect(isRevokedGrant(400, 'null')).toBe(false);
});
it('refreshes only finite expiries within the next minute', () => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-09-13T12:00:00Z'));
expect(shouldRefreshToken(null)).toBe(false);
expect(shouldRefreshToken('not-a-date')).toBe(false);
expect(shouldRefreshToken('2026-09-13T12:02:00Z')).toBe(false);
expect(shouldRefreshToken('2026-09-13T12:00:30Z')).toBe(true);
expect(shouldRefreshToken('2026-09-13T11:59:00Z')).toBe(true);
vi.useRealTimers();
});
});