Skip to content

Commit bc735a9

Browse files
authored
fix: clear testcase while chagne (#570)
1 parent eff02a2 commit bc735a9

File tree

3 files changed

+263
-227
lines changed

3 files changed

+263
-227
lines changed

packages/connect-examples/expo-example/src/components/BaseTestRunner/useRunnerTest.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,19 @@ export function useRunnerTest<T>(config: RunnerConfig<T>) {
442442
beginTest(true);
443443
}, [beginTest]);
444444

445+
const clearTestResults = useCallback(() => {
446+
// Clear all test results and state
447+
clearItemVerifyState?.();
448+
stableContext.setItemValues?.([]);
449+
setFailedTasks?.([]);
450+
stableContext.setRunnerLogs?.([]);
451+
}, [clearItemVerifyState, stableContext, setFailedTasks]);
452+
445453
return {
446454
beginTest,
447455
stopTest,
448456
retryFailedTasks,
457+
clearTestResults,
449458
};
450459
}
451460

packages/connect-examples/expo-example/src/testTools/addressTest/TestBatchAddress.tsx

Lines changed: 150 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
1+
import { useCallback, useEffect, useMemo, useRef, useState, useContext } from 'react';
22

33
import { CoreMessage, UI_EVENT, UI_REQUEST, UI_RESPONSE } from '@onekeyfe/hd-core';
44
import { Picker } from '@react-native-picker/picker';
@@ -16,6 +16,7 @@ import useExportReport from '../../components/BaseTestRunner/useExportReport';
1616
import { Button } from '../../components/ui/Button';
1717
import TestRunnerOptionButtons from '../../components/BaseTestRunner/TestRunnerOptionButtons';
1818
import { useHardwareInputPinDialog } from '../../provider/HardwareInputPinProvider';
19+
import { TestRunnerContext } from '../../components/BaseTestRunner/Context/TestRunnerProvider';
1920

2021
type TestCaseDataType = AddressBatchTestCase['data'][0];
2122
type ResultViewProps = { item: TestCaseDataWithKey<TestCaseDataType> };
@@ -64,9 +65,11 @@ function ExportReportView() {
6465
});
6566

6667
if (showExportReport) {
67-
<Button variant="primary" onPress={exportReport}>
68-
{intl.formatMessage({ id: 'action__export_report' })}
69-
</Button>;
68+
return (
69+
<Button variant="primary" onPress={exportReport}>
70+
{intl.formatMessage({ id: 'action__export_report' })}
71+
</Button>
72+
);
7073
}
7174

