The mobile GDPR export screen (app/backup/export.tsx) called backupService.exportData -> POST /compliance/export-data, an endpoint that never existed on the backend and always 404'd silently (same bug class as the web buttons fixed in 842be56e). Added requestDataExport/getDataExportStatus to backupService.ts and a new utils/dataExportFlow.ts (mirroring web/utils/dataExportFlow.ts) that drives the real async pipeline at /api/v1/data-export (request -> poll status -> download), confirmed live via backend/src/routes/dataExport.ts. Downloads the ready file to local storage with expo-file-system (already a dependency) and hands it to expo-sharing (newly added; no suitable dependency for file sharing already existed) so the user can save or share it via the OS share sheet. Collapsed the screen's fake JSON/CSV format picker to the one format the backend actually produces (JSON) and surfaces every failure (request, poll timeout, download, share) as a visible alert instead of failing silently. Added unit tests for the service calls and the export flow, plus a screen-level test for the button wiring and error paths. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
213 lines
8.1 KiB
TypeScript
213 lines
8.1 KiB
TypeScript
import * as FileSystem from 'expo-file-system/legacy';
|
|
import * as Sharing from 'expo-sharing';
|
|
import {
|
|
requestAndDownloadDataExport,
|
|
shareExportFile,
|
|
DataExportTimeoutError,
|
|
} from '../../utils/dataExportFlow';
|
|
import { backupService } from '../../services/backupService';
|
|
import { useAuthStore } from '../../store/authStore';
|
|
|
|
jest.mock('../../services/backupService');
|
|
jest.mock('../../store/authStore');
|
|
|
|
describe('dataExportFlow', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
jest.useRealTimers();
|
|
|
|
(useAuthStore.getState as jest.Mock) = jest.fn(() => ({
|
|
accessToken: 'test-access-token',
|
|
refreshAccessToken: jest.fn(),
|
|
}));
|
|
});
|
|
|
|
describe('requestAndDownloadDataExport', () => {
|
|
it('requests the export, polls until ready, then downloads the file', async () => {
|
|
(backupService.requestDataExport as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-abc', estimatedTime: '2-5 minutes', createdAt: 'now' },
|
|
});
|
|
|
|
(backupService.getDataExportStatus as jest.Mock)
|
|
.mockResolvedValueOnce({
|
|
success: true,
|
|
data: { exportId: 'export-abc', status: 'processing', progress: 20 },
|
|
})
|
|
.mockResolvedValueOnce({
|
|
success: true,
|
|
data: { exportId: 'export-abc', status: 'ready', progress: 100 },
|
|
});
|
|
|
|
jest.useFakeTimers();
|
|
const phases: string[] = [];
|
|
const resultPromise = requestAndDownloadDataExport((phase) => phases.push(phase));
|
|
await jest.advanceTimersByTimeAsync(3000);
|
|
const result = await resultPromise;
|
|
jest.useRealTimers();
|
|
|
|
expect(backupService.requestDataExport).toHaveBeenCalledTimes(1);
|
|
expect(backupService.getDataExportStatus).toHaveBeenCalledTimes(2);
|
|
expect(FileSystem.downloadAsync).toHaveBeenCalledWith(
|
|
expect.stringContaining('/data-export/export-abc/download'),
|
|
expect.stringContaining('export-abc'),
|
|
{ headers: { Authorization: 'Bearer test-access-token' } }
|
|
);
|
|
expect(result).toEqual({
|
|
exportId: 'export-abc',
|
|
fileUri: 'file:///mock-cache/mock-export.json',
|
|
});
|
|
expect(phases).toEqual(['requesting', 'processing', 'downloading', 'done']);
|
|
});
|
|
|
|
it('falls back to a refreshed token when there is no access token yet', async () => {
|
|
const refreshAccessToken = jest.fn().mockResolvedValue('refreshed-token');
|
|
(useAuthStore.getState as jest.Mock).mockReturnValue({
|
|
accessToken: null,
|
|
refreshAccessToken,
|
|
});
|
|
|
|
(backupService.requestDataExport as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-xyz', estimatedTime: '2-5 minutes', createdAt: 'now' },
|
|
});
|
|
(backupService.getDataExportStatus as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-xyz', status: 'ready', progress: 100 },
|
|
});
|
|
|
|
await requestAndDownloadDataExport();
|
|
|
|
expect(refreshAccessToken).toHaveBeenCalledTimes(1);
|
|
expect(FileSystem.downloadAsync).toHaveBeenCalledWith(
|
|
expect.any(String),
|
|
expect.any(String),
|
|
{ headers: { Authorization: 'Bearer refreshed-token' } }
|
|
);
|
|
});
|
|
|
|
it('throws when the user is not signed in and cannot refresh a token', async () => {
|
|
(useAuthStore.getState as jest.Mock).mockReturnValue({
|
|
accessToken: null,
|
|
refreshAccessToken: jest.fn().mockResolvedValue(null),
|
|
});
|
|
|
|
(backupService.requestDataExport as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-xyz', estimatedTime: '2-5 minutes', createdAt: 'now' },
|
|
});
|
|
(backupService.getDataExportStatus as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-xyz', status: 'ready', progress: 100 },
|
|
});
|
|
|
|
await expect(requestAndDownloadDataExport()).rejects.toThrow(
|
|
'You must be signed in to export your data.'
|
|
);
|
|
});
|
|
|
|
it('throws a user-visible error when the export fails server-side', async () => {
|
|
(backupService.requestDataExport as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-fail', estimatedTime: '2-5 minutes', createdAt: 'now' },
|
|
});
|
|
(backupService.getDataExportStatus as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-fail', status: 'failed', progress: 0 },
|
|
});
|
|
|
|
await expect(requestAndDownloadDataExport()).rejects.toThrow(
|
|
'Data export failed. Please try again.'
|
|
);
|
|
});
|
|
|
|
it('throws a user-visible error when the export expires before download', async () => {
|
|
(backupService.requestDataExport as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-expired', estimatedTime: '2-5 minutes', createdAt: 'now' },
|
|
});
|
|
(backupService.getDataExportStatus as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-expired', status: 'expired', progress: 100 },
|
|
});
|
|
|
|
await expect(requestAndDownloadDataExport()).rejects.toThrow(
|
|
'Data export expired before it could be downloaded. Please request a new one.'
|
|
);
|
|
});
|
|
|
|
it('propagates the request error without silently swallowing it', async () => {
|
|
(backupService.requestDataExport as jest.Mock).mockRejectedValue(
|
|
new Error('Network request failed')
|
|
);
|
|
|
|
await expect(requestAndDownloadDataExport()).rejects.toThrow('Network request failed');
|
|
expect(backupService.getDataExportStatus).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('throws DataExportTimeoutError when the export never becomes ready', async () => {
|
|
(backupService.requestDataExport as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-slow', estimatedTime: '2-5 minutes', createdAt: 'now' },
|
|
});
|
|
(backupService.getDataExportStatus as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-slow', status: 'processing', progress: 10 },
|
|
});
|
|
|
|
jest.useFakeTimers();
|
|
// Attach the rejection assertion synchronously, right when the promise
|
|
// is created, so Node never sees it as unhandled while the fake timers
|
|
// are advanced below.
|
|
const assertion = expect(requestAndDownloadDataExport()).rejects.toBeInstanceOf(
|
|
DataExportTimeoutError
|
|
);
|
|
// Let every poll's sleep() resolve without waiting real wall-clock time.
|
|
await jest.advanceTimersByTimeAsync(3000 * 61);
|
|
|
|
await assertion;
|
|
jest.useRealTimers();
|
|
}, 15000);
|
|
|
|
it('surfaces a non-2xx download status as a thrown error', async () => {
|
|
(backupService.requestDataExport as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-bad', estimatedTime: '2-5 minutes', createdAt: 'now' },
|
|
});
|
|
(backupService.getDataExportStatus as jest.Mock).mockResolvedValue({
|
|
success: true,
|
|
data: { exportId: 'export-bad', status: 'ready', progress: 100 },
|
|
});
|
|
(FileSystem.downloadAsync as jest.Mock).mockResolvedValueOnce({
|
|
uri: 'file:///mock-cache/export-bad.json',
|
|
status: 410,
|
|
});
|
|
|
|
await expect(requestAndDownloadDataExport()).rejects.toThrow(
|
|
'Failed to download export (status 410).'
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('shareExportFile', () => {
|
|
it('shares the downloaded file via the OS share sheet', async () => {
|
|
await shareExportFile('file:///mock-cache/mock-export.json');
|
|
|
|
expect(Sharing.isAvailableAsync).toHaveBeenCalledTimes(1);
|
|
expect(Sharing.shareAsync).toHaveBeenCalledWith('file:///mock-cache/mock-export.json', {
|
|
mimeType: 'application/json',
|
|
dialogTitle: 'Save or share your data export',
|
|
});
|
|
});
|
|
|
|
it('throws a user-visible error when sharing is unavailable on this device', async () => {
|
|
(Sharing.isAvailableAsync as jest.Mock).mockResolvedValueOnce(false);
|
|
|
|
await expect(shareExportFile('file:///mock-cache/mock-export.json')).rejects.toThrow(
|
|
'Sharing is not available on this device. The file was saved to app storage.'
|
|
);
|
|
expect(Sharing.shareAsync).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|