Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

test(tooling): 对齐跨平台临时媒体与首启路径

longmeidao854e9d54ce06
diff --git a/frontend/e2e/setup.test.ts b/frontend/e2e/setup.test.ts
index 7b7ff906b..38794a529 100644
--- a/frontend/e2e/setup.test.ts
+++ b/frontend/e2e/setup.test.ts
@@ -15,6 +15,7 @@ import { launch, layout, VIEWPORTS } from './harness.ts';
const origin = process.env.PEACH_E2E_SETUP_ORIGIN ?? '';
const media = process.env.PEACH_E2E_SETUP_MEDIA ?? '';
const dataRoot = process.env.PEACH_E2E_SETUP_DATA ?? '';
+const ledgerRoot = process.env.PEACH_E2E_SETUP_LEDGER_ROOT ?? '';
/** 填错那一轮的 400 是预期内的:Chrome 会把它同时记成一条控制台错误和一条 4xx 响应。 */
const expectedRejection = (problem: string) =>
@@ -45,6 +46,11 @@ describe('首次运行页', { skip: origin && media && dataRoot ? false : '只
await heading.waitFor({ state: 'visible', timeout: 15_000 });
const folder = page.getByRole('textbox', { name: '媒体库 1' });
await folder.waitFor({ state: 'visible', timeout: 15_000 });
+ const windowsRoot = page.getByRole('textbox', { name: 'Windows 中的对应路径' });
+ if (await windowsRoot.count()) {
+ assert.ok(ledgerRoot, '非 Windows 首启需要账本中的声明根');
+ await windowsRoot.fill(ledgerRoot);
+ }
await page.waitForFunction(() => !document.querySelector('[aria-busy="true"]'), undefined, { timeout: 15_000 });
const host = page.getByRole('radiogroup');
assert.equal(await host.count(), 0, '局域网单选收在高级设置里,首屏不露出');
@@ -90,7 +96,8 @@ describe('首次运行页', { skip: origin && media && dataRoot ? false : '只
const accepted = page.waitForResponse((response) => response.url().endsWith('/api/setup')
&& response.request().method() === 'POST');
await page.getByRole('button', { name: '完成设置' }).click();
- assert.equal((await accepted).status(), 200);
+ const acceptedResponse = await accepted;
+ assert.equal(acceptedResponse.status(), 200, await acceptedResponse.text());
// 完成态:入口链接拿到焦点,扫描没有排队,运行信息收起,口令不在页面上。
await page.getByRole('heading', { name: '设置完成' }).waitFor({ state: 'visible' });
diff --git a/tests/test_scripts.py b/tests/test_scripts.py
index d61d309f4..8e8a4e2da 100644
--- a/tests/test_scripts.py
+++ b/tests/test_scripts.py
@@ -111,6 +111,9 @@ class EnglishNameSpacingTests(unittest.TestCase):
def test_recompute_batch_plans_only_mis_split_rows_and_apply_accepts_it(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory).resolve()
+ declared = PureWindowsPath('R:/media')
+ canonical = lambda path: str(declared.joinpath(*path.relative_to(root).parts))
+ translate = lambda path: root.joinpath(*PureWindowsPath(path).relative_to(declared).parts)
db = fresh_ledger(root)
wrong = root / "29_Could n't resist her.mp4"
right = root / '02_Throat fucking.mp4'
@@ -120,19 +123,19 @@ class EnglishNameSpacingTests(unittest.TestCase):
with closing(sqlite3.connect(db)) as connection:
for asset_id, path in ((1, wrong), (2, right), (3, root / 'elsewhere' / moved.name)):
connection.execute('INSERT INTO asset(id,location,path,name,medium,size) VALUES(?,?,?,?,?,?)',
- (asset_id, 'local', str(path), path.name, 'video', 5))
+ (asset_id, 'local', canonical(path), path.name, 'video', 5))
connection.commit()
log = root / 'organize-batch.json'
# 第 1 条在批次之后被目录整理从 New 搬到了 root:按 asset_id 找当前行。
log.write_text(json.dumps({'entries': [
- {'asset_id': 1, 'old_path': str(root / 'New' / "29_Couldn'tresisther.mp4"),
- 'new_path': str(root / 'New' / wrong.name)},
- {'asset_id': 2, 'old_path': str(root / '02_Throatfucking.mp4'), 'new_path': str(right)},
- {'asset_id': 3, 'old_path': str(root / '03_Sloppythroatt.mp4'), 'new_path': str(moved)},
+ {'asset_id': 1, 'old_path': canonical(root / 'New' / "29_Couldn'tresisther.mp4"),
+ 'new_path': canonical(root / 'New' / wrong.name)},
+ {'asset_id': 2, 'old_path': canonical(root / '02_Throatfucking.mp4'), 'new_path': canonical(right)},
+ {'asset_id': 3, 'old_path': canonical(root / '03_Sloppythroatt.mp4'), 'new_path': canonical(moved)},
]}), encoding='utf-8')
review = root / 'recompute.csv'
with redirect_stdout(io.StringIO()) as output:
- self.assertEqual(self.script.main(['--db', str(db), '--root', str(root),
+ self.assertEqual(self.script.main(['--db', str(db), '--root', str(declared),
'--recompute-batch', str(log), '--out', str(review)]), 0)
report = json.loads(output.getvalue())
self.assertEqual(report['planned'], 1)
@@ -142,31 +145,38 @@ class EnglishNameSpacingTests(unittest.TestCase):
[('1', "29_Couldn't resist her.mp4")])
self.assertTrue(wrong.exists())
- with mock.patch.object(self.script, 'location_roots', return_value={'local': [str(root)]}), \
+ with mock.patch.object(self.script, 'location_roots', return_value={'local': [str(declared)]}), \
+ mock.patch.object(self.script, 'translate_ledger_path', side_effect=translate), \
+ mock.patch.object(self.script.organize, 'translate_ledger_path', side_effect=translate), \
mock.patch.object(self.script, 'GENERATED_DIR', root / 'generated'), \
redirect_stdout(io.StringIO()):
- self.assertEqual(self.script.main(['--db', str(db), '--root', str(root), '--review-csv', str(review),
+ self.assertEqual(self.script.main(['--db', str(db), '--root', str(declared), '--review-csv', str(review),
'--apply', '--backup', str(root / 'backup.db')]), 0)
self.assertFalse(wrong.exists())
self.assertTrue((root / "29_Couldn't resist her.mp4").exists())
def test_apply_rejects_character_changes_before_opening_writer(self):
with tempfile.TemporaryDirectory() as directory:
- root = Path(directory)
+ root = Path(directory).resolve()
+ declared = PureWindowsPath('R:/media')
+ canonical = lambda path: str(declared.joinpath(*path.relative_to(root).parts))
+ translate = lambda path: root.joinpath(*PureWindowsPath(path).relative_to(declared).parts)
db = fresh_ledger(root)
source = root / 'original.mp4'
source.write_bytes(b'media')
with closing(sqlite3.connect(db)) as connection:
connection.execute('INSERT INTO asset(id,location,path,name,medium,size) VALUES(1,?,?,?,?,?)',
- ('local', str(source), source.name, 'video', 5))
+ ('local', canonical(source), source.name, 'video', 5))
connection.commit()
review = root / 'names.csv'
- row = dict(asset_id=1, location='local', current_path=str(source), target_path=str(root / 'invented.mp4'),
+ row = dict(asset_id=1, location='local', current_path=canonical(source), target_path=canonical(root / 'invented.mp4'),
action='rename', reason='', size=5, original_name=source.name, new_name='invented.mp4')
self.script.write_rows(review, self.script.FIELDS, [row])
- with mock.patch.object(self.script, 'open_for_write') as writer, mock.patch.object(self.script, 'location_roots', return_value={'local':[str(root)]}):
+ with mock.patch.object(self.script, 'open_for_write') as writer, \
+ mock.patch.object(self.script, 'location_roots', return_value={'local': [str(declared)]}), \
+ mock.patch.object(self.script, 'translate_ledger_path', side_effect=translate):
with self.assertRaisesRegex(ValueError, '改变了内容字符'):
- self.script.main(['--db', str(db), '--root', str(root), '--review-csv', str(review),
+ self.script.main(['--db', str(db), '--root', str(declared), '--review-csv', str(review),
'--apply', '--backup', str(root / 'backup.db')])
writer.assert_not_called()
self.assertEqual(source.read_bytes(), b'media')
@@ -393,7 +403,7 @@ class LibraryDirectoryTests(unittest.TestCase):
def translate(path):
if PureWindowsPath(path).drive.casefold() == 'b:':
return media.joinpath(*PureWindowsPath(path).parts[1:])
- return Path(path)
+ return Path(PureWindowsPath(path).as_posix())
def rename(old, new):
translate(new).parent.mkdir(parents=True, exist_ok=True)
translate(old).rename(translate(new))
diff --git a/tests/test_web_e2e.py b/tests/test_web_e2e.py
index 2799390e0..d61787f11 100644
--- a/tests/test_web_e2e.py
+++ b/tests/test_web_e2e.py
@@ -342,6 +342,7 @@ class SetupE2ETests(unittest.TestCase):
cls.data = cls.root / "peach-data"
cls.media = cls.root / "media"
cls.media.mkdir()
+ cls.ledger_root = str(cls.media) if os.name == "nt" else "R:\\media"
cls.port = free_port()
cls.origin = f"http://127.0.0.1:{cls.port}"
env = dict(os.environ, PEACH_DATA_ROOT=str(cls.data), PYTHONIOENCODING="utf-8")
@@ -393,7 +394,8 @@ class SetupE2ETests(unittest.TestCase):
def test_the_first_run_page_writes_errors_back_and_finishes(self):
self.assertFalse((self.data / settings_file.SETTINGS_FILENAME).exists())
env = dict(os.environ, PEACH_E2E_SETUP_ORIGIN=self.origin, PEACH_E2E_SETUP_MEDIA=str(self.media),
- PEACH_E2E_SETUP_DATA=str(self.data), PEACH_E2E_CHROME=self.chrome)
+ PEACH_E2E_SETUP_DATA=str(self.data), PEACH_E2E_SETUP_LEDGER_ROOT=self.ledger_root,
+ PEACH_E2E_CHROME=self.chrome)
BROWSER_LOGS.mkdir(parents=True, exist_ok=True)
log_path = BROWSER_LOGS / "setup.tap"
try:
@@ -415,7 +417,9 @@ class SetupE2ETests(unittest.TestCase):
# 改对的那一次提交在临时数据根里落了设置文件,媒体库就是传进去的那个目录。
written = settings_file.load_config(environ={settings_file.DATA_ROOT_ENV: str(self.data)})
self.assertTrue(written.present)
- self.assertEqual([Path(path) for path in written.locations.get("local", ())], [self.media])
+ self.assertEqual(list(written.locations.get("local", ())), [self.ledger_root])
+ if os.name != "nt":
+ self.assertEqual([Path(path) for path in written.mounts.get("local", ())], [self.media])
class MissingPrerequisiteTests(unittest.TestCase):