@openpeepshq/rn-components 0.21.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/components/custom/modals/media/audio-picker-sheet.test.tsx +913 -0
- package/src/components/custom/modals/media/audio-picker-sheet.tsx +181 -49
- package/src/components/custom/modals/media/record-audio-sheet.test.tsx +626 -0
- package/src/components/custom/modals/media/record-audio-sheet.tsx +335 -111
- package/src/lib/downloadFile.ts +13 -6
- package/src/lib/uploadMedia.test.ts +190 -0
- package/src/lib/uploadMedia.ts +55 -0
- package/src/pages/settings/Account.test.tsx +303 -0
- package/src/pages/settings/Account.tsx +70 -3
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Focused unit coverage for the `uploadMedia` audio branch (#327, review item
|
|
3
|
+
* 1). The rest of the feature leans on this branch's honest name/mimetype logic
|
|
4
|
+
* (recorded → trusted .m4a/audio/mp4; picked → keep the real container), but it
|
|
5
|
+
* was previously only exercised through a mocked-away `uploadMedia`. Here the
|
|
6
|
+
* real `uploadMedia` runs; only the native modules it touches (RNFS, the asset
|
|
7
|
+
* converter, Toast) are mocked.
|
|
8
|
+
*/
|
|
9
|
+
const fsExists = jest.fn(async () => false);
|
|
10
|
+
const fsStat = jest.fn(async () => ({ size: 4096 }));
|
|
11
|
+
const fsCopyFile = jest.fn(async () => undefined);
|
|
12
|
+
const copyAssetsFileIOS = jest.fn(async () => undefined);
|
|
13
|
+
|
|
14
|
+
jest.mock('react-native-fs', () => ({
|
|
15
|
+
TemporaryDirectoryPath: '/tmp/',
|
|
16
|
+
CachesDirectoryPath: '/cache/',
|
|
17
|
+
exists: (p: string) => fsExists(p),
|
|
18
|
+
stat: (p: string) => fsStat(p),
|
|
19
|
+
copyFile: (a: string, b: string) => fsCopyFile(a, b),
|
|
20
|
+
copyAssetsFileIOS: (...a: unknown[]) => copyAssetsFileIOS(...a),
|
|
21
|
+
readFile: jest.fn(async () => ''),
|
|
22
|
+
writeFile: jest.fn(async () => undefined),
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
jest.mock('react-native-convert-ph-asset', () => ({
|
|
26
|
+
__esModule: true,
|
|
27
|
+
default: { convertVideoFromUrl: jest.fn() },
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
jest.mock('react-native-toast-message', () => ({
|
|
31
|
+
__esModule: true,
|
|
32
|
+
default: { show: jest.fn() },
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
// `uploadMedia` pulls in `../i18n/index`, whose load-time `initI18nOnce()`
|
|
36
|
+
// references the RN `__DEV__` global — so the i18n module is mocked (it is only
|
|
37
|
+
// touched in the error path, which these tests do not exercise).
|
|
38
|
+
jest.mock('../i18n/index', () => ({
|
|
39
|
+
__esModule: true,
|
|
40
|
+
default: { t: (key: string) => key },
|
|
41
|
+
}));
|
|
42
|
+
|
|
43
|
+
// `mediaUriHelpers` (react-native) is the only other RN-specific runtime —
|
|
44
|
+
// mocked so the native runtime is never required under Jest's Node environment.
|
|
45
|
+
jest.mock('react-native', () => ({
|
|
46
|
+
Platform: { OS: 'android' },
|
|
47
|
+
InteractionManager: {
|
|
48
|
+
runAfterInteractions: (cb: () => void) => {
|
|
49
|
+
cb();
|
|
50
|
+
return {
|
|
51
|
+
then: () => undefined,
|
|
52
|
+
done: () => undefined,
|
|
53
|
+
cancel: () => undefined,
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
jest.mock('react-native-config', () => ({
|
|
60
|
+
__esModule: true,
|
|
61
|
+
default: {
|
|
62
|
+
BASE_URL: '',
|
|
63
|
+
APP_ENV: 'test',
|
|
64
|
+
LOGIN_EMAIL: '',
|
|
65
|
+
LOGIN_PASSWORD: '',
|
|
66
|
+
},
|
|
67
|
+
}));
|
|
68
|
+
|
|
69
|
+
import { uploadMedia, AUDIO_EXTENSION_MIME } from './uploadMedia';
|
|
70
|
+
|
|
71
|
+
// Web's `ComposeAttachments` gates an uploaded file as "audio" by this
|
|
72
|
+
// extension set (AUDIO_NAME). Every container web classifies as audio must be
|
|
73
|
+
// covered by RN's picked-file map, or the same upload would carry a
|
|
74
|
+
// .bin/octet-stream name+mimetype on RN but a correct audio one on web.
|
|
75
|
+
// (RN's map may additionally hold `mp4` — an MPEG-4 audio container — which
|
|
76
|
+
// web routes to video; that superset entry is intentional and not checked here.)
|
|
77
|
+
const WEB_AUDIO_EXTENSIONS = [
|
|
78
|
+
'mp3',
|
|
79
|
+
'wav',
|
|
80
|
+
'ogg',
|
|
81
|
+
'oga',
|
|
82
|
+
'm4a',
|
|
83
|
+
'aac',
|
|
84
|
+
'flac',
|
|
85
|
+
'opus',
|
|
86
|
+
'weba',
|
|
87
|
+
];
|
|
88
|
+
|
|
89
|
+
const captured = jest.fn();
|
|
90
|
+
const createAttachments = jest.fn(async (...args: unknown[]) => {
|
|
91
|
+
captured(args[0]);
|
|
92
|
+
return {
|
|
93
|
+
id: 'att-1',
|
|
94
|
+
type: 'audio',
|
|
95
|
+
url: 'https://cdn/att',
|
|
96
|
+
previewUrl: null,
|
|
97
|
+
filename: null,
|
|
98
|
+
meta: { usage: 'attachment', mimetype: 'x' },
|
|
99
|
+
description: 'audio',
|
|
100
|
+
blurhash: null,
|
|
101
|
+
status: 'ready',
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const callUpload = (
|
|
106
|
+
mediaUri: string,
|
|
107
|
+
opts: {
|
|
108
|
+
name?: string | null;
|
|
109
|
+
mimeType?: string | null;
|
|
110
|
+
isRecordedAudio?: boolean;
|
|
111
|
+
} = {}
|
|
112
|
+
) =>
|
|
113
|
+
uploadMedia({
|
|
114
|
+
mediaUri,
|
|
115
|
+
createAttachments,
|
|
116
|
+
type: 'audio',
|
|
117
|
+
usage: 'attachment',
|
|
118
|
+
alt: 'audio',
|
|
119
|
+
name: opts.name ?? null,
|
|
120
|
+
mimeType: opts.mimeType ?? null,
|
|
121
|
+
isRecordedAudio: opts.isRecordedAudio,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const sentFile = () => (captured as unknown as jest.Mock).mock.calls[0][0].file;
|
|
125
|
+
const sentName = () => sentFile().name as string;
|
|
126
|
+
const sentMime = () => sentFile().type as string;
|
|
127
|
+
|
|
128
|
+
beforeEach(() => {
|
|
129
|
+
captured.mockClear();
|
|
130
|
+
createAttachments.mockClear();
|
|
131
|
+
fsExists.mockClear();
|
|
132
|
+
fsStat.mockClear();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe('uploadMedia audio branch', () => {
|
|
136
|
+
it('records a voice note as a trusted .m4a / audio/mp4 (no extension guess)', async () => {
|
|
137
|
+
await callUpload('file:///tmp/rec_123.m4a', { isRecordedAudio: true });
|
|
138
|
+
expect(sentName()).toMatch(/\.m4a$/);
|
|
139
|
+
expect(sentMime()).toBe('audio/mp4');
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it.each([
|
|
143
|
+
['mp3', 'audio/mpeg'],
|
|
144
|
+
['ogg', 'audio/ogg'],
|
|
145
|
+
['wav', 'audio/wav'],
|
|
146
|
+
['flac', 'audio/flac'],
|
|
147
|
+
['m4a', 'audio/mp4'],
|
|
148
|
+
['aac', 'audio/aac'],
|
|
149
|
+
])(
|
|
150
|
+
'a picked .%s file keeps its extension and honest mime',
|
|
151
|
+
async (ext, mime) => {
|
|
152
|
+
await callUpload(`file:///tmp/pick_${Date.now()}.${ext}`);
|
|
153
|
+
expect(sentName()).toMatch(new RegExp(`\\.${ext}$`));
|
|
154
|
+
expect(sentMime()).toBe(mime);
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
it('degrades a content:// URI with no extension to .bin / octet-stream', async () => {
|
|
159
|
+
await callUpload('content://media/external/audio/media/123');
|
|
160
|
+
expect(sentName()).toMatch(/\.bin$/);
|
|
161
|
+
expect(sentMime()).toBe('application/octet-stream');
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('lets an explicit mime override the extension inference for picked files', async () => {
|
|
165
|
+
await callUpload('file:///tmp/pick.ogg', { mimeType: 'audio/ogg' });
|
|
166
|
+
expect(sentName()).toMatch(/\.ogg$/);
|
|
167
|
+
expect(sentMime()).toBe('audio/ogg');
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('classifies an out-of-map container as audio from the picker-reported name+mime (no .bin fallthrough)', async () => {
|
|
171
|
+
// Round-18 item 2: an opaque content:// URI whose container is not in
|
|
172
|
+
// AUDIO_EXTENSION_MIME (audio .webm) used to degrade to .bin/
|
|
173
|
+
// application/octet-stream on RN — which the server classifies as a
|
|
174
|
+
// document — while the same file picked on web (file.type audio/webm)
|
|
175
|
+
// becomes a post-audio attachment. The document picker's authoritative
|
|
176
|
+
// name/type now carry the classification across.
|
|
177
|
+
await callUpload('content://media/external/audio/media/777', {
|
|
178
|
+
name: 'track.webm',
|
|
179
|
+
mimeType: 'audio/webm',
|
|
180
|
+
});
|
|
181
|
+
expect(sentName()).toMatch(/\.webm$/);
|
|
182
|
+
expect(sentMime()).toBe('audio/webm');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('covers every web-audio extension in AUDIO_EXTENSION_MIME', () => {
|
|
186
|
+
for (const ext of WEB_AUDIO_EXTENSIONS) {
|
|
187
|
+
expect(AUDIO_EXTENSION_MIME[ext]).toMatch(/^audio\//);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
});
|
package/src/lib/uploadMedia.ts
CHANGED
|
@@ -13,6 +13,23 @@ type UploadProgressInfo = {
|
|
|
13
13
|
estimatedRemainingMs?: number;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
// Audio containers the file picker can return, mapped to honest mimetypes for
|
|
17
|
+
// the picked-file branch of `type: 'audio'` (recordings never go through this
|
|
18
|
+
// table — the recorder's .m4a/AAC output is trusted directly). Exported so the
|
|
19
|
+
// unit suite can pin it against web's `ComposeAttachments` audio extensions.
|
|
20
|
+
export const AUDIO_EXTENSION_MIME: Record<string, string> = {
|
|
21
|
+
m4a: 'audio/mp4',
|
|
22
|
+
mp4: 'audio/mp4',
|
|
23
|
+
aac: 'audio/aac',
|
|
24
|
+
mp3: 'audio/mpeg',
|
|
25
|
+
ogg: 'audio/ogg',
|
|
26
|
+
oga: 'audio/ogg',
|
|
27
|
+
opus: 'audio/opus',
|
|
28
|
+
wav: 'audio/wav',
|
|
29
|
+
flac: 'audio/flac',
|
|
30
|
+
weba: 'audio/webm',
|
|
31
|
+
};
|
|
32
|
+
|
|
16
33
|
export type UploadProgressMap = {
|
|
17
34
|
[key: string]: { percent: number; estimatedRemainingMs?: number };
|
|
18
35
|
};
|
|
@@ -32,6 +49,12 @@ type UploadMediaType = {
|
|
|
32
49
|
name?: string | null;
|
|
33
50
|
mimeType?: string | null;
|
|
34
51
|
setUploadProgress?: React.Dispatch<React.SetStateAction<UploadProgressMap>>;
|
|
52
|
+
/**
|
|
53
|
+
* The URI is the recorder's own output (a fresh .m4a), not a file the user
|
|
54
|
+
* picked. Only then is the m4a/AAC name+mimetype trusted instead of the
|
|
55
|
+
* URI-based inference a picked file must keep honest.
|
|
56
|
+
*/
|
|
57
|
+
isRecordedAudio?: boolean;
|
|
35
58
|
};
|
|
36
59
|
|
|
37
60
|
export const uploadMedia = async ({
|
|
@@ -43,6 +66,7 @@ export const uploadMedia = async ({
|
|
|
43
66
|
name,
|
|
44
67
|
mimeType,
|
|
45
68
|
setUploadProgress,
|
|
69
|
+
isRecordedAudio,
|
|
46
70
|
}: UploadMediaType) => {
|
|
47
71
|
const decodedMediaUri = decodeFileUri(mediaUri);
|
|
48
72
|
let finalUri = decodedMediaUri;
|
|
@@ -64,6 +88,37 @@ export const uploadMedia = async ({
|
|
|
64
88
|
}
|
|
65
89
|
}
|
|
66
90
|
finalMimeType = resolveDocumentMime(mimeType, name);
|
|
91
|
+
} else if (type === 'audio') {
|
|
92
|
+
if (isRecordedAudio) {
|
|
93
|
+
// The recorder emits .m4a (AAC-in-MP4), so the known output format is
|
|
94
|
+
// trusted instead of guessing the mime from the extension (a fragile
|
|
95
|
+
// `uri.split('.')` inference) — keeps the filename and mimetype honest
|
|
96
|
+
// without a .bin / octet-stream fallback. Full rationale in
|
|
97
|
+
// record-audio-sheet.tsx (onStartRecord).
|
|
98
|
+
fileExtension = 'm4a';
|
|
99
|
+
finalMimeType = 'audio/mp4';
|
|
100
|
+
} else {
|
|
101
|
+
// Picked files keep whatever container the user chose — their bytes are
|
|
102
|
+
// not transcoded on the client, so the reported name/mimetype must match
|
|
103
|
+
// the on-disk content (the server normalizes them to m4a later). The
|
|
104
|
+
// extension is inferred from the URI's last path segment (picked files
|
|
105
|
+
// carry no `name`), so an opaque content:// URI degrades to
|
|
106
|
+
// .bin/octet-stream and the server sniffs it.
|
|
107
|
+
const source = name || decodedMediaUri;
|
|
108
|
+
const lastSegment = source.split('/').pop() ?? '';
|
|
109
|
+
const dot = lastSegment.lastIndexOf('.');
|
|
110
|
+
const pickedExt =
|
|
111
|
+
dot > 0 && dot < lastSegment.length - 1
|
|
112
|
+
? lastSegment.slice(dot + 1).toLowerCase()
|
|
113
|
+
: undefined;
|
|
114
|
+
if (pickedExt) {
|
|
115
|
+
fileExtension = pickedExt;
|
|
116
|
+
}
|
|
117
|
+
finalMimeType =
|
|
118
|
+
mimeType && mimeType.includes('/')
|
|
119
|
+
? mimeType
|
|
120
|
+
: (AUDIO_EXTENSION_MIME[fileExtension] ?? 'application/octet-stream');
|
|
121
|
+
}
|
|
67
122
|
}
|
|
68
123
|
|
|
69
124
|
const tempFileName = `${Date.now()}_${Math.random().toString(36).substring(2, 9)}.${fileExtension}`;
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression tests for the RN account export flow (issue #237 round-8
|
|
3
|
+
* review S1/S3). The page hands the shared `useAccountExport` hook an
|
|
4
|
+
* external `downloadDocument` step; a failed, expired, or handed-off-failed
|
|
5
|
+
* export must not be silent on RN — the hook's `status` has to surface as a
|
|
6
|
+
* toast, and the handoff must receive the resolved download URL.
|
|
7
|
+
*
|
|
8
|
+
* The hook under test is the real react-package implementation (required
|
|
9
|
+
* directly in the `@openpeepshq/react` mock, so the ESM-only transitive deps
|
|
10
|
+
* of the react barrel stay out of Jest's runtime).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const React = require('react');
|
|
14
|
+
const ReactTestRenderer = require('react-test-renderer');
|
|
15
|
+
|
|
16
|
+
// Fake export job client. The status queue mirrors the react-package test:
|
|
17
|
+
// each poll pops the next payload, the last repeats when exhausted.
|
|
18
|
+
const start = jest.fn(async () => ({ data: { jobId: 'job-1' } }));
|
|
19
|
+
const status = jest.fn();
|
|
20
|
+
const downloadUrl = jest.fn(async () => 'https://x/export?token=tok');
|
|
21
|
+
const statusResults: Array<Record<string, unknown>> = [];
|
|
22
|
+
const downloadDocument = jest.fn();
|
|
23
|
+
const toastShow = jest.fn();
|
|
24
|
+
|
|
25
|
+
jest.mock('react-native', () => ({
|
|
26
|
+
View: (props: { children?: unknown }) =>
|
|
27
|
+
React.createElement('View', null, props.children),
|
|
28
|
+
ActivityIndicator: () => null,
|
|
29
|
+
Linking: {
|
|
30
|
+
canOpenURL: jest.fn(async () => true),
|
|
31
|
+
openURL: jest.fn(async () => undefined),
|
|
32
|
+
},
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
jest.mock('react-i18next', () => ({
|
|
36
|
+
useTranslation: () => ({
|
|
37
|
+
t: (key: string, opts?: { defaultValue?: string }) =>
|
|
38
|
+
opts?.defaultValue ?? key,
|
|
39
|
+
}),
|
|
40
|
+
}));
|
|
41
|
+
|
|
42
|
+
jest.mock('react-native-keyboard-aware-scroll-view', () => ({
|
|
43
|
+
KeyboardAwareScrollView: (props: { children?: unknown }) =>
|
|
44
|
+
React.createElement('KeyboardAwareScrollView', null, props.children),
|
|
45
|
+
}));
|
|
46
|
+
|
|
47
|
+
jest.mock('react-native-toast-message', () => ({
|
|
48
|
+
__esModule: true,
|
|
49
|
+
default: { show: toastShow },
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
jest.mock('@openpeepshq/react', () => ({
|
|
53
|
+
useOpenpeeps: () => ({
|
|
54
|
+
openpeepsApi: {
|
|
55
|
+
useCurrentAccount: () => ({ data: undefined }),
|
|
56
|
+
logoutAction: jest.fn(),
|
|
57
|
+
updateCurrentAccountAction: jest.fn(),
|
|
58
|
+
validationEmailAction: jest.fn(),
|
|
59
|
+
},
|
|
60
|
+
queryClient: { invalidateQueries: jest.fn() },
|
|
61
|
+
client: {
|
|
62
|
+
accounts: {
|
|
63
|
+
current: {
|
|
64
|
+
export: {
|
|
65
|
+
start: () => start(),
|
|
66
|
+
status: (opts: unknown) => {
|
|
67
|
+
status(opts);
|
|
68
|
+
const next = statusResults.shift() ?? statusResults[0];
|
|
69
|
+
return Promise.resolve(next ? { data: next } : undefined);
|
|
70
|
+
},
|
|
71
|
+
downloadUrl: (token: string) => downloadUrl(token),
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
// Real hook, straight from the react package source.
|
|
78
|
+
useAccountExport:
|
|
79
|
+
require('../../../../react/src/hooks/settings/useAccountExport')
|
|
80
|
+
.useAccountExport,
|
|
81
|
+
}));
|
|
82
|
+
|
|
83
|
+
jest.mock('@openpeepshq/common', () => ({
|
|
84
|
+
Account: 'Account',
|
|
85
|
+
}));
|
|
86
|
+
|
|
87
|
+
jest.mock('react-hook-form', () => ({
|
|
88
|
+
useForm: () => ({
|
|
89
|
+
getValues: () => ({
|
|
90
|
+
email: 'a@b.c',
|
|
91
|
+
oldPassword: '',
|
|
92
|
+
newPassword: undefined,
|
|
93
|
+
confirmPassword: undefined,
|
|
94
|
+
}),
|
|
95
|
+
setValue: jest.fn(),
|
|
96
|
+
control: {},
|
|
97
|
+
}),
|
|
98
|
+
}));
|
|
99
|
+
|
|
100
|
+
jest.mock('../../lib/downloadFile', () => ({
|
|
101
|
+
downloadDocument,
|
|
102
|
+
}));
|
|
103
|
+
|
|
104
|
+
jest.mock('../../components/ui/themed-view', () => ({
|
|
105
|
+
ThemedView: (props: { children?: unknown }) =>
|
|
106
|
+
React.createElement('ThemedView', null, props.children),
|
|
107
|
+
}));
|
|
108
|
+
|
|
109
|
+
jest.mock('../../components/ui/themed-text', () => ({
|
|
110
|
+
ThemedText: (props: { children?: unknown }) =>
|
|
111
|
+
React.createElement('ThemedText', null, props.children),
|
|
112
|
+
}));
|
|
113
|
+
|
|
114
|
+
jest.mock('../../components/ui/form', () => ({
|
|
115
|
+
Form: (props: { children?: unknown }) =>
|
|
116
|
+
React.createElement('Form', null, props.children),
|
|
117
|
+
FormField: (props: {
|
|
118
|
+
render?: (p: { field: { value: string } }) => unknown;
|
|
119
|
+
}) => (props.render ? props.render({ field: { value: '' } }) : null),
|
|
120
|
+
FormInput: () => null,
|
|
121
|
+
}));
|
|
122
|
+
|
|
123
|
+
jest.mock('../../components/ui/button', () => ({
|
|
124
|
+
Button: (props: Record<string, unknown>) =>
|
|
125
|
+
React.createElement('Button', props),
|
|
126
|
+
}));
|
|
127
|
+
|
|
128
|
+
jest.mock('../../components/layout', () => ({
|
|
129
|
+
HeaderActionButton: () => null,
|
|
130
|
+
useSetPageHeader: () => undefined,
|
|
131
|
+
}));
|
|
132
|
+
|
|
133
|
+
import { AccountSettings } from './Account';
|
|
134
|
+
|
|
135
|
+
type Instance = {
|
|
136
|
+
type?: unknown;
|
|
137
|
+
props?: Record<string, unknown>;
|
|
138
|
+
children?: Array<unknown>;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
let tree: ReturnType<typeof ReactTestRenderer.create> | undefined;
|
|
142
|
+
|
|
143
|
+
const renderPage = () => {
|
|
144
|
+
ReactTestRenderer.act(() => {
|
|
145
|
+
tree = ReactTestRenderer.create(
|
|
146
|
+
React.createElement(AccountSettings, { navigation: {} } as never)
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const advance = async (ms: number) => {
|
|
152
|
+
await ReactTestRenderer.act(async () => {
|
|
153
|
+
await jest.advanceTimersByTimeAsync(ms);
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const toasts = () =>
|
|
158
|
+
toastShow.mock.calls.map((c: Array<Record<string, unknown>>) => c[0]);
|
|
159
|
+
|
|
160
|
+
const textCount = (text: string) =>
|
|
161
|
+
tree!.root.findAll(
|
|
162
|
+
(node: Instance) =>
|
|
163
|
+
node.type === 'ThemedText' && node.children?.includes(text)
|
|
164
|
+
).length;
|
|
165
|
+
|
|
166
|
+
// The export button (not the email-verify one): the Button whose subtree
|
|
167
|
+
// carries the idle "Download" label.
|
|
168
|
+
const downloadButton = (): Instance => {
|
|
169
|
+
const buttons = tree!.root.findAll(
|
|
170
|
+
(node: Instance) =>
|
|
171
|
+
node.type === 'Button' && typeof node.props?.onPress === 'function'
|
|
172
|
+
) as Array<
|
|
173
|
+
Instance & {
|
|
174
|
+
findAll: (pred: (n: Instance) => boolean) => Instance[];
|
|
175
|
+
}
|
|
176
|
+
>;
|
|
177
|
+
const btn = buttons.find(
|
|
178
|
+
(b) =>
|
|
179
|
+
b.findAll(
|
|
180
|
+
(n: Instance) =>
|
|
181
|
+
n.type === 'ThemedText' && n.children?.includes('Download')
|
|
182
|
+
).length > 0
|
|
183
|
+
);
|
|
184
|
+
if (!btn) throw new Error('download button not found');
|
|
185
|
+
return btn;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
beforeEach(() => {
|
|
189
|
+
(
|
|
190
|
+
globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
|
|
191
|
+
).IS_REACT_ACT_ENVIRONMENT = true;
|
|
192
|
+
jest.useFakeTimers();
|
|
193
|
+
start.mockClear();
|
|
194
|
+
status.mockClear();
|
|
195
|
+
downloadUrl.mockClear().mockResolvedValue('https://x/export?token=tok');
|
|
196
|
+
statusResults.length = 0;
|
|
197
|
+
downloadDocument.mockClear();
|
|
198
|
+
toastShow.mockClear();
|
|
199
|
+
tree = undefined;
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
afterEach(() => {
|
|
203
|
+
if (tree) {
|
|
204
|
+
ReactTestRenderer.act(() => {
|
|
205
|
+
tree!.unmount();
|
|
206
|
+
});
|
|
207
|
+
tree = undefined;
|
|
208
|
+
}
|
|
209
|
+
jest.useRealTimers();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('RN account export flow (issue #237 round-8 S1/S3)', () => {
|
|
213
|
+
it('hands the resolved download URL to the external handler', async () => {
|
|
214
|
+
statusResults.push(
|
|
215
|
+
{ state: 'building' },
|
|
216
|
+
{ state: 'ready', downloadToken: 'tok-1' }
|
|
217
|
+
);
|
|
218
|
+
downloadDocument.mockResolvedValue(true);
|
|
219
|
+
renderPage();
|
|
220
|
+
|
|
221
|
+
await ReactTestRenderer.act(async () => {
|
|
222
|
+
await downloadButton().props!.onPress!();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
await advance(2001); // first poll: building
|
|
226
|
+
await advance(2001); // second poll: ready → hand off
|
|
227
|
+
expect(downloadUrl).toHaveBeenCalledWith('tok-1');
|
|
228
|
+
expect(downloadDocument).toHaveBeenCalledWith('https://x/export?token=tok');
|
|
229
|
+
expect(toasts()).toHaveLength(0);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('toasts when the export job fails (status error)', async () => {
|
|
233
|
+
statusResults.push({ state: 'error', error: 'boom' });
|
|
234
|
+
renderPage();
|
|
235
|
+
|
|
236
|
+
await ReactTestRenderer.act(async () => {
|
|
237
|
+
await downloadButton().props!.onPress!();
|
|
238
|
+
});
|
|
239
|
+
await advance(2001);
|
|
240
|
+
|
|
241
|
+
expect(toasts()).toEqual([{ type: 'error', text1: 'Failed: boom' }]);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('toasts when the external download handoff fails', async () => {
|
|
245
|
+
statusResults.push({ state: 'ready', downloadToken: 'tok-1' });
|
|
246
|
+
downloadDocument.mockResolvedValue(false);
|
|
247
|
+
renderPage();
|
|
248
|
+
|
|
249
|
+
await ReactTestRenderer.act(async () => {
|
|
250
|
+
await downloadButton().props!.onPress!();
|
|
251
|
+
});
|
|
252
|
+
await advance(2001);
|
|
253
|
+
|
|
254
|
+
expect(downloadDocument).toHaveBeenCalled();
|
|
255
|
+
expect(toasts()).toEqual([
|
|
256
|
+
{
|
|
257
|
+
type: 'error',
|
|
258
|
+
text1:
|
|
259
|
+
'Failed: No app was found to download the file. Please try again.',
|
|
260
|
+
},
|
|
261
|
+
]);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('toasts when polling outlives the deadline without a result', async () => {
|
|
265
|
+
// Only `building` is ever queued (450 = the polls inside the 15-minute
|
|
266
|
+
// deadline at the 2 s cadence), so the loop runs until the deadline.
|
|
267
|
+
statusResults.push(
|
|
268
|
+
...Array.from({ length: 450 }, () => ({ state: 'building' }))
|
|
269
|
+
);
|
|
270
|
+
renderPage();
|
|
271
|
+
|
|
272
|
+
await ReactTestRenderer.act(async () => {
|
|
273
|
+
await downloadButton().props!.onPress!();
|
|
274
|
+
});
|
|
275
|
+
// One jump past the 15-minute deadline (fake timers make it cheap).
|
|
276
|
+
await advance(15 * 60 * 1000 + 1);
|
|
277
|
+
|
|
278
|
+
expect(status).toHaveBeenCalledTimes(450);
|
|
279
|
+
expect(toasts()).toEqual([
|
|
280
|
+
{
|
|
281
|
+
type: 'error',
|
|
282
|
+
text1: 'Failed: The export did not finish in time. Please try again.',
|
|
283
|
+
},
|
|
284
|
+
]);
|
|
285
|
+
// The button is back to its idle label once polling stopped.
|
|
286
|
+
expect(textCount('Preparing your export…')).toBe(0);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('shows the preparing label while the export is running', async () => {
|
|
290
|
+
statusResults.push(
|
|
291
|
+
...Array.from({ length: 450 }, () => ({ state: 'building' }))
|
|
292
|
+
);
|
|
293
|
+
renderPage();
|
|
294
|
+
expect(textCount('Preparing your export…')).toBe(0);
|
|
295
|
+
|
|
296
|
+
await ReactTestRenderer.act(async () => {
|
|
297
|
+
await downloadButton().props!.onPress!();
|
|
298
|
+
});
|
|
299
|
+
expect(textCount('Preparing your export…')).toBe(1);
|
|
300
|
+
await advance(15 * 60 * 1000 + 1); // let the deadline stop polling
|
|
301
|
+
expect(textCount('Preparing your export…')).toBe(0);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MainScreenProps } from '../../components/navigation/types/index';
|
|
2
|
-
import { useOpenpeeps } from '@openpeepshq/react';
|
|
2
|
+
import { useOpenpeeps, useAccountExport } from '@openpeepshq/react';
|
|
3
3
|
import { HeaderActionButton, useSetPageHeader } from '../../components/layout';
|
|
4
|
-
import React, { useEffect, useState } from 'react';
|
|
4
|
+
import React, { useCallback, useEffect, useState } from 'react';
|
|
5
5
|
import { ThemedText } from '../../components/ui/themed-text';
|
|
6
6
|
import { ThemedView } from '../../components/ui/themed-view';
|
|
7
7
|
import { useForm } from 'react-hook-form';
|
|
@@ -12,6 +12,7 @@ import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view
|
|
|
12
12
|
import { Account } from '@openpeepshq/common';
|
|
13
13
|
import { ActivityIndicator, View } from 'react-native';
|
|
14
14
|
import { useTranslation } from 'react-i18next';
|
|
15
|
+
import { downloadDocument } from '../../lib/downloadFile';
|
|
15
16
|
|
|
16
17
|
type AccountSettingsProps = MainScreenProps<'AccountSettings'>;
|
|
17
18
|
|
|
@@ -23,7 +24,7 @@ type AccountFormValues = {
|
|
|
23
24
|
};
|
|
24
25
|
|
|
25
26
|
export const AccountSettings: React.FC<AccountSettingsProps> = () => {
|
|
26
|
-
const { openpeepsApi, queryClient } = useOpenpeeps();
|
|
27
|
+
const { openpeepsApi, queryClient, client } = useOpenpeeps();
|
|
27
28
|
const { data: currentAccount } = openpeepsApi.useCurrentAccount();
|
|
28
29
|
const logout = openpeepsApi.logoutAction();
|
|
29
30
|
const [isOldPasswordVisible, setIsOldPasswordVisible] = React.useState(false);
|
|
@@ -36,6 +37,40 @@ export const AccountSettings: React.FC<AccountSettingsProps> = () => {
|
|
|
36
37
|
const [verifying, setVerifying] = useState(false);
|
|
37
38
|
const { t } = useTranslation();
|
|
38
39
|
|
|
40
|
+
// RN has no fetch/blob, so the shared hook's web download step is replaced
|
|
41
|
+
// with an external handler. `download` must stay stable across renders:
|
|
42
|
+
// the hook closes over it in its memoized run callback.
|
|
43
|
+
const download = useCallback(
|
|
44
|
+
async (url: string) => {
|
|
45
|
+
const opened = await downloadDocument(url);
|
|
46
|
+
if (!opened) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
t('settings.account.exportDownloadFailed', {
|
|
49
|
+
defaultValue:
|
|
50
|
+
'No app was found to download the file. Please try again.',
|
|
51
|
+
})
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
[t]
|
|
56
|
+
);
|
|
57
|
+
const { exporting, status, startExport } = useAccountExport({
|
|
58
|
+
client,
|
|
59
|
+
t,
|
|
60
|
+
download,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// The hook reports export failures through its `status` (the web page
|
|
64
|
+
// renders it as an inline toast); RN has no such surface, so mirror the
|
|
65
|
+
// change to the imperative toast.
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (!status) return;
|
|
68
|
+
Toast.show({
|
|
69
|
+
type: status.type,
|
|
70
|
+
text1: status.message,
|
|
71
|
+
});
|
|
72
|
+
}, [status]);
|
|
73
|
+
|
|
39
74
|
const form = useForm<AccountFormValues>({
|
|
40
75
|
defaultValues: {
|
|
41
76
|
email: (currentAccount as Account)?.email ?? '',
|
|
@@ -309,6 +344,38 @@ export const AccountSettings: React.FC<AccountSettingsProps> = () => {
|
|
|
309
344
|
{t('settings.account.oldPasswordReason')}
|
|
310
345
|
</ThemedText>
|
|
311
346
|
</View>
|
|
347
|
+
<ThemedText className="text-sm text-muted-foreground">
|
|
348
|
+
{t('settings.account.exportData', {
|
|
349
|
+
defaultValue: 'Download my content',
|
|
350
|
+
})}
|
|
351
|
+
</ThemedText>
|
|
352
|
+
<ThemedText className="text-xs text-muted-foreground">
|
|
353
|
+
{t('settings.account.exportDataDescription', {
|
|
354
|
+
defaultValue:
|
|
355
|
+
'Download a copy of your data: your posts (with their thread context), posts you reacted to, and your profile and media, as a zip archive.',
|
|
356
|
+
})}
|
|
357
|
+
</ThemedText>
|
|
358
|
+
<View className="w-full">
|
|
359
|
+
<Button
|
|
360
|
+
variant="outline"
|
|
361
|
+
onPress={() => void startExport()}
|
|
362
|
+
disabled={exporting}
|
|
363
|
+
>
|
|
364
|
+
{exporting ? (
|
|
365
|
+
<ThemedText className="text-sm">
|
|
366
|
+
{t('settings.account.exportPreparing', {
|
|
367
|
+
defaultValue: 'Preparing your export…',
|
|
368
|
+
})}
|
|
369
|
+
</ThemedText>
|
|
370
|
+
) : (
|
|
371
|
+
<ThemedText className="text-sm">
|
|
372
|
+
{t('settings.account.downloadData', {
|
|
373
|
+
defaultValue: 'Download',
|
|
374
|
+
})}
|
|
375
|
+
</ThemedText>
|
|
376
|
+
)}
|
|
377
|
+
</Button>
|
|
378
|
+
</View>
|
|
312
379
|
</ThemedView>
|
|
313
380
|
</Form>
|
|
314
381
|
</KeyboardAwareScrollView>
|