Skip to content

Commit f7845ef

Browse files
committed
optimized settings import mechanism and added test case
1 parent 566ed26 commit f7845ef

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/options/index.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ function SettingPage(): JSX.Element {
105105
if (!(settings instanceof Object)) {
106106
throw new Error('导入的设定文件格式错误。')
107107
}
108+
if (Object.keys(settings).length === 0) {
109+
throw new Error('导入的设定文件格式错误。')
110+
}
108111
if (!Object.keys(settings).every(key => (fragmentKeys as string[]).includes(key))) {
109112
throw new Error('导入的设定文件格式错误。')
110113
}
@@ -117,8 +120,7 @@ function SettingPage(): JSX.Element {
117120
})();
118121
toast.promise(importing, {
119122
loading: '正在导入设定...',
120-
success: '设定已经导入成功。',
121-
error: err => '导入设定失败: ' + err.message
123+
success: '设定已经导入成功。'
122124
})
123125
await importing
124126
if (!processing) {

tests/pages/options.spec.ts

+35-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { expect, test } from '@tests/fixtures/background'
44
import BilibiliPage from '@tests/helpers/bilibili-page'
55
import logger from '@tests/helpers/logger'
66
import { getSuperChatList } from '@tests/utils/playwright'
7+
import { mkdir, writeFile } from 'fs/promises'
78
import type { MV2Settings } from '~migrations/schema'
89

910
test.beforeEach(async ({ page, extensionId }) => {
@@ -146,13 +147,45 @@ test('測試導出導入設定', async ({ page }) => {
146147
await expect(inputLineGap).toHaveValue('7')
147148
})
148149

150+
// 向下兼容,即舊版設定檔沒有某些新設定區塊,依然可以導入
151+
test('測試導入向下兼容設定', async ({ page }) => {
152+
await mkdir('out', { recursive: true })
153+
154+
{
155+
logger.info('正在嘗試導入空設定....')
156+
await writeFile('out/empty.json', '{}')
157+
158+
const fileChoosing = page.waitForEvent('filechooser')
159+
await page.getByText('导入设定').click()
160+
const fileChooser = await fileChoosing
161+
await fileChooser.setFiles('out/empty.json')
162+
163+
await page.getByText('导入的设定文件格式错误。').waitFor({ state: 'visible' })
164+
}
165+
166+
await page.reload({ waitUntil: 'domcontentloaded' })
167+
168+
{
169+
logger.info('正在嘗試導入正確設定....')
170+
await writeFile('out/valid.json', JSON.stringify({ 'settings.version': {} }))
171+
172+
const fileChoosing = page.waitForEvent('filechooser')
173+
await page.getByText('导入设定').click()
174+
const fileChooser = await fileChoosing
175+
await fileChooser.setFiles('out/valid.json')
176+
177+
await page.getByText('设定已经导入成功。').waitFor({ state: 'visible' })
178+
}
179+
180+
})
181+
149182

150183
test('測試清空數據庫', async ({ page, front: room, api }) => {
151184

152185
await page.bringToFront()
153186
const feature = page.getByText('功能设定')
154187
await feature.click()
155-
188+
156189
const btns = await page.locator('section#settings\\.features').getByText('启用离线记录').all()
157190
for (const btn of btns) {
158191
await btn.click()
@@ -517,7 +550,7 @@ test('測試导航', async ({ page, serviceWorker }) => {
517550
test('測試點擊使用指南', async ({ context, page }) => {
518551

519552
await page.getByText('功能设定').click()
520-
553+
521554
const tutorial = context.waitForEvent('page')
522555
await page.getByText('使用指南').click()
523556
const tutorialPage = await tutorial

0 commit comments

Comments
 (0)