7275
return null;
@@ -76,6 +79,7 @@ let hardwareUiEventListener: any | undefined;
7679
function ExecuteView({ batchTestCases }: { batchTestCases: AddressBatchTestCase[] }) {
7780
const intl = useIntl();
7881
const { openDialog } = useHardwareInputPinDialog();
82+
const { setItemValues } = useContext(TestRunnerContext);
7983

8084
const [testCaseList, setTestCaseList] = useState<string[]>([]);
8185
const [currentTestCase, setCurrentTestCase] = useState<AddressBatchTestCase>();
@@ -112,167 +116,170 @@ function ExecuteView({ batchTestCases }: { batchTestCases: AddressBatchTestCase[
112116
const fullOriginDataRef = useRef(passphraseTestCase);
113117
const originDataRef = useRef(passphraseTestCase);
114118

115-
const { stopTest, beginTest, retryFailedTasks } = useRunnerTest<TestCaseDataType>({
116-
initTestCase: () => {
117-
const testCase = currentTestCase;
118-
const currentTestCases = testCase?.data?.map((item, index) => {
119-
const key = `${item.method}-${index}`;
120-
121-
return {
122-
...item,
123-
$key: key,
124-
} as unknown as TestCaseDataWithKey<TestCaseDataType>;
125-
});
126-
if (testCase && currentTestCases) {
127-
return Promise.resolve({
128-
title: testCase.name,
129-
data: currentTestCases,
119+
const { stopTest, beginTest, retryFailedTasks, clearTestResults } =
120+
useRunnerTest<TestCaseDataType>({
121+
initTestCase: () => {
122+
const testCase = currentTestCase;
123+
const currentTestCases = testCase?.data?.map((item, index) => {
124+
const key = `${item.method}-${index}`;
125+
126+
return {
127+
...item,
128+
$key: key,
129+
} as unknown as TestCaseDataWithKey<TestCaseDataType>;
130130
});
131-
}
132-
return Promise.resolve(undefined);
133-
},
134-
initHardwareListener: sdk => {
135-
if (hardwareUiEventListener) {
136-
sdk.off(UI_EVENT, hardwareUiEventListener);
137-
}
138-
139-
hardwareUiEventListener = (message: CoreMessage) => {
140-
console.log('TopLEVEL EVENT ===>>>>: ', message);
141-
if (message.type === UI_REQUEST.REQUEST_PIN) {
142-
openDialog(sdk, message.payload.device.features);
131+
if (testCase && currentTestCases) {
132+
return Promise.resolve({
133+
title: testCase.name,
134+
data: currentTestCases,
135+
});
143136
}
144-
if (message.type === UI_REQUEST.REQUEST_PASSPHRASE) {
145-
setTimeout(() => {
146-
sdk.uiResponse({
147-
type: UI_RESPONSE.RECEIVE_PASSPHRASE,
148-
payload: {
149-
value: currentPassphrase.current ?? '',
150-
},
151-
});
152-
}, 200);
137+
return Promise.resolve(undefined);
138+
},
139+
initHardwareListener: sdk => {
140+
if (hardwareUiEventListener) {
141+
sdk.off(UI_EVENT, hardwareUiEventListener);
153142
}
154-
};
155-
sdk.on(UI_EVENT, hardwareUiEventListener);
156-
return Promise.resolve();
157-
},
158-
prepareRunner: async (connectId, deviceId, features, sdk) => {
159-
const testCase = currentTestCase;
160-
161-
if (features?.passphrase_protection === true && testCase?.extra?.passphrase == null) {
162-
await sdk.deviceSettings(connectId, {
163-
usePassphrase: false,
164-
});
165-
}
166-
if (!features?.passphrase_protection && testCase?.extra?.passphrase != null) {
167-
await sdk.deviceSettings(connectId, {
168-
usePassphrase: true,
169-
});
170-
}
171-
172-
currentPassphrase.current = testCase?.extra?.passphrase;
173143

174-
fullOriginDataRef.current = fullPath(passphraseTestCase);
175-
originDataRef.current = passphraseTestCase;
176-
},
177-
generateRequestParams: item => {
178-
const { params } = item;
179-
const requestParams = {
180-
...params,
181-
passphraseState: currentTestCase?.extra?.passphraseState,
182-
useEmptyPassphrase: !currentTestCase?.extra?.passphrase,
183-
};
184-
return Promise.resolve({
185-
method: item.method,
186-
params: requestParams,
187-
});
188-
},
189-
processResponse: (res, item, itemIndex) => {
190-
const response = res as {
191-
path: string;
192-
address: string;
193-
// ada
194-
serializedPath: string;
195-
}[];
196-
197-
let error = '';
198-
199-
for (const key of Object.keys(item.result)) {
200-
const address = response?.find(
201-
account => account.path === key || account.serializedPath === key
202-
);
203-
204-
// 测试数据
205-
originDataRef.current = {
206-
...originDataRef.current,
207-
data: [
208-
...originDataRef.current.data.map((item, index) => {
209-
if (index === itemIndex) {
210-
const originParams = fullOriginDataRef.current.data[index].params;
211-
const template = originParams?.addressParameters?.path || originParams?.path;
212-
213-
const originKey = Object.keys(item.expectedAddress).find(key => {
214-
const path = replaceTemplate(key, template);
215-
const resultPath = address?.serializedPath || address?.path;
216-
if (path === resultPath) {
217-
return key;
218-
}
219-
return false;
220-
});
221-
222-
const indexKey = originKey || key;
223-
return {
224-
...item,
225-
expectedAddress: {
226-
...item.expectedAddress,
227-
[indexKey]: address?.address,
228-
},
229-
};
230-
}
231-
return item;
232-
}),
233-
],
144+
hardwareUiEventListener = (message: CoreMessage) => {
145+
console.log('TopLEVEL EVENT ===>>>>: ', message);
146+
if (message.type === UI_REQUEST.REQUEST_PIN) {
147+
openDialog(sdk, message.payload.device.features);
148+
}
149+
if (message.type === UI_REQUEST.REQUEST_PASSPHRASE) {
150+
setTimeout(() => {
151+
sdk.uiResponse({
152+
type: UI_RESPONSE.RECEIVE_PASSPHRASE,
153+
payload: {
154+
value: currentPassphrase.current ?? '',
155+
},
156+
});
157+
}, 200);
158+
}
234159
};
160+
sdk.on(UI_EVENT, hardwareUiEventListener);
161+
return Promise.resolve();
162+
},
163+
prepareRunner: async (connectId, deviceId, features, sdk) => {
164+
const testCase = currentTestCase;
165+
166+
if (features?.passphrase_protection === true && testCase?.extra?.passphrase == null) {
167+
await sdk.deviceSettings(connectId, {
168+
usePassphrase: false,
169+
});
170+
}
171+
if (!features?.passphrase_protection && testCase?.extra?.passphrase != null) {
172+
await sdk.deviceSettings(connectId, {
173+
usePassphrase: true,
174+
});
175+
}
235176

236-
for (const verifyField of Object.keys(item.result[key])) {
237-
if (
238-
// @ts-expect-error
239-
address[verifyField] !== item.result[key][verifyField]
240-
) {
241-
// @ts-expect-error
242-
error += `(${key}) actual: ${address[verifyField]}, expected: ${item.result[key][verifyField]}\n`;
177+
currentPassphrase.current = testCase?.extra?.passphrase;
178+
179+
fullOriginDataRef.current = fullPath(passphraseTestCase);
180+
originDataRef.current = passphraseTestCase;
181+
},
182+
generateRequestParams: item => {
183+
const { params } = item;
184+
const requestParams = {
185+
...params,
186+
passphraseState: currentTestCase?.extra?.passphraseState,
187+
useEmptyPassphrase: !currentTestCase?.extra?.passphrase,
188+
};
189+
return Promise.resolve({
190+
method: item.method,
191+
params: requestParams,
192+
});
193+
},
194+
processResponse: (res, item, itemIndex) => {
195+
const response = res as {
196+
path: string;
197+
address: string;
198+
// ada
199+
serializedPath: string;
200+
}[];
201+
202+
let error = '';
203+
204+
for (const key of Object.keys(item.result)) {
205+
const address = response?.find(
206+
account => account.path === key || account.serializedPath === key
207+
);
208+
209+
// 测试数据
210+
originDataRef.current = {
211+
...originDataRef.current,
212+
data: [
213+
...originDataRef.current.data.map((item, index) => {
214+
if (index === itemIndex) {
215+
const originParams = fullOriginDataRef.current.data[index].params;
216+
const template = originParams?.addressParameters?.path || originParams?.path;
217+
218+
const originKey = Object.keys(item.expectedAddress).find(key => {
219+
const path = replaceTemplate(key, template);
220+
const resultPath = address?.serializedPath || address?.path;
221+
if (path === resultPath) {
222+
return key;
223+
}
224+
return false;
225+
});
226+
227+
const indexKey = originKey || key;
228+
return {
229+
...item,
230+
expectedAddress: {
231+
...item.expectedAddress,
232+
[indexKey]: address?.address,
233+
},
234+
};
235+
}
236+
return item;
237+
}),
238+
],
239+
};
240+
241+
for (const verifyField of Object.keys(item.result[key])) {
242+
if (
243+
// @ts-expect-error
244+
address[verifyField] !== item.result[key][verifyField]
245+
) {
246+
// @ts-expect-error
247+
error += `(${key}) actual: ${address[verifyField]}, expected: ${item.result[key][verifyField]}\n`;
248+
}
243249
}
244250
}
245-
}
246251

247-
return Promise.resolve({
248-
error,
249-
});
250-
},
251-
removeHardwareListener: sdk => {
252-
if (hardwareUiEventListener) {
253-
sdk.off(UI_EVENT, hardwareUiEventListener);
254-
}
255-
return Promise.resolve();
256-
},
257-
processRunnerDone: () => {
258-
console.log('=====>>> Success Data:\n', JSON.stringify(originDataRef.current, null, 2));
259-
},
260-
});
252+
return Promise.resolve({
253+
error,
254+
});
255+
},
256+
removeHardwareListener: sdk => {
257+
if (hardwareUiEventListener) {
258+
sdk.off(UI_EVENT, hardwareUiEventListener);
259+
}
260+
return Promise.resolve();
261+
},
262+
processRunnerDone: () => {
263+
console.log('=====>>> Success Data:\n', JSON.stringify(originDataRef.current, null, 2));
264+
},
265+
});
261266

262267
// Additional effect to handle test case switching
263268
// This ensures that when users switch test cases, any running tests are properly stopped
269+
// and all previous test results are cleared for a clean state
264270
const prevTestCaseRef = useRef<AddressBatchTestCase | undefined>();
265271
useEffect(() => {
266272
if (
267273
prevTestCaseRef.current &&
268274
currentTestCase &&
269275
prevTestCaseRef.current.name !== currentTestCase.name
270276
) {
271-
// Test case changed - stop any running tests to ensure clean state
277+
// Test case changed - stop any running tests and clear all results
272278
stopTest();
279+
clearTestResults();
273280
}
274281
prevTestCaseRef.current = currentTestCase;
275-
}, [currentTestCase, stopTest]);
282+
}, [currentTestCase, stopTest, clearTestResults]);
276283

277284
const contentMemo = useMemo(
278285
() => (

0 commit comments

Comments
 (0)