Compare commits
19 Commits
c879c4c0d9
...
testbed
| Author | SHA1 | Date | |
|---|---|---|---|
| 32e96ebea4 | |||
| 3f43662c1f | |||
| aa4517aaf5 | |||
| 15e7636a92 | |||
| f791798206 | |||
| 3719cda819 | |||
| 2718d817d9 | |||
| 5f62040611 | |||
| fda9f01da1 | |||
| 559342fae7 | |||
| fe4061d1a1 | |||
| 22697fdc1d | |||
| c982fb930f | |||
| 7dae8647e6 | |||
| a5baba8057 | |||
| 74b26d10ab | |||
| 6eefcc6820 | |||
| e3aae69068 | |||
| 58709570a9 |
+87
-2
@@ -4,7 +4,8 @@ from typing import Sequence
|
||||
|
||||
from fastapi import FastAPI, Form, HTTPException, Query, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
|
||||
import asyncio
|
||||
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse, StreamingResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -14,9 +15,16 @@ from app.utils.local_playlist import load_local_playlist, scan_local_playlists
|
||||
from app.utils.logger import logger
|
||||
from app.utils.playlist_merge import SyncMode, TEST_PLAYLIST_DIR, sync_all_playlists
|
||||
from app.utils.plex_client import plex_client
|
||||
from app.utils.scheduler import start_scheduler, update_scheduler_job, get_next_run_time, validate_cron_expression
|
||||
from app.utils.sync_manager import sync_manager
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
sync_manager.set_event_loop(asyncio.get_running_loop())
|
||||
start_scheduler()
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
@@ -107,6 +115,51 @@ class ConnectRequest(BaseModel):
|
||||
library_name: str | None = None
|
||||
|
||||
|
||||
class ScheduleSettings(BaseModel):
|
||||
mode: str
|
||||
cronExpression: str
|
||||
dailyTime: str
|
||||
weeklyDays: list[int]
|
||||
weeklyTime: str
|
||||
autoWatch: bool
|
||||
|
||||
|
||||
@app.get("/api/schedule")
|
||||
async def get_schedule():
|
||||
next_run = get_next_run_time()
|
||||
next_run_str = next_run.strftime("%Y-%m-%d %H:%M:%S") if next_run else None
|
||||
|
||||
return {
|
||||
"mode": server_config.schedule_mode,
|
||||
"cronExpression": server_config.schedule_cron,
|
||||
"dailyTime": server_config.schedule_daily_time,
|
||||
"weeklyDays": server_config.schedule_weekly_days,
|
||||
"weeklyTime": server_config.schedule_weekly_time,
|
||||
"autoWatch": server_config.schedule_auto_watch,
|
||||
"nextRun": next_run_str
|
||||
}
|
||||
|
||||
|
||||
@app.put("/api/schedule")
|
||||
async def save_schedule(settings: ScheduleSettings):
|
||||
# Validate Cron if mode is CRON
|
||||
if settings.mode == "CRON" and settings.cronExpression.strip():
|
||||
if not validate_cron_expression(settings.cronExpression):
|
||||
raise HTTPException(status_code=400, detail="Invalid Cron expression format")
|
||||
|
||||
server_config.set_schedule(
|
||||
mode=settings.mode,
|
||||
cron=settings.cronExpression,
|
||||
daily_time=settings.dailyTime,
|
||||
weekly_days=settings.weeklyDays,
|
||||
weekly_time=settings.weeklyTime,
|
||||
auto_watch=settings.autoWatch
|
||||
)
|
||||
update_scheduler_job()
|
||||
logger.info(f"Schedule settings updated via API. Mode: {settings.mode}")
|
||||
return {"status": "success", "message": "Schedule updated"}
|
||||
|
||||
|
||||
class ConnectResponse(BaseModel):
|
||||
token: str
|
||||
serverInfo: dict
|
||||
@@ -401,6 +454,25 @@ async def api_playlists(server: str = Query(..., pattern="(?i)^(local|cloud)$"),
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/sync/status")
|
||||
async def get_sync_status():
|
||||
return sync_manager.status
|
||||
|
||||
@app.get("/api/sync/events")
|
||||
async def sync_events(request: Request):
|
||||
async def event_generator():
|
||||
q = await sync_manager.subscribe()
|
||||
try:
|
||||
while True:
|
||||
if await request.is_disconnected():
|
||||
break
|
||||
data = await q.get()
|
||||
yield f"data: {data}\n\n"
|
||||
finally:
|
||||
sync_manager.unsubscribe(q)
|
||||
|
||||
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
||||
|
||||
@app.post("/api/sync")
|
||||
async def api_sync(payload: SyncRequest):
|
||||
server_config.load()
|
||||
@@ -410,7 +482,20 @@ async def api_sync(payload: SyncRequest):
|
||||
raise HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
local_dir = payload.local_path or server_config.local_path
|
||||
results = sync_all_playlists(local_dir=local_dir, mode=sync_mode, test_folder=TEST_PLAYLIST_DIR)
|
||||
|
||||
# Use sync_manager to execute sync, ensuring state is updated
|
||||
try:
|
||||
results = sync_manager.run_sync(
|
||||
trigger_source="api",
|
||||
wait=True,
|
||||
# We need to pass these to _perform_sync
|
||||
sync_kwargs={"local_dir": local_dir, "mode": sync_mode}
|
||||
)
|
||||
except Exception as e:
|
||||
if str(e) == "Sync already in progress":
|
||||
raise HTTPException(status_code=409, detail="Sync already in progress")
|
||||
raise e
|
||||
|
||||
merged_count = sum(len(item.merged_paths) for item in results)
|
||||
conflict_count = sum(len(item.conflicts) for item in results)
|
||||
deleted_count = sum(1 for item in results if item.action == "deleted")
|
||||
|
||||
+37
-1
@@ -22,6 +22,12 @@ class ServerConfig:
|
||||
self.sync_mode = DEFAULT_SYNC_MODE
|
||||
self.local_path = "playlist"
|
||||
self.path_rules: list[dict[str, str]] = []
|
||||
self.schedule_mode = "DISABLED"
|
||||
self.schedule_cron = ""
|
||||
self.schedule_daily_time = "02:00"
|
||||
self.schedule_weekly_days = [0]
|
||||
self.schedule_weekly_time = "03:00"
|
||||
self.schedule_auto_watch = False
|
||||
self.load()
|
||||
|
||||
def load(self) -> None:
|
||||
@@ -49,7 +55,14 @@ class ServerConfig:
|
||||
self.sync_mode = config.get("sync_mode", DEFAULT_SYNC_MODE)
|
||||
self.local_path = config.get("local_path", "playlist")
|
||||
self.path_rules = config.get("path_rules", []) or []
|
||||
logger.info(f"Server config loaded: {self.__dict__}")
|
||||
self.schedule_mode = config.get("schedule_mode", "DISABLED")
|
||||
self.schedule_cron = config.get("schedule_cron", "")
|
||||
self.schedule_daily_time = config.get("schedule_daily_time", "02:00")
|
||||
self.schedule_weekly_days = config.get("schedule_weekly_days", [0])
|
||||
self.schedule_weekly_time = config.get("schedule_weekly_time", "03:00")
|
||||
self.schedule_auto_watch = config.get("schedule_auto_watch", False)
|
||||
logger.info(f"Server config loaded.")
|
||||
logger.debug(f"Current server config: {self.__dict__}")
|
||||
|
||||
def save(self):
|
||||
config = {
|
||||
@@ -63,6 +76,12 @@ class ServerConfig:
|
||||
"sync_mode": self.sync_mode,
|
||||
"local_path": self.local_path,
|
||||
"path_rules": self.path_rules,
|
||||
"schedule_mode": self.schedule_mode,
|
||||
"schedule_cron": self.schedule_cron,
|
||||
"schedule_daily_time": self.schedule_daily_time,
|
||||
"schedule_weekly_days": self.schedule_weekly_days,
|
||||
"schedule_weekly_time": self.schedule_weekly_time,
|
||||
"schedule_auto_watch": self.schedule_auto_watch,
|
||||
}
|
||||
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
||||
json.dump(config, f, indent=4, ensure_ascii=False)
|
||||
@@ -102,6 +121,23 @@ class ServerConfig:
|
||||
def set_path_rules(self, path_rules: list[dict[str, str]]) -> None:
|
||||
self.path_rules = path_rules or []
|
||||
|
||||
def set_schedule(
|
||||
self,
|
||||
mode: str,
|
||||
cron: str,
|
||||
daily_time: str,
|
||||
weekly_days: list[int],
|
||||
weekly_time: str,
|
||||
auto_watch: bool,
|
||||
) -> None:
|
||||
self.schedule_mode = mode
|
||||
self.schedule_cron = cron
|
||||
self.schedule_daily_time = daily_time
|
||||
self.schedule_weekly_days = weekly_days
|
||||
self.schedule_weekly_time = weekly_time
|
||||
self.schedule_auto_watch = auto_watch
|
||||
self.save()
|
||||
|
||||
def set_and_save_config(
|
||||
self,
|
||||
theme: str = None,
|
||||
|
||||
+23
-1
@@ -4,7 +4,29 @@ import os
|
||||
LOG_PATH = os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), "..", "logs", "app.log"))
|
||||
|
||||
LOG_LEVEL = logging.DEBUG
|
||||
def _get_log_level():
|
||||
"""Get log level from environment variable."""
|
||||
level_str = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||
|
||||
# Try to convert to integer
|
||||
if level_str.isdigit():
|
||||
return int(level_str)
|
||||
|
||||
# Map string to logging level
|
||||
levels = {
|
||||
"CRITICAL": logging.CRITICAL,
|
||||
"FATAL": logging.FATAL,
|
||||
"ERROR": logging.ERROR,
|
||||
"WARN": logging.WARNING,
|
||||
"WARNING": logging.WARNING,
|
||||
"INFO": logging.INFO,
|
||||
"DEBUG": logging.DEBUG,
|
||||
"NOTSET": logging.NOTSET,
|
||||
}
|
||||
|
||||
return levels.get(level_str, logging.INFO)
|
||||
|
||||
LOG_LEVEL = _get_log_level()
|
||||
|
||||
def logger_initialize() -> logging.Logger:
|
||||
"""Initialize the logger for the application. Return a logger that logs to console and a app.log."""
|
||||
|
||||
@@ -159,8 +159,22 @@ def _read_text_if_exists(path: str) -> tuple[str, bool]:
|
||||
def _save_playlist_to_folder(filename: str, paths: Sequence[str], folder: str) -> str:
|
||||
_ensure_test_dir(folder)
|
||||
file_path = os.path.join(folder, filename)
|
||||
logger.info(f"Saving playlist to: {file_path}")
|
||||
|
||||
new_content = save_paths(paths)
|
||||
|
||||
# Check if content has changed before writing to avoid triggering unnecessary file events
|
||||
if os.path.exists(file_path):
|
||||
try:
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
current_content = f.read()
|
||||
if current_content == new_content:
|
||||
return file_path
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
with open(file_path, "w", encoding="utf-8") as file:
|
||||
file.write(save_paths(paths))
|
||||
file.write(new_content)
|
||||
return file_path
|
||||
|
||||
|
||||
@@ -572,6 +586,7 @@ def sync_all_playlists(
|
||||
server_config.load()
|
||||
compiled_rules = _compile_regex_rules(server_config.path_rules)
|
||||
_ensure_test_dir(test_folder)
|
||||
logger.info(f"Syncing playlists to test folder: {test_folder}")
|
||||
local_playlists = _load_local_playlists(local_dir)
|
||||
remote_playlists = _fetch_remote_playlists()
|
||||
playlist_names: set[str] = set(local_playlists.keys())
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
from typing import Optional
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from apscheduler.triggers.cron import CronTrigger
|
||||
from apscheduler.triggers.base import BaseTrigger
|
||||
from app.utils.config import server_config
|
||||
from app.utils.logger import logger
|
||||
from app.utils.watcher import watcher_manager
|
||||
from app.utils.sync_manager import sync_manager
|
||||
import os
|
||||
|
||||
# Initialize the scheduler
|
||||
scheduler = BackgroundScheduler()
|
||||
|
||||
def validate_cron_expression(expression: str) -> bool:
|
||||
"""
|
||||
Validates a cron expression.
|
||||
Expected format: "minute hour day month day_of_week"
|
||||
"""
|
||||
try:
|
||||
parts = expression.split()
|
||||
if len(parts) != 5:
|
||||
return False
|
||||
# Try to create a trigger to validate
|
||||
CronTrigger(
|
||||
minute=parts[0],
|
||||
hour=parts[1],
|
||||
day=parts[2],
|
||||
month=parts[3],
|
||||
day_of_week=parts[4]
|
||||
)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def job_function():
|
||||
"""
|
||||
The function to be executed by the scheduler.
|
||||
Triggers the sync process.
|
||||
"""
|
||||
logger.info("Executing scheduled sync job...")
|
||||
try:
|
||||
sync_manager.run_sync(trigger_source="scheduler", wait=False)
|
||||
except Exception as e:
|
||||
logger.error(f"Error during scheduled sync job: {e}", exc_info=True)
|
||||
|
||||
def start_scheduler():
|
||||
"""
|
||||
Starts the background scheduler if it's not already running.
|
||||
"""
|
||||
if not scheduler.running:
|
||||
scheduler.start()
|
||||
logger.info("Scheduler started.")
|
||||
update_scheduler_job()
|
||||
|
||||
def _create_cron_trigger(cron_exp: str) -> Optional[CronTrigger]:
|
||||
"""Helper to create a CronTrigger from a cron expression string."""
|
||||
try:
|
||||
# 5 parts: minute hour day month day_of_week
|
||||
parts = cron_exp.split()
|
||||
if len(parts) == 5:
|
||||
return CronTrigger(
|
||||
minute=parts[0],
|
||||
hour=parts[1],
|
||||
day=parts[2],
|
||||
month=parts[3],
|
||||
day_of_week=parts[4]
|
||||
)
|
||||
else:
|
||||
logger.error(f"Invalid cron expression format (needs 5 parts): {cron_exp}")
|
||||
except Exception as e:
|
||||
logger.error(f"Invalid cron expression: {cron_exp}, error: {e}")
|
||||
return None
|
||||
|
||||
def _create_daily_trigger(time_str: str) -> Optional[CronTrigger]:
|
||||
"""Helper to create a CronTrigger for daily execution at a specific time."""
|
||||
try:
|
||||
hour, minute = map(int, time_str.split(':'))
|
||||
return CronTrigger(hour=hour, minute=minute)
|
||||
except ValueError:
|
||||
logger.error(f"Invalid daily time format: {time_str}")
|
||||
return None
|
||||
|
||||
def _create_weekly_trigger(days: list[int], time_str: str) -> Optional[CronTrigger]:
|
||||
"""
|
||||
Helper to create a CronTrigger for weekly execution.
|
||||
days: List of integers 0-6 where 0 is Sunday, 1 is Monday, ..., 6 is Saturday.
|
||||
APScheduler expects: 0 = Monday, ..., 6 = Sunday.
|
||||
"""
|
||||
# Convert Frontend days (0=Sun...6=Sat) to APScheduler days (0=Mon...6=Sun)
|
||||
aps_days = []
|
||||
for d in days:
|
||||
if d == 0:
|
||||
aps_days.append(6) # Sunday
|
||||
else:
|
||||
aps_days.append(d - 1) # Mon(1)->0, ..., Sat(6)->5
|
||||
|
||||
days_str = ",".join(map(str, aps_days))
|
||||
|
||||
try:
|
||||
hour, minute = map(int, time_str.split(':'))
|
||||
return CronTrigger(day_of_week=days_str, hour=hour, minute=minute)
|
||||
except ValueError:
|
||||
logger.error(f"Invalid weekly time format: {time_str}")
|
||||
return None
|
||||
|
||||
def update_scheduler_job():
|
||||
"""
|
||||
Updates the scheduler jobs based on the current configuration.
|
||||
Reloads configuration, handles auto-watch, and sets up the sync job trigger.
|
||||
"""
|
||||
scheduler.remove_all_jobs()
|
||||
|
||||
# Reload config to get latest schedule settings
|
||||
server_config.load()
|
||||
logger.info("Configuration reloaded for scheduler update.")
|
||||
|
||||
# Handle Auto Watch
|
||||
if server_config.schedule_auto_watch:
|
||||
# Ensure we have an absolute path
|
||||
local_path = os.path.abspath(server_config.local_path)
|
||||
watcher_manager.start(local_path)
|
||||
logger.info(f"Auto-watch started for path: {local_path}")
|
||||
else:
|
||||
watcher_manager.stop()
|
||||
logger.info("Auto-watch stopped.")
|
||||
|
||||
mode = server_config.schedule_mode
|
||||
logger.info(f"Updating scheduler with mode: {mode}")
|
||||
|
||||
if mode == "DISABLED":
|
||||
logger.info("Schedule is disabled. No jobs added.")
|
||||
return
|
||||
|
||||
trigger: Optional[BaseTrigger] = None
|
||||
|
||||
if mode == "CRON":
|
||||
trigger = _create_cron_trigger(server_config.schedule_cron)
|
||||
|
||||
elif mode == "DAILY":
|
||||
trigger = _create_daily_trigger(server_config.schedule_daily_time)
|
||||
|
||||
elif mode == "WEEKLY":
|
||||
trigger = _create_weekly_trigger(server_config.schedule_weekly_days, server_config.schedule_weekly_time)
|
||||
|
||||
if trigger:
|
||||
scheduler.add_job(job_function, trigger)
|
||||
logger.info(f"Added scheduled job with mode '{mode}' and trigger: {trigger}")
|
||||
else:
|
||||
logger.warning(f"Failed to create trigger for mode '{mode}'. No job added.")
|
||||
|
||||
def get_next_run_time():
|
||||
"""
|
||||
Returns the next run time of the scheduled job, if any.
|
||||
"""
|
||||
jobs = scheduler.get_jobs()
|
||||
if not jobs:
|
||||
return None
|
||||
# Assuming only one job is scheduled for sync
|
||||
job = jobs[0]
|
||||
return job.next_run_time
|
||||
@@ -0,0 +1,124 @@
|
||||
import threading
|
||||
import asyncio
|
||||
import json
|
||||
from datetime import datetime
|
||||
from app.utils.logger import logger
|
||||
from app.utils.playlist_merge import sync_all_playlists, SyncMode
|
||||
from app.utils.config import server_config
|
||||
|
||||
class SyncManager:
|
||||
def __init__(self):
|
||||
self._lock = threading.Lock()
|
||||
self._is_syncing = False
|
||||
self._last_sync_time = None
|
||||
self._last_status = "idle" # idle, syncing, success, error
|
||||
self._last_error = None
|
||||
self._listeners = [] # List of asyncio.Queue
|
||||
self._loop = None
|
||||
|
||||
def set_event_loop(self, loop):
|
||||
self._loop = loop
|
||||
|
||||
async def subscribe(self):
|
||||
q = asyncio.Queue()
|
||||
self._listeners.append(q)
|
||||
# Send current status immediately
|
||||
await q.put(json.dumps(self.status))
|
||||
return q
|
||||
|
||||
def unsubscribe(self, q):
|
||||
if q in self._listeners:
|
||||
self._listeners.remove(q)
|
||||
|
||||
def _notify_listeners(self):
|
||||
if not self._loop or not self._listeners:
|
||||
return
|
||||
|
||||
status_json = json.dumps(self.status)
|
||||
|
||||
for q in self._listeners:
|
||||
try:
|
||||
self._loop.call_soon_threadsafe(q.put_nowait, status_json)
|
||||
except Exception as e:
|
||||
logger.error(f"Error notifying listener: {e}")
|
||||
|
||||
@property
|
||||
def is_syncing(self):
|
||||
with self._lock:
|
||||
return self._is_syncing
|
||||
|
||||
@property
|
||||
def status(self):
|
||||
with self._lock:
|
||||
return {
|
||||
"is_syncing": self._is_syncing,
|
||||
"last_sync_time": self._last_sync_time.isoformat() if self._last_sync_time else None,
|
||||
"status": self._last_status,
|
||||
"error": str(self._last_error) if self._last_error else None
|
||||
}
|
||||
|
||||
def run_sync(self, trigger_source="manual", wait=False, sync_kwargs=None):
|
||||
"""
|
||||
Thread-safe sync execution.
|
||||
If wait=True, blocks until sync completes and returns result.
|
||||
If wait=False, runs in background and returns True if started.
|
||||
"""
|
||||
with self._lock:
|
||||
if self._is_syncing:
|
||||
logger.warning(f"Sync requested ({trigger_source}) but already in progress.")
|
||||
if wait:
|
||||
raise Exception("Sync already in progress")
|
||||
return False
|
||||
self._is_syncing = True
|
||||
self._last_status = "syncing"
|
||||
self._last_error = None
|
||||
|
||||
self._notify_listeners()
|
||||
logger.info(f"Starting sync (Source: {trigger_source})...")
|
||||
|
||||
if wait:
|
||||
try:
|
||||
result = self._perform_sync(sync_kwargs)
|
||||
self._complete_sync("success")
|
||||
return result
|
||||
except Exception as e:
|
||||
self._complete_sync("error", e)
|
||||
raise e
|
||||
else:
|
||||
thread = threading.Thread(target=self._sync_worker, args=(trigger_source, sync_kwargs))
|
||||
thread.start()
|
||||
return True
|
||||
|
||||
def _sync_worker(self, trigger_source, sync_kwargs=None):
|
||||
try:
|
||||
self._perform_sync(sync_kwargs)
|
||||
self._complete_sync("success")
|
||||
logger.info(f"Sync completed successfully (Source: {trigger_source}).")
|
||||
except Exception as e:
|
||||
logger.error(f"Sync failed (Source: {trigger_source}): {e}")
|
||||
self._complete_sync("error", e)
|
||||
|
||||
def _perform_sync(self, sync_kwargs=None):
|
||||
# Reload config to ensure latest values
|
||||
server_config.load()
|
||||
|
||||
kwargs = {
|
||||
"local_dir": server_config.local_path,
|
||||
"mode": SyncMode(server_config.sync_mode)
|
||||
}
|
||||
|
||||
if sync_kwargs:
|
||||
kwargs.update(sync_kwargs)
|
||||
|
||||
# Execute sync
|
||||
return sync_all_playlists(**kwargs)
|
||||
|
||||
def _complete_sync(self, status, error=None):
|
||||
with self._lock:
|
||||
self._last_status = status
|
||||
self._last_error = error
|
||||
self._last_sync_time = datetime.now()
|
||||
self._is_syncing = False
|
||||
self._notify_listeners()
|
||||
|
||||
sync_manager = SyncManager()
|
||||
@@ -0,0 +1,115 @@
|
||||
import os
|
||||
import threading
|
||||
from typing import Optional
|
||||
from watchdog.observers.polling import PollingObserver as Observer
|
||||
from watchdog.events import FileSystemEventHandler, FileSystemEvent
|
||||
from app.utils.logger import logger
|
||||
from app.utils.sync_manager import sync_manager
|
||||
|
||||
class PlaylistEventHandler(FileSystemEventHandler):
|
||||
"""
|
||||
Handles file system events for the playlist directory.
|
||||
Triggers a sync operation when changes are detected, with debouncing.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.debounce_timer: Optional[threading.Timer] = None
|
||||
self.debounce_interval = 5.0 # Seconds
|
||||
|
||||
def on_any_event(self, event: FileSystemEvent):
|
||||
# Log all events at DEBUG level to avoid cluttering INFO logs
|
||||
logger.debug(f"[Watcher] Event detected: {event.event_type} {event.src_path}")
|
||||
|
||||
if event.is_directory:
|
||||
return
|
||||
|
||||
# Filter out noisy events. Only listen to actual changes.
|
||||
# 'opened' and 'closed' (without write) are read events and should be ignored.
|
||||
if event.event_type not in ['created', 'modified', 'deleted', 'moved']:
|
||||
return
|
||||
|
||||
# Ignore temporary files or hidden files
|
||||
filename = os.path.basename(event.src_path)
|
||||
if filename.startswith('.'):
|
||||
return
|
||||
|
||||
# Prevent feedback loops: if sync is in progress, ignore events
|
||||
if sync_manager.is_syncing:
|
||||
logger.debug(f"[Watcher] Ignoring event {event.event_type} on {event.src_path} because sync is in progress.")
|
||||
return
|
||||
|
||||
logger.info(f"[Watcher] Accepted file change: {event.event_type} {event.src_path}")
|
||||
self.trigger_sync()
|
||||
|
||||
def trigger_sync(self):
|
||||
"""
|
||||
Triggers the sync process after a debounce interval.
|
||||
"""
|
||||
if self.debounce_timer:
|
||||
self.debounce_timer.cancel()
|
||||
|
||||
logger.debug(f"[Watcher] Debouncing sync for {self.debounce_interval} seconds...")
|
||||
self.debounce_timer = threading.Timer(self.debounce_interval, self.run_sync)
|
||||
self.debounce_timer.start()
|
||||
|
||||
def run_sync(self):
|
||||
"""
|
||||
Executes the sync via SyncManager.
|
||||
"""
|
||||
logger.info("[Watcher] Debounce timer expired. Triggering sync due to file changes.")
|
||||
try:
|
||||
sync_manager.run_sync(trigger_source="watcher", wait=False)
|
||||
except Exception as e:
|
||||
logger.error(f"[Watcher] Failed to trigger sync: {e}", exc_info=True)
|
||||
|
||||
class WatcherManager:
|
||||
"""
|
||||
Manages the lifecycle of the file watcher.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.observer: Optional[Observer] = None
|
||||
self.handler: Optional[PlaylistEventHandler] = None
|
||||
self.current_path: Optional[str] = None
|
||||
|
||||
def start(self, path: str):
|
||||
"""
|
||||
Starts watching the specified directory.
|
||||
"""
|
||||
# If already watching the same path, do nothing
|
||||
if self.observer and self.observer.is_alive() and self.current_path == path:
|
||||
logger.info(f"[Watcher] Already running on {path}")
|
||||
return
|
||||
|
||||
self.stop()
|
||||
|
||||
if not os.path.exists(path):
|
||||
logger.warning(f"[Watcher] Cannot watch path {path}: Directory does not exist.")
|
||||
return
|
||||
|
||||
logger.info(f"[Watcher] Starting file watcher on: {path}")
|
||||
try:
|
||||
files = os.listdir(path)
|
||||
logger.debug(f"[Watcher] Initial files in watch directory: {files}")
|
||||
except Exception as e:
|
||||
logger.error(f"[Watcher] Failed to list files in watch directory: {e}")
|
||||
|
||||
self.handler = PlaylistEventHandler()
|
||||
# Explicitly set timeout for PollingObserver
|
||||
self.observer = Observer(timeout=1.0)
|
||||
self.observer.schedule(self.handler, path, recursive=True)
|
||||
self.observer.start()
|
||||
self.current_path = path
|
||||
logger.info("[Watcher] Watcher started successfully.")
|
||||
|
||||
def stop(self):
|
||||
"""
|
||||
Stops the file watcher.
|
||||
"""
|
||||
if self.observer:
|
||||
logger.info("[Watcher] Stopping file watcher...")
|
||||
self.observer.stop()
|
||||
self.observer.join()
|
||||
self.observer = None
|
||||
self.current_path = None
|
||||
logger.info("[Watcher] Watcher stopped.")
|
||||
|
||||
watcher_manager = WatcherManager()
|
||||
+3
-1
@@ -5,8 +5,10 @@ services:
|
||||
ports:
|
||||
- "8888:8080"
|
||||
volumes:
|
||||
- path_to_your_playlist:/app/playlist
|
||||
- ./output_playlists:/app/app/test_playlists
|
||||
- ./test_case/local_playlist:/app/playlist:ro
|
||||
environment:
|
||||
- PYTHONUNBUFFERED=1
|
||||
- PYTHONDONTWRITEBYTECODE=1
|
||||
- LOG_LEVEL=INFO
|
||||
restart: unless-stopped
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
Write-Output "Starting PlexPlaylistSync Docker Container..."
|
||||
Set-Location ./frontend
|
||||
npm run build
|
||||
Set-Location ..
|
||||
docker compose down
|
||||
docker compose up --build
|
||||
+178
-5
@@ -1,6 +1,6 @@
|
||||
|
||||
import React, { useEffect, useState, useCallback, useRef } from 'react';
|
||||
import { Playlist, ServerType, SyncStrategy, PlexServerConnection, RegexReplacement, PlexConnectionSettings, SyncState } from './types';
|
||||
import { Playlist, ServerType, SyncStrategy, PlexServerConnection, RegexReplacement, PlexConnectionSettings, SyncState, ScheduleSettings, ScheduleMode } from './types';
|
||||
import { apiService } from './services/api';
|
||||
import {
|
||||
STRIPE_BASE_SPEED,
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
import ServerPanel from './components/ServerPanel';
|
||||
import StrategySelector from './components/StrategySelector';
|
||||
import ConnectionModal from './components/ConnectionModal';
|
||||
import { ArrowLeftRight, ShieldCheck, X, Server, ServerOff } from 'lucide-react';
|
||||
import { ArrowLeftRight, ShieldCheck, X, Server, ServerOff, Clock, Eye, EyeOff } from 'lucide-react';
|
||||
|
||||
interface Toast {
|
||||
id: number;
|
||||
@@ -125,6 +125,8 @@ const App: React.FC = () => {
|
||||
const [loadingCloud, setLoadingCloud] = useState(false);
|
||||
|
||||
const [syncState, setSyncState] = useState<SyncState>(SyncState.IDLE);
|
||||
const manualSyncInProgress = useRef(false);
|
||||
const lastKnownSyncTimeRef = useRef<string | null | undefined>(undefined);
|
||||
|
||||
// Animation Refs
|
||||
const { leftYellowRef, leftGreenRef, rightYellowRef, rightGreenRef } = useStripeAnimation(syncState);
|
||||
@@ -142,6 +144,17 @@ const App: React.FC = () => {
|
||||
// Regex State
|
||||
const [regexReplacements, setRegexReplacements] = useState<RegexReplacement[]>([]);
|
||||
|
||||
// Schedule State
|
||||
const [scheduleSettings, setScheduleSettings] = useState<ScheduleSettings>({
|
||||
mode: ScheduleMode.DISABLED,
|
||||
cronExpression: '',
|
||||
dailyTime: '02:00',
|
||||
weeklyDays: [0], // Sunday
|
||||
weeklyTime: '03:00',
|
||||
autoWatch: false
|
||||
});
|
||||
const [nextRunTime, setNextRunTime] = useState<string | undefined>(undefined);
|
||||
|
||||
// Toast Notification System
|
||||
const [toasts, setToasts] = useState<Toast[]>([]);
|
||||
const timeoutsRef = useRef<{[key: number]: ReturnType<typeof setTimeout>}>({});
|
||||
@@ -154,7 +167,7 @@ const App: React.FC = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const addToast = (message: string) => {
|
||||
const addToast = useCallback((message: string) => {
|
||||
const id = Date.now();
|
||||
// Start with entering: true to position it above
|
||||
const newToast: Toast = { id, message, exiting: false, entering: true };
|
||||
@@ -171,7 +184,7 @@ const App: React.FC = () => {
|
||||
}, TOAST_AUTO_DISMISS_MS);
|
||||
|
||||
timeoutsRef.current[id] = dismissTimer;
|
||||
};
|
||||
}, []);
|
||||
|
||||
// Effect to trigger the "slide down" animation
|
||||
useEffect(() => {
|
||||
@@ -219,6 +232,35 @@ const App: React.FC = () => {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const loadSchedule = useCallback(async () => {
|
||||
const result = await apiService.getScheduleSettings();
|
||||
if (result.status === 'success') {
|
||||
setScheduleSettings(result.data);
|
||||
setNextRunTime(result.data.nextRun);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Handle Schedule Save
|
||||
const handleSaveSchedule = async (settings: ScheduleSettings): Promise<boolean> => {
|
||||
const result = await apiService.saveScheduleSettings(settings);
|
||||
|
||||
if (result.status === 'success') {
|
||||
setScheduleSettings(settings);
|
||||
// Refresh schedule info to get next run time
|
||||
loadSchedule();
|
||||
|
||||
if (settings.mode === ScheduleMode.DISABLED) {
|
||||
addToast("Scheduled tasks disabled.");
|
||||
} else {
|
||||
addToast("Scheduled task updated successfully.");
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
addToast(result.message || "Failed to update schedule.");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Fetch Local Playlists
|
||||
const refreshLocal = useCallback(async () => {
|
||||
if (localAbortRef.current) localAbortRef.current.abort();
|
||||
@@ -280,7 +322,8 @@ const App: React.FC = () => {
|
||||
// Load persisted configuration
|
||||
useEffect(() => {
|
||||
loadSettings();
|
||||
}, [loadSettings]);
|
||||
loadSchedule();
|
||||
}, [loadSettings, loadSchedule]);
|
||||
|
||||
// Initial Load
|
||||
useEffect(() => {
|
||||
@@ -320,9 +363,12 @@ const App: React.FC = () => {
|
||||
if (syncState !== SyncState.IDLE) return;
|
||||
|
||||
setSyncState(SyncState.SYNCING);
|
||||
manualSyncInProgress.current = true;
|
||||
|
||||
const result = await apiService.syncPlaylists(currentStrategy, regexReplacements, localPath || undefined);
|
||||
|
||||
manualSyncInProgress.current = false;
|
||||
|
||||
if (result.status === 'success') {
|
||||
setSyncState(SyncState.SUCCESS);
|
||||
|
||||
@@ -338,6 +384,77 @@ const App: React.FC = () => {
|
||||
}
|
||||
};
|
||||
|
||||
// SSE for sync status
|
||||
useEffect(() => {
|
||||
const eventSource = new EventSource(`${import.meta.env.VITE_API_BASE_URL || ''}/api/sync/events`);
|
||||
|
||||
eventSource.onmessage = (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
const { is_syncing, status, error, last_sync_time } = data;
|
||||
|
||||
// Initialize lastKnownSyncTime if it's the first event
|
||||
if (lastKnownSyncTimeRef.current === undefined) {
|
||||
lastKnownSyncTimeRef.current = last_sync_time;
|
||||
// If we are currently syncing on load, show it
|
||||
if (is_syncing && !manualSyncInProgress.current) {
|
||||
setSyncState(SyncState.SYNCING);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// If manual sync is in progress, we ignore background updates to avoid state conflict
|
||||
if (manualSyncInProgress.current) {
|
||||
if (last_sync_time !== lastKnownSyncTimeRef.current) {
|
||||
lastKnownSyncTimeRef.current = last_sync_time;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle Syncing State
|
||||
if (is_syncing) {
|
||||
if (syncState !== SyncState.SYNCING) {
|
||||
setSyncState(SyncState.SYNCING);
|
||||
}
|
||||
} else {
|
||||
// Check for completion by comparing timestamps
|
||||
if (last_sync_time !== lastKnownSyncTimeRef.current) {
|
||||
lastKnownSyncTimeRef.current = last_sync_time;
|
||||
|
||||
// A sync has completed since our last check
|
||||
if (status === 'success') {
|
||||
setSyncState(SyncState.SUCCESS);
|
||||
refreshLocal();
|
||||
refreshCloud();
|
||||
addToast("Background sync completed successfully.");
|
||||
setTimeout(() => setSyncState(SyncState.IDLE), SYNC_SUCCESS_TOTAL_MS);
|
||||
} else if (status === 'error') {
|
||||
setSyncState(SyncState.ERROR);
|
||||
addToast(`Background sync failed: ${error}`);
|
||||
setTimeout(() => setSyncState(SyncState.IDLE), SYNC_ERROR_RESET_MS);
|
||||
}
|
||||
} else {
|
||||
// Edge case: We are in SYNCING state but backend says not syncing, and time hasn't changed.
|
||||
if (syncState === SyncState.SYNCING) {
|
||||
setSyncState(SyncState.IDLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to parse SSE event", e);
|
||||
}
|
||||
};
|
||||
|
||||
eventSource.onerror = (err) => {
|
||||
console.error("EventSource failed:", err);
|
||||
eventSource.close();
|
||||
};
|
||||
|
||||
return () => {
|
||||
eventSource.close();
|
||||
};
|
||||
}, [syncState, refreshLocal, refreshCloud, addToast]);
|
||||
|
||||
const handleConnectSuccess = async (serverInfo: PlexServerConnection) => {
|
||||
setCloudServerInfo(serverInfo);
|
||||
if (serverInfo.libraryName) {
|
||||
@@ -369,6 +486,33 @@ const App: React.FC = () => {
|
||||
|
||||
const isConnected = cloudServerInfo?.isConnected;
|
||||
|
||||
const getScheduleDisplayInfo = () => {
|
||||
const result = {
|
||||
label: 'Schedule',
|
||||
value: 'Not configured',
|
||||
active: false,
|
||||
autoWatch: scheduleSettings.autoWatch
|
||||
};
|
||||
|
||||
if (scheduleSettings.mode === ScheduleMode.DISABLED) {
|
||||
result.label = 'Auto-Sync';
|
||||
result.value = 'Disabled';
|
||||
return result;
|
||||
}
|
||||
|
||||
let label = 'Schedule';
|
||||
if (scheduleSettings.mode === ScheduleMode.CRON) label = 'Cron Schedule';
|
||||
else if (scheduleSettings.mode === ScheduleMode.DAILY) label = 'Daily Schedule';
|
||||
else if (scheduleSettings.mode === ScheduleMode.WEEKLY) label = 'Weekly Schedule';
|
||||
|
||||
result.label = label;
|
||||
result.value = nextRunTime ? `Next: ${nextRunTime}` : 'Calculating...';
|
||||
result.active = true;
|
||||
return result;
|
||||
};
|
||||
|
||||
const scheduleInfo = getScheduleDisplayInfo();
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex flex-col bg-gray-900 text-gray-100 font-sans overflow-hidden bg-[radial-gradient(ellipse_at_top,_var(--tw-gradient-stops))] from-gray-800 via-gray-900 to-black">
|
||||
|
||||
@@ -440,6 +584,32 @@ const App: React.FC = () => {
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
{/* Normal Toolbar Right */}
|
||||
<div className="flex items-center gap-4">
|
||||
{/* Schedule Info */}
|
||||
<div className="flex flex-col items-end mr-2 md:mr-0 hidden md:flex">
|
||||
<span className="text-[10px] uppercase font-bold text-gray-500 tracking-wider">
|
||||
{scheduleInfo.label}
|
||||
</span>
|
||||
<div className="text-xs font-mono flex items-center gap-1.5">
|
||||
{/* Schedule Part */}
|
||||
<div className={`flex items-center gap-1.5 ${scheduleInfo.active ? 'text-plex-orange' : 'text-gray-600'}`}>
|
||||
{scheduleInfo.active && <Clock size={12} />}
|
||||
<span>{scheduleInfo.value}</span>
|
||||
</div>
|
||||
|
||||
{/* Watch Part */}
|
||||
<span className="text-gray-700 mx-0.5">|</span>
|
||||
<div
|
||||
className={`flex items-center gap-1 ${scheduleInfo.autoWatch ? 'text-plex-orange' : 'text-gray-600'}`}
|
||||
title={scheduleInfo.autoWatch ? "Local Playlist Monitoring Enabled" : "Local Playlist Monitoring Disabled"}
|
||||
>
|
||||
{scheduleInfo.autoWatch ? <Eye size={12} /> : <EyeOff size={12} />}
|
||||
<span className="text-[10px] font-sans font-bold">WATCH</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Connection Status Button */}
|
||||
<button
|
||||
onClick={() => setIsConnectionModalOpen(true)}
|
||||
@@ -452,6 +622,7 @@ const App: React.FC = () => {
|
||||
>
|
||||
{isConnected ? <Server size={18} /> : <ServerOff size={18} />}
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<div className="absolute inset-0 flex items-center justify-center pointer-events-none z-10">
|
||||
@@ -522,6 +693,8 @@ const App: React.FC = () => {
|
||||
onSelect={handleStrategyChange}
|
||||
savedRegexReplacements={regexReplacements}
|
||||
onSaveRegex={handleSaveRegex}
|
||||
savedSchedule={scheduleSettings}
|
||||
onSaveSchedule={handleSaveSchedule}
|
||||
syncState={syncState}
|
||||
onSync={handleSyncTrigger}
|
||||
/>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
import React, { useState, useRef, useEffect } from 'react';
|
||||
import { SyncStrategy, RegexReplacement, SyncState } from '../types';
|
||||
import { SyncStrategy, RegexReplacement, SyncState, ScheduleSettings, ScheduleMode } from '../types';
|
||||
import {
|
||||
ArrowRightCircle,
|
||||
ArrowLeftCircle,
|
||||
@@ -12,8 +11,13 @@ import {
|
||||
Trash2,
|
||||
Save,
|
||||
RotateCcw,
|
||||
Zap,
|
||||
Loader2,
|
||||
Zap
|
||||
Calendar,
|
||||
Clock,
|
||||
Repeat,
|
||||
CheckSquare,
|
||||
Square
|
||||
} from 'lucide-react';
|
||||
|
||||
interface StrategyOption {
|
||||
@@ -55,11 +59,34 @@ const STRATEGIES: StrategyOption[] = [
|
||||
}
|
||||
];
|
||||
|
||||
const WEEK_DAYS = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
|
||||
|
||||
// Helper to determine the actual mode and settings that would be saved based on the current UI state
|
||||
const deriveEffectiveSchedule = (schedule: ScheduleSettings, tab: ScheduleMode): ScheduleSettings => {
|
||||
const derived = { ...schedule };
|
||||
|
||||
if (tab === ScheduleMode.CRON) {
|
||||
derived.mode = derived.cronExpression.trim() !== '' ? ScheduleMode.CRON : ScheduleMode.DISABLED;
|
||||
} else {
|
||||
// For Daily/Weekly
|
||||
// If the mode matches the tab, we keep it (Enabled).
|
||||
// If the mode doesn't match (e.g. it was CRON or DISABLED), then in the context of this tab, it is effectively Disabled until the user checks the box.
|
||||
if (derived.mode === tab) {
|
||||
derived.mode = tab;
|
||||
} else {
|
||||
derived.mode = ScheduleMode.DISABLED;
|
||||
}
|
||||
}
|
||||
return derived;
|
||||
};
|
||||
|
||||
interface StrategySelectorProps {
|
||||
currentStrategy: SyncStrategy;
|
||||
onSelect: (strategy: SyncStrategy, label: string) => void;
|
||||
savedRegexReplacements: RegexReplacement[];
|
||||
onSaveRegex: (replacements: RegexReplacement[]) => void;
|
||||
savedSchedule: ScheduleSettings;
|
||||
onSaveSchedule: (settings: ScheduleSettings) => Promise<boolean>;
|
||||
syncState: SyncState;
|
||||
onSync: () => void;
|
||||
}
|
||||
@@ -69,6 +96,8 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
onSelect,
|
||||
savedRegexReplacements,
|
||||
onSaveRegex,
|
||||
savedSchedule,
|
||||
onSaveSchedule,
|
||||
syncState,
|
||||
onSync
|
||||
}) => {
|
||||
@@ -77,23 +106,50 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
|
||||
// Local state for regex editing
|
||||
const [localReplacements, setLocalReplacements] = useState<RegexReplacement[]>([]);
|
||||
const [isDirty, setIsDirty] = useState(false);
|
||||
const [isRegexDirty, setIsRegexDirty] = useState(false);
|
||||
|
||||
// Local state for Schedule editing
|
||||
const [localSchedule, setLocalSchedule] = useState<ScheduleSettings>(savedSchedule);
|
||||
const [isScheduleDirty, setIsScheduleDirty] = useState(false);
|
||||
|
||||
// UI State for Schedule Tabs
|
||||
// We initialize active tab based on the saved mode. If DISABLED, default to CRON.
|
||||
const [activeTab, setActiveTab] = useState<ScheduleMode>(
|
||||
savedSchedule.mode === ScheduleMode.DISABLED ? ScheduleMode.CRON : savedSchedule.mode
|
||||
);
|
||||
|
||||
const isSyncing = syncState === SyncState.SYNCING;
|
||||
const isLocked = isSyncing || syncState === SyncState.SUCCESS;
|
||||
|
||||
// Initialize local state when prop updates (only if not dirty, or initially)
|
||||
// Initialize local state when prop updates
|
||||
useEffect(() => {
|
||||
setLocalReplacements(JSON.parse(JSON.stringify(savedRegexReplacements)));
|
||||
setIsDirty(false);
|
||||
setIsRegexDirty(false);
|
||||
}, [savedRegexReplacements]);
|
||||
|
||||
useEffect(() => {
|
||||
setLocalSchedule(JSON.parse(JSON.stringify(savedSchedule)));
|
||||
// If the saved mode is not disabled, ensure we show that tab.
|
||||
if (savedSchedule.mode !== ScheduleMode.DISABLED) {
|
||||
setActiveTab(savedSchedule.mode);
|
||||
}
|
||||
setIsScheduleDirty(false);
|
||||
}, [savedSchedule]);
|
||||
|
||||
// Check dirty state whenever local changes
|
||||
useEffect(() => {
|
||||
const isDifferent = JSON.stringify(localReplacements) !== JSON.stringify(savedRegexReplacements);
|
||||
setIsDirty(isDifferent);
|
||||
setIsRegexDirty(isDifferent);
|
||||
}, [localReplacements, savedRegexReplacements]);
|
||||
|
||||
// Check dirty state for Schedule (including Active Tab changes)
|
||||
useEffect(() => {
|
||||
// We calculate what the "effective" schedule would be if we saved right now.
|
||||
const effectiveLocal = deriveEffectiveSchedule(localSchedule, activeTab);
|
||||
const isDifferent = JSON.stringify(effectiveLocal) !== JSON.stringify(savedSchedule);
|
||||
setIsScheduleDirty(isDifferent);
|
||||
}, [localSchedule, savedSchedule, activeTab]);
|
||||
|
||||
const selectedOption = STRATEGIES.find(s => s.value === currentStrategy) || STRATEGIES[0];
|
||||
|
||||
useEffect(() => {
|
||||
@@ -106,45 +162,108 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||
}, []);
|
||||
|
||||
// Determine if tabs have changed from the saved state
|
||||
const initialTab = savedSchedule.mode === ScheduleMode.DISABLED ? ScheduleMode.CRON : savedSchedule.mode;
|
||||
const hasTabChanged = activeTab !== initialTab;
|
||||
const isScheduleActionable = isScheduleDirty || hasTabChanged;
|
||||
|
||||
const handleSelect = (strategy: StrategyOption) => {
|
||||
if (isLocked) return;
|
||||
onSelect(strategy.value, strategy.label);
|
||||
};
|
||||
|
||||
// Regex Handlers
|
||||
// --- Regex Handlers ---
|
||||
const handleAddRegex = () => {
|
||||
if (isLocked) return;
|
||||
const newId = Date.now().toString();
|
||||
setLocalReplacements(prev => [...prev, { id: newId, pattern: '', replacement: '' }]);
|
||||
};
|
||||
|
||||
const handleDeleteRegex = (id: string) => {
|
||||
if (isLocked) return;
|
||||
setLocalReplacements(prev => prev.filter(r => r.id !== id));
|
||||
};
|
||||
|
||||
const handleUpdateRegex = (id: string, field: 'pattern' | 'replacement', value: string) => {
|
||||
if (isLocked) return;
|
||||
setLocalReplacements(prev => prev.map(r =>
|
||||
r.id === id ? { ...r, [field]: value } : r
|
||||
));
|
||||
};
|
||||
|
||||
const handleReset = () => {
|
||||
const handleResetRegex = () => {
|
||||
if (isLocked) return;
|
||||
setLocalReplacements(JSON.parse(JSON.stringify(savedRegexReplacements)));
|
||||
};
|
||||
|
||||
const handleSave = () => {
|
||||
const handleSaveRegex = () => {
|
||||
if (isLocked) return;
|
||||
const validReplacements = localReplacements.filter(r => r.pattern.trim() !== '');
|
||||
setLocalReplacements(validReplacements);
|
||||
onSaveRegex(validReplacements);
|
||||
};
|
||||
|
||||
// --- Schedule Handlers ---
|
||||
const handleUpdateSchedule = (field: keyof ScheduleSettings, value: any) => {
|
||||
if (isLocked) return;
|
||||
setLocalSchedule(prev => ({ ...prev, [field]: value }));
|
||||
};
|
||||
|
||||
const toggleWeekDay = (dayIndex: number) => {
|
||||
if (isLocked) return;
|
||||
const currentDays = localSchedule.weeklyDays;
|
||||
const newDays = currentDays.includes(dayIndex)
|
||||
? currentDays.filter(d => d !== dayIndex)
|
||||
: [...currentDays, dayIndex].sort();
|
||||
handleUpdateSchedule('weeklyDays', newDays);
|
||||
};
|
||||
|
||||
const handleResetSchedule = () => {
|
||||
if (isLocked) return;
|
||||
setLocalSchedule(JSON.parse(JSON.stringify(savedSchedule)));
|
||||
if (savedSchedule.mode !== ScheduleMode.DISABLED) {
|
||||
setActiveTab(savedSchedule.mode);
|
||||
} else {
|
||||
setActiveTab(ScheduleMode.CRON);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSaveScheduleClick = async () => {
|
||||
if (isLocked) return;
|
||||
|
||||
// Determine the effective settings based on the current view (tab) and inputs
|
||||
const settingsToSave = deriveEffectiveSchedule(localSchedule, activeTab);
|
||||
|
||||
// Call API
|
||||
const success = await onSaveSchedule(settingsToSave);
|
||||
if (success) {
|
||||
setLocalSchedule(settingsToSave);
|
||||
// Dirty state is cleared by the useEffect prop update, or we can clear it optimistically here if needed,
|
||||
// but useEffect [savedSchedule] handles it correctly.
|
||||
}
|
||||
};
|
||||
|
||||
const handleSyncClick = () => {
|
||||
if (isLocked || isDirty) return;
|
||||
if (isLocked) return;
|
||||
onSync();
|
||||
};
|
||||
|
||||
// Helper to toggle enable/disable for current active tab (Daily/Weekly)
|
||||
const toggleScheduleEnable = (targetMode: ScheduleMode) => {
|
||||
if (isLocked) return;
|
||||
if (localSchedule.mode === targetMode) {
|
||||
handleUpdateSchedule('mode', ScheduleMode.DISABLED);
|
||||
} else {
|
||||
handleUpdateSchedule('mode', targetMode);
|
||||
}
|
||||
};
|
||||
|
||||
// If syncing or locked, apply grayscale filter to content sections
|
||||
const contentClass = isLocked ? "opacity-50 pointer-events-none grayscale transition-all" : "transition-all";
|
||||
|
||||
return (
|
||||
<div className={`relative group ${isLocked ? 'opacity-80' : ''}`} ref={dropdownRef}>
|
||||
{/* Trigger Button - Added Ring to create visual 'cutout' over panels */}
|
||||
<div className="relative group" ref={dropdownRef}>
|
||||
{/* Trigger Button */}
|
||||
<button
|
||||
onClick={() => setIsOpen(!isOpen)}
|
||||
className="flex items-center justify-center w-12 h-12 rounded-full bg-gray-800/90 border border-gray-600 hover:border-plex-orange text-gray-300 hover:text-white hover:bg-gray-700/80 transition-all shadow-2xl hover:shadow-plex-orange/30 ring-[6px] md:ring-8 ring-gray-900 backdrop-blur-sm active:scale-95"
|
||||
@@ -156,7 +275,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
</div>
|
||||
</button>
|
||||
|
||||
{/* Dropdown Menu - Persistent Mount for State Preservation */}
|
||||
{/* Dropdown Menu */}
|
||||
<div
|
||||
className={`absolute
|
||||
top-14
|
||||
@@ -165,12 +284,14 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
/* Desktop: Center alignment */
|
||||
md:left-1/2 md:right-auto md:origin-top md:-translate-x-1/2
|
||||
|
||||
w-80 md:w-[30rem] bg-gray-800/95 border border-white/10 rounded-xl shadow-2xl z-50 overflow-hidden backdrop-blur-xl
|
||||
w-80 md:w-[32rem] bg-gray-800/95 border border-white/10 rounded-xl shadow-2xl z-50 overflow-hidden backdrop-blur-xl
|
||||
transition-all duration-200 ease-out
|
||||
${isOpen ? 'opacity-100 scale-100 visible translate-y-0' : 'opacity-0 scale-95 invisible pointer-events-none -translate-y-2'}`}
|
||||
>
|
||||
<div className={`flex flex-col max-h-[75vh] md:max-h-[85vh] overflow-y-auto custom-scrollbar ${contentClass}`}>
|
||||
|
||||
{/* Section 1: Sync Strategy */}
|
||||
<div className="px-4 py-3 bg-black/20 border-b border-white/5">
|
||||
<div className="px-4 py-3 bg-black/20 border-b border-white/5 flex-none">
|
||||
<h3 className="text-[10px] font-bold text-gray-500 uppercase tracking-widest mb-2">Sync Strategy</h3>
|
||||
<div className="space-y-1">
|
||||
{STRATEGIES.map((strategy) => (
|
||||
@@ -208,7 +329,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
</div>
|
||||
|
||||
{/* Section 2: Regex Preprocessing */}
|
||||
<div className="p-4 bg-gray-900/40">
|
||||
<div className="p-4 bg-gray-900/40 border-b border-white/5 flex-none">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<h3 className="text-[10px] font-bold text-gray-500 uppercase tracking-widest">Regex Rules</h3>
|
||||
{localReplacements.length === 0 && (
|
||||
@@ -222,9 +343,9 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2 mb-4 max-h-52 overflow-y-auto pr-1 custom-scrollbar">
|
||||
<div className="space-y-2 mb-4 max-h-40 overflow-y-auto pr-1 custom-scrollbar">
|
||||
{localReplacements.length === 0 ? (
|
||||
<div className="text-xs text-gray-600 italic text-center py-4 border border-dashed border-gray-700/50 rounded-lg">
|
||||
<div className="text-xs text-gray-600 italic text-center py-2 border border-dashed border-gray-700/50 rounded-lg">
|
||||
No regex replacements configured.
|
||||
</div>
|
||||
) : (
|
||||
@@ -233,12 +354,11 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
<div className="flex-1 min-w-0">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Regex Pattern"
|
||||
placeholder="Pattern"
|
||||
value={regex.pattern}
|
||||
onChange={(e) => handleUpdateRegex(regex.id, 'pattern', e.target.value)}
|
||||
disabled={isLocked}
|
||||
className={`w-full bg-gray-900/80 border rounded-md px-2.5 py-1.5 text-xs text-gray-200 focus:outline-none focus:ring-1 transition-all placeholder-gray-600 disabled:opacity-60 disabled:cursor-not-allowed
|
||||
${!regex.pattern && isDirty ? 'border-red-500/30 focus:border-red-500' : 'border-gray-700 focus:border-plex-orange'}`}
|
||||
className={`w-full bg-gray-900/80 border rounded-md px-2 py-1.5 text-xs text-gray-200 focus:outline-none focus:ring-1 transition-all placeholder-gray-600
|
||||
${!regex.pattern && isRegexDirty ? 'border-red-500/30 focus:border-red-500' : 'border-gray-700 focus:border-plex-orange'}`}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex-none text-gray-600">
|
||||
@@ -250,14 +370,12 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
placeholder="Replacement"
|
||||
value={regex.replacement}
|
||||
onChange={(e) => handleUpdateRegex(regex.id, 'replacement', e.target.value)}
|
||||
disabled={isLocked}
|
||||
className="w-full bg-gray-900/80 border border-gray-700 rounded-md px-2.5 py-1.5 text-xs text-gray-200 focus:outline-none focus:border-plex-orange focus:ring-1 focus:ring-plex-orange transition-all placeholder-gray-600 disabled:opacity-60 disabled:cursor-not-allowed"
|
||||
className="w-full bg-gray-900/80 border border-gray-700 rounded-md px-2 py-1.5 text-xs text-gray-200 focus:outline-none focus:border-plex-orange focus:ring-1 focus:ring-plex-orange transition-all placeholder-gray-600"
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => handleDeleteRegex(regex.id)}
|
||||
disabled={isLocked}
|
||||
className="text-gray-600 hover:text-red-400 p-1.5 hover:bg-red-500/10 rounded transition-colors disabled:opacity-60 disabled:cursor-not-allowed"
|
||||
className="text-gray-600 hover:text-red-400 p-1.5 hover:bg-red-500/10 rounded transition-colors"
|
||||
title="Delete Rule"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
@@ -267,57 +385,221 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="space-y-3 pt-3 border-t border-white/5">
|
||||
{localReplacements.length > 0 && (
|
||||
<div className="flex justify-center">
|
||||
<div className="flex justify-between items-center gap-2">
|
||||
<button
|
||||
onClick={handleAddRegex}
|
||||
disabled={isLocked}
|
||||
className="flex items-center space-x-1.5 text-xs text-plex-orange hover:text-yellow-400 transition-colors opacity-80 hover:opacity-100 disabled:opacity-40 disabled:cursor-not-allowed"
|
||||
className={`flex items-center space-x-1 px-2 py-1 rounded text-[10px] font-bold uppercase tracking-wide transition-colors ${localReplacements.length > 0 ? 'text-plex-orange hover:bg-plex-orange/10' : 'hidden'}`}
|
||||
>
|
||||
<Plus size={12} />
|
||||
<span className="font-medium">Add Rule</span>
|
||||
<Plus size={10} />
|
||||
<span>Add</span>
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div className="flex items-center gap-2 ml-auto">
|
||||
<button
|
||||
onClick={handleReset}
|
||||
disabled={!isDirty || isLocked}
|
||||
className={`flex items-center justify-center space-x-2 py-1.5 rounded-lg text-xs font-medium border transition-all
|
||||
${isDirty && !isLocked
|
||||
onClick={handleResetRegex}
|
||||
disabled={!isRegexDirty}
|
||||
className={`flex items-center justify-center space-x-1.5 px-3 py-1.5 rounded-md text-xs font-medium border transition-all
|
||||
${isRegexDirty
|
||||
? 'bg-gray-800 border-gray-600 text-gray-300 hover:bg-gray-700 hover:text-white'
|
||||
: 'bg-transparent border-gray-800 text-gray-600 cursor-not-allowed'}`}
|
||||
: 'bg-transparent border-transparent text-gray-700 cursor-not-allowed'}`}
|
||||
>
|
||||
<RotateCcw size={14} />
|
||||
<RotateCcw size={12} />
|
||||
<span>Revert</span>
|
||||
</button>
|
||||
<button
|
||||
onClick={handleSave}
|
||||
disabled={!isDirty || isLocked}
|
||||
className={`flex items-center justify-center space-x-2 py-1.5 rounded-lg text-xs font-bold border transition-all
|
||||
${isDirty && !isLocked
|
||||
onClick={handleSaveRegex}
|
||||
disabled={!isRegexDirty}
|
||||
className={`flex items-center justify-center space-x-1.5 px-3 py-1.5 rounded-md text-xs font-bold border transition-all
|
||||
${isRegexDirty
|
||||
? 'bg-plex-orange border-plex-orange text-gray-900 hover:bg-yellow-500 shadow-lg shadow-plex-orange/10'
|
||||
: 'bg-gray-800/50 border-gray-800 text-gray-600 cursor-not-allowed'}`}
|
||||
: 'bg-gray-800/30 border-gray-800/50 text-gray-600 cursor-not-allowed'}`}
|
||||
>
|
||||
<Save size={14} />
|
||||
<span>Save Changes</span>
|
||||
<Save size={12} />
|
||||
<span>Save</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Section 3: Sync Now Button */}
|
||||
<div className="p-4 bg-gray-950/50 border-t border-white/5">
|
||||
{/* Section 3: Scheduled Tasks */}
|
||||
<div className="p-4 bg-gray-900/40 border-b border-white/5 flex-none">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<h3 className="text-[10px] font-bold text-gray-500 uppercase tracking-widest">Scheduled Tasks</h3>
|
||||
</div>
|
||||
|
||||
{/* Tabs */}
|
||||
<div className="flex space-x-1 bg-black/30 p-1 rounded-lg mb-4">
|
||||
{[
|
||||
{ id: ScheduleMode.CRON, label: 'Cron', icon: Repeat },
|
||||
{ id: ScheduleMode.DAILY, label: 'Daily', icon: Clock },
|
||||
{ id: ScheduleMode.WEEKLY, label: 'Weekly', icon: Calendar },
|
||||
].map((tab) => (
|
||||
<button
|
||||
key={tab.id}
|
||||
onClick={() => setActiveTab(tab.id)}
|
||||
className={`flex-1 flex items-center justify-center space-x-2 py-1.5 rounded-md text-xs font-medium transition-all
|
||||
${activeTab === tab.id
|
||||
? 'bg-gray-700 text-plex-orange shadow-sm'
|
||||
: 'text-gray-400 hover:text-gray-200 hover:bg-white/5'
|
||||
}`}
|
||||
>
|
||||
<tab.icon size={12} />
|
||||
<span>{tab.label}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Tab Content */}
|
||||
<div className="mb-4 min-h-[50px]">
|
||||
{activeTab === ScheduleMode.CRON && (
|
||||
<div className="space-y-2 animate-in fade-in duration-200">
|
||||
<div className="flex items-center space-x-2">
|
||||
<span className="text-gray-500 font-mono text-xs">Cron:</span>
|
||||
<input
|
||||
type="text"
|
||||
value={localSchedule.cronExpression}
|
||||
onChange={(e) => handleUpdateSchedule('cronExpression', e.target.value)}
|
||||
placeholder="0 0 * * *"
|
||||
className="flex-1 bg-gray-800 border border-gray-700 rounded-md px-2.5 py-1.5 text-xs text-gray-200 font-mono focus:border-plex-orange focus:outline-none focus:ring-1 focus:ring-plex-orange placeholder-gray-600"
|
||||
/>
|
||||
</div>
|
||||
<p className="text-[10px] text-gray-500">
|
||||
Unix-cron format. Leave empty to disable schedule.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{activeTab === ScheduleMode.DAILY && (
|
||||
<div className="flex flex-col animate-in fade-in duration-200">
|
||||
{/* Top Row: Checkbox + Label */}
|
||||
<div className="flex items-center justify-start space-x-2 mb-2">
|
||||
<button
|
||||
onClick={() => toggleScheduleEnable(ScheduleMode.DAILY)}
|
||||
className={`transition-colors flex-none ${localSchedule.mode === ScheduleMode.DAILY ? 'text-plex-orange' : 'text-gray-500 hover:text-gray-400'}`}
|
||||
title={localSchedule.mode === ScheduleMode.DAILY ? "Schedule Enabled" : "Schedule Disabled"}
|
||||
>
|
||||
{localSchedule.mode === ScheduleMode.DAILY ? <CheckSquare size={16} /> : <Square size={16} />}
|
||||
</button>
|
||||
<label className="text-xs text-gray-400 font-medium">Run daily at:</label>
|
||||
</div>
|
||||
|
||||
{/* Bottom Row: Centered Native Time Input */}
|
||||
<div className="flex justify-center mt-2">
|
||||
<input
|
||||
type="time"
|
||||
value={localSchedule.dailyTime}
|
||||
onChange={(e) => handleUpdateSchedule('dailyTime', e.target.value)}
|
||||
disabled={localSchedule.mode !== ScheduleMode.DAILY}
|
||||
className={`bg-gray-800 border border-gray-700 text-white rounded-md px-4 py-2 text-xl font-mono focus:border-plex-orange focus:ring-1 focus:ring-plex-orange focus:outline-none transition-all ${localSchedule.mode !== ScheduleMode.DAILY ? 'opacity-50 cursor-not-allowed' : ''}`}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{activeTab === ScheduleMode.WEEKLY && (
|
||||
<div className="flex flex-col animate-in fade-in duration-200">
|
||||
{/* Top Row: Checkbox + Label */}
|
||||
<div className="flex items-center justify-start space-x-2 mb-2">
|
||||
<button
|
||||
onClick={() => toggleScheduleEnable(ScheduleMode.WEEKLY)}
|
||||
className={`transition-colors flex-none ${localSchedule.mode === ScheduleMode.WEEKLY ? 'text-plex-orange' : 'text-gray-500 hover:text-gray-400'}`}
|
||||
title={localSchedule.mode === ScheduleMode.WEEKLY ? "Schedule Enabled" : "Schedule Disabled"}
|
||||
>
|
||||
{localSchedule.mode === ScheduleMode.WEEKLY ? <CheckSquare size={16} /> : <Square size={16} />}
|
||||
</button>
|
||||
<label className="text-xs text-gray-400 font-medium">Run on days:</label>
|
||||
</div>
|
||||
|
||||
{/* Middle Row: Full Width Capsules */}
|
||||
<div className={`grid grid-cols-7 w-full transition-opacity duration-200 mb-3 ${localSchedule.mode !== ScheduleMode.WEEKLY ? 'opacity-50 pointer-events-none' : ''}`}>
|
||||
{WEEK_DAYS.map((day, index) => {
|
||||
const isSelected = localSchedule.weeklyDays.includes(index);
|
||||
return (
|
||||
<button
|
||||
key={index}
|
||||
onClick={() => toggleWeekDay(index)}
|
||||
className={`h-9 text-xs font-bold border-y border-l last:border-r border-gray-600/50 transition-colors
|
||||
first:rounded-l-lg last:rounded-r-lg
|
||||
${isSelected
|
||||
? 'bg-plex-orange text-gray-900 border-plex-orange z-10'
|
||||
: 'bg-gray-800 text-gray-400 hover:bg-gray-700 hover:text-gray-200'
|
||||
}
|
||||
`}
|
||||
>
|
||||
{day}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
{/* Bottom Row: Centered Native Time Input */}
|
||||
<div className="flex justify-center mt-1">
|
||||
<input
|
||||
type="time"
|
||||
value={localSchedule.weeklyTime}
|
||||
onChange={(e) => handleUpdateSchedule('weeklyTime', e.target.value)}
|
||||
disabled={localSchedule.mode !== ScheduleMode.WEEKLY}
|
||||
className={`bg-gray-800 border border-gray-700 text-white rounded-md px-4 py-2 text-xl font-mono focus:border-plex-orange focus:ring-1 focus:ring-plex-orange focus:outline-none transition-all ${localSchedule.mode !== ScheduleMode.WEEKLY ? 'opacity-50 cursor-not-allowed' : ''}`}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Auto Watch Checkbox */}
|
||||
<div className="flex items-center mb-4 px-1">
|
||||
<button
|
||||
onClick={() => handleUpdateSchedule('autoWatch', !localSchedule.autoWatch)}
|
||||
className="flex items-center space-x-2 group"
|
||||
>
|
||||
{localSchedule.autoWatch ? (
|
||||
<CheckSquare size={16} className="text-plex-orange" />
|
||||
) : (
|
||||
<Square size={16} className="text-gray-600 group-hover:text-gray-400" />
|
||||
)}
|
||||
<span className={`text-xs ${localSchedule.autoWatch ? 'text-gray-200' : 'text-gray-500 group-hover:text-gray-400'}`}>
|
||||
Watch for local playlist changes
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Action Buttons (Mirrored from Regex) */}
|
||||
<div className="flex items-center gap-2 justify-end pt-3 border-t border-white/5">
|
||||
<button
|
||||
onClick={handleResetSchedule}
|
||||
disabled={!isScheduleActionable}
|
||||
className={`flex items-center justify-center space-x-1.5 px-3 py-1.5 rounded-md text-xs font-medium border transition-all
|
||||
${isScheduleActionable
|
||||
? 'bg-gray-800 border-gray-600 text-gray-300 hover:bg-gray-700 hover:text-white'
|
||||
: 'bg-transparent border-transparent text-gray-700 cursor-not-allowed'}`}
|
||||
>
|
||||
<RotateCcw size={12} />
|
||||
<span>Revert</span>
|
||||
</button>
|
||||
<button
|
||||
onClick={handleSaveScheduleClick}
|
||||
disabled={!isScheduleActionable}
|
||||
className={`flex items-center justify-center space-x-1.5 px-3 py-1.5 rounded-md text-xs font-bold border transition-all
|
||||
${isScheduleActionable
|
||||
? 'bg-plex-orange border-plex-orange text-gray-900 hover:bg-yellow-500 shadow-lg shadow-plex-orange/10'
|
||||
: 'bg-gray-800/30 border-gray-800/50 text-gray-600 cursor-not-allowed'}`}
|
||||
>
|
||||
<Save size={12} />
|
||||
<span>Save</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Section 4: Sync Now Button */}
|
||||
<div className="p-4 bg-gray-950/50 border-t border-white/5 flex-none">
|
||||
<button
|
||||
onClick={handleSyncClick}
|
||||
disabled={isLocked || isDirty}
|
||||
disabled={isLocked}
|
||||
className={`w-full py-3 rounded-lg text-sm font-bold flex items-center justify-center gap-2 transition-all shadow-lg
|
||||
${isLocked
|
||||
? 'bg-gray-700/30 text-gray-500 cursor-not-allowed border border-gray-700/50'
|
||||
: isDirty
|
||||
: isRegexDirty
|
||||
? 'bg-gray-800 text-gray-500 cursor-not-allowed border border-gray-700'
|
||||
: 'bg-green-600 hover:bg-green-500 text-white border border-green-500 shadow-green-900/20 active:scale-[0.98]'
|
||||
}`}
|
||||
@@ -334,9 +616,9 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
{isDirty && (
|
||||
{(isRegexDirty) && (
|
||||
<p className="text-[10px] text-plex-orange text-center mt-2">
|
||||
Please save or revert regex rules changes before syncing.
|
||||
Please save regex changes before syncing.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Playlist, ServerType, ApiResponse, PlexServerConnection, PlexConnectionSettings, PlexLibrary, RegexReplacement, SyncStrategy } from '../types';
|
||||
import { Playlist, ServerType, ApiResponse, PlexServerConnection, PlexConnectionSettings, PlexLibrary, RegexReplacement, SyncStrategy, ScheduleSettings } from '../types';
|
||||
|
||||
const API_BASE = import.meta.env.VITE_API_BASE_URL || '';
|
||||
|
||||
@@ -97,6 +97,20 @@ export const apiService = {
|
||||
return handleResponse(response);
|
||||
},
|
||||
|
||||
async getScheduleSettings(): Promise<ApiResponse<ScheduleSettings & { nextRun?: string }>> {
|
||||
const response = await fetch(`${API_BASE}/api/schedule`);
|
||||
return handleResponse(response);
|
||||
},
|
||||
|
||||
async saveScheduleSettings(settings: ScheduleSettings): Promise<ApiResponse<null>> {
|
||||
const response = await fetch(`${API_BASE}/api/schedule`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(settings),
|
||||
});
|
||||
return handleResponse(response);
|
||||
},
|
||||
|
||||
async getPlaylists(serverType: ServerType, signal?: AbortSignal, localPath?: string): Promise<ApiResponse<Playlist[]>> {
|
||||
const params = new URLSearchParams({ server: serverType.toLowerCase() });
|
||||
if (serverType === ServerType.LOCAL && localPath) {
|
||||
@@ -167,4 +181,9 @@ export const apiService = {
|
||||
});
|
||||
return handleResponse(response);
|
||||
},
|
||||
|
||||
async getSyncStatus(): Promise<ApiResponse<{ is_syncing: boolean; last_sync_time: string | null; status: string; error: string | null }>> {
|
||||
const response = await fetch(`${API_BASE}/api/sync/status`);
|
||||
return handleResponse(response);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -40,6 +40,22 @@ export interface RegexReplacement {
|
||||
replacement: string;
|
||||
}
|
||||
|
||||
export enum ScheduleMode {
|
||||
DISABLED = 'DISABLED',
|
||||
CRON = 'CRON',
|
||||
DAILY = 'DAILY',
|
||||
WEEKLY = 'WEEKLY'
|
||||
}
|
||||
|
||||
export interface ScheduleSettings {
|
||||
mode: ScheduleMode;
|
||||
cronExpression: string;
|
||||
dailyTime: string;
|
||||
weeklyDays: number[]; // 0 = Sunday, 1 = Monday, etc.
|
||||
weeklyTime: string;
|
||||
autoWatch: boolean;
|
||||
}
|
||||
|
||||
export interface PlexLibrary {
|
||||
id: string;
|
||||
title: string;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 1 - Local playlist
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\ピンポン - ED - 僕らについて\01. “あのヒーローと” 僕らについて - ピンポン - ED - 僕らについて.mp3
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 2 - Local playlist
|
||||
# A comment that should be ignored
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
N:\Music\Anime\ダンジョン飯\Party!! (期間生産限定盤)\01. Party!! - Party!! (期間生産限定盤).flac
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
# Case 3 - Local playlist
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 4 - Local playlist
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
@@ -0,0 +1,5 @@
|
||||
#EXTM3U
|
||||
/mnt/music/Anime/CITY THE ANIMATION/Hello/01. Hello - Hello.flac
|
||||
\\koha9-nas\koha9-nas\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\02. Reckless - Theme of New PANTY & STOCKING.flac
|
||||
N:\MUSIC\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\音ゲー\USAO\MEMORIES\11. Echoes - MEMORIES.flac
|
||||
@@ -4,3 +4,5 @@ jinja2
|
||||
python-multipart
|
||||
plexapi
|
||||
merge3
|
||||
apscheduler
|
||||
watchdog
|
||||
|
||||
+69
-13
@@ -1,7 +1,5 @@
|
||||
|
||||
|
||||
import React, { useEffect, useState, useCallback, useRef } from 'react';
|
||||
import { Playlist, ServerType, SyncStrategy, PlexServerConnection, RegexReplacement, SyncState, ScheduleSettings, ScheduleMode } from './types';
|
||||
import { Playlist, ServerType, SyncStrategy, PlexServerConnection, PathMappingConfig, PathMappingMode, SyncState, ScheduleSettings, ScheduleMode } from './types';
|
||||
import { apiService } from './services/api';
|
||||
import {
|
||||
STRIPE_BASE_SPEED,
|
||||
@@ -17,7 +15,7 @@ import { SYNC_BANNER_PADDING_X, SYNC_BANNER_PADDING_Y, SYNC_BANNER_MIN_WIDTH } f
|
||||
import ServerPanel from './components/ServerPanel';
|
||||
import StrategySelector from './components/StrategySelector';
|
||||
import ConnectionModal from './components/ConnectionModal';
|
||||
import { ArrowLeftRight, ShieldCheck, X, Server, ServerOff, Clock, Eye, EyeOff } from 'lucide-react';
|
||||
import { ArrowLeftRight, ShieldCheck, X, Server, ServerOff, Clock, Eye, EyeOff, Type, Code2 } from 'lucide-react';
|
||||
|
||||
interface Toast {
|
||||
id: number;
|
||||
@@ -137,8 +135,17 @@ const App: React.FC = () => {
|
||||
// Strategy State
|
||||
const [currentStrategy, setCurrentStrategy] = useState<SyncStrategy>(SyncStrategy.LOCAL_OVERWRITE);
|
||||
|
||||
// Regex State
|
||||
const [regexReplacements, setRegexReplacements] = useState<RegexReplacement[]>([]);
|
||||
// Path Mapping State (Includes Simple and Regex Rules)
|
||||
const [pathMappingConfig, setPathMappingConfig] = useState<PathMappingConfig>({
|
||||
mode: PathMappingMode.SIMPLE,
|
||||
simple: [],
|
||||
regex: {
|
||||
localPre: [],
|
||||
localPost: [],
|
||||
remotePre: [],
|
||||
remotePost: []
|
||||
}
|
||||
});
|
||||
|
||||
// Schedule State
|
||||
const [scheduleSettings, setScheduleSettings] = useState<ScheduleSettings>({
|
||||
@@ -292,10 +299,10 @@ const App: React.FC = () => {
|
||||
addToast(`Selected strategy "${label}" has been saved.`);
|
||||
};
|
||||
|
||||
// Handle Regex Save
|
||||
const handleSaveRegex = (replacements: RegexReplacement[]) => {
|
||||
setRegexReplacements(replacements);
|
||||
addToast('Regex preprocessing rules have been saved.');
|
||||
// Handle Path Mapping Save
|
||||
const handleSavePathMapping = (config: PathMappingConfig) => {
|
||||
setPathMappingConfig(config);
|
||||
addToast('Path mapping rules have been saved.');
|
||||
};
|
||||
|
||||
// Handle Schedule Save
|
||||
@@ -328,7 +335,7 @@ const App: React.FC = () => {
|
||||
setSyncState(SyncState.SYNCING);
|
||||
|
||||
// Note: We deliberately do not clear playlists here to keep UI populated during sync
|
||||
const result = await apiService.syncPlaylists(currentStrategy, regexReplacements);
|
||||
const result = await apiService.syncPlaylists(currentStrategy, pathMappingConfig);
|
||||
|
||||
if (result.status === 'success') {
|
||||
// Transition to Success state
|
||||
@@ -478,6 +485,44 @@ const App: React.FC = () => {
|
||||
|
||||
const scheduleInfo = getScheduleDisplayInfo(scheduleSettings);
|
||||
|
||||
// Helper: Calculate Path Mapping Info
|
||||
const getPathMappingDisplayInfo = (config: PathMappingConfig) => {
|
||||
let count = 0;
|
||||
let modeLabel = '';
|
||||
let Icon = Type;
|
||||
|
||||
if (config.mode === PathMappingMode.SIMPLE) {
|
||||
modeLabel = 'Simple';
|
||||
count = config.simple.length;
|
||||
Icon = Type;
|
||||
} else {
|
||||
modeLabel = 'Regex';
|
||||
count = config.regex.localPre.length +
|
||||
config.regex.localPost.length +
|
||||
config.regex.remotePre.length +
|
||||
config.regex.remotePost.length;
|
||||
Icon = Code2;
|
||||
}
|
||||
|
||||
if (count === 0) {
|
||||
return {
|
||||
label: 'Path Mapping',
|
||||
value: 'Not Set',
|
||||
active: false,
|
||||
Icon: Icon
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
label: 'Path Mapping',
|
||||
value: `${modeLabel} (${count})`,
|
||||
active: true,
|
||||
Icon: Icon
|
||||
};
|
||||
};
|
||||
|
||||
const pathMappingInfo = getPathMappingDisplayInfo(pathMappingConfig);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex flex-col bg-gray-900 text-gray-100 font-sans overflow-hidden bg-[radial-gradient(ellipse_at_top,_var(--tw-gradient-stops))] from-gray-800 via-gray-900 to-black">
|
||||
|
||||
@@ -555,6 +600,17 @@ const App: React.FC = () => {
|
||||
|
||||
{/* Normal Toolbar Right */}
|
||||
<div className="flex items-center gap-4">
|
||||
{/* Path Mapping Info */}
|
||||
<div className="flex flex-col items-end hidden md:flex border-r border-gray-700/50 pr-4 mr-1">
|
||||
<span className="text-[10px] uppercase font-bold text-gray-500 tracking-wider">
|
||||
{pathMappingInfo.label}
|
||||
</span>
|
||||
<div className={`text-xs font-mono flex items-center gap-1.5 ${pathMappingInfo.active ? 'text-plex-orange' : 'text-gray-600'}`}>
|
||||
{pathMappingInfo.active && <pathMappingInfo.Icon size={12} />}
|
||||
<span>{pathMappingInfo.value}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Schedule Info */}
|
||||
<div className="flex flex-col items-end mr-2 md:mr-0 hidden md:flex">
|
||||
<span className="text-[10px] uppercase font-bold text-gray-500 tracking-wider">
|
||||
@@ -661,8 +717,8 @@ const App: React.FC = () => {
|
||||
<StrategySelector
|
||||
currentStrategy={currentStrategy}
|
||||
onSelect={handleStrategyChange}
|
||||
savedRegexReplacements={regexReplacements}
|
||||
onSaveRegex={handleSaveRegex}
|
||||
savedPathMapping={pathMappingConfig}
|
||||
onSavePathMapping={handleSavePathMapping}
|
||||
savedSchedule={scheduleSettings}
|
||||
onSaveSchedule={handleSaveSchedule}
|
||||
syncState={syncState}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
import React, { useState, useRef, useEffect } from 'react';
|
||||
import { SyncStrategy, RegexReplacement, SyncState, ScheduleSettings, ScheduleMode } from '../types';
|
||||
import { SyncStrategy, ReplacementRule, PathMappingConfig, PathMappingRules, PathMappingMode, SyncState, ScheduleSettings, ScheduleMode } from '../types';
|
||||
import {
|
||||
ArrowRightCircle,
|
||||
ArrowLeftCircle,
|
||||
@@ -18,7 +17,9 @@ import {
|
||||
Clock,
|
||||
Repeat,
|
||||
CheckSquare,
|
||||
Square
|
||||
Square,
|
||||
Type,
|
||||
Code2
|
||||
} from 'lucide-react';
|
||||
|
||||
interface StrategyOption {
|
||||
@@ -62,6 +63,29 @@ const STRATEGIES: StrategyOption[] = [
|
||||
|
||||
const WEEK_DAYS = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
|
||||
|
||||
// Color Theme Variables for Mapping Editors
|
||||
const MAPPING_THEME = {
|
||||
// Container Themes
|
||||
local: {
|
||||
borderColor: "border-blue-500/20",
|
||||
bgColor: "bg-blue-900/10"
|
||||
},
|
||||
remote: {
|
||||
borderColor: "border-green-500/20",
|
||||
bgColor: "bg-green-900/10"
|
||||
},
|
||||
simple: {
|
||||
borderColor: "border-gray-700/50",
|
||||
bgColor: "bg-gray-900/40"
|
||||
},
|
||||
// Input Field Themes
|
||||
inputs: {
|
||||
default: "bg-gray-800 border-gray-700 text-gray-200 focus:border-plex-orange placeholder-gray-600",
|
||||
local: "bg-blue-500/10 border-blue-500/30 text-blue-100 focus:border-blue-400 placeholder-blue-300/30",
|
||||
cloud: "bg-green-500/10 border-green-500/30 text-green-100 focus:border-green-400 placeholder-green-300/30"
|
||||
}
|
||||
};
|
||||
|
||||
// Helper to determine the actual mode and settings that would be saved based on the current UI state
|
||||
const deriveEffectiveSchedule = (schedule: ScheduleSettings, tab: ScheduleMode): ScheduleSettings => {
|
||||
const derived = { ...schedule };
|
||||
@@ -81,11 +105,114 @@ const deriveEffectiveSchedule = (schedule: ScheduleSettings, tab: ScheduleMode):
|
||||
return derived;
|
||||
};
|
||||
|
||||
// Sub-component for a single Mapping Group Editor
|
||||
interface MappingGroupEditorProps {
|
||||
title: string;
|
||||
subtitle?: string;
|
||||
rules: ReplacementRule[];
|
||||
onChange: (newRules: ReplacementRule[]) => void;
|
||||
isLocked: boolean;
|
||||
borderColor?: string;
|
||||
bgColor?: string;
|
||||
// Input specific props
|
||||
leftPlaceholder?: string;
|
||||
rightPlaceholder?: string;
|
||||
leftInputClass?: string;
|
||||
rightInputClass?: string;
|
||||
}
|
||||
|
||||
const MappingGroupEditor: React.FC<MappingGroupEditorProps> = ({
|
||||
title,
|
||||
subtitle,
|
||||
rules,
|
||||
onChange,
|
||||
isLocked,
|
||||
borderColor = "border-gray-700",
|
||||
bgColor = "bg-gray-900/50",
|
||||
leftPlaceholder = "Pattern",
|
||||
rightPlaceholder = "Replace",
|
||||
leftInputClass,
|
||||
rightInputClass
|
||||
}) => {
|
||||
|
||||
const handleAdd = () => {
|
||||
if (isLocked) return;
|
||||
const newId = Date.now().toString() + Math.random().toString();
|
||||
onChange([...rules, { id: newId, search: '', replace: '' }]);
|
||||
};
|
||||
|
||||
const handleUpdate = (id: string, field: 'search' | 'replace', value: string) => {
|
||||
if (isLocked) return;
|
||||
onChange(rules.map(r => r.id === id ? { ...r, [field]: value } : r));
|
||||
};
|
||||
|
||||
const handleDelete = (id: string) => {
|
||||
if (isLocked) return;
|
||||
onChange(rules.filter(r => r.id !== id));
|
||||
};
|
||||
|
||||
// Default input style if not provided
|
||||
const defaultInputStyle = MAPPING_THEME.inputs.default;
|
||||
|
||||
return (
|
||||
<div className={`p-3 rounded-lg border ${borderColor} ${bgColor} flex flex-col h-full transition-colors`}>
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<div>
|
||||
<h4 className="text-[10px] font-bold uppercase tracking-wider text-gray-400">{title}</h4>
|
||||
{subtitle && <p className="text-[9px] text-gray-500">{subtitle}</p>}
|
||||
</div>
|
||||
<button
|
||||
onClick={handleAdd}
|
||||
disabled={isLocked}
|
||||
className="p-1 rounded bg-gray-700/50 hover:bg-gray-600 text-gray-400 hover:text-white transition-colors"
|
||||
title="Add Rule"
|
||||
>
|
||||
<Plus size={12} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 space-y-2 overflow-y-auto max-h-32 custom-scrollbar pr-1">
|
||||
{rules.length === 0 ? (
|
||||
<div className="text-xs text-gray-600 italic text-center py-2 border border-dashed border-gray-700/50 rounded-lg">
|
||||
No rules defined.
|
||||
</div>
|
||||
) : (
|
||||
rules.map((rule) => (
|
||||
<div key={rule.id} className="flex items-center space-x-1 animate-in slide-in-from-left-1 duration-200">
|
||||
<input
|
||||
type="text"
|
||||
placeholder={leftPlaceholder}
|
||||
value={rule.search}
|
||||
onChange={(e) => handleUpdate(rule.id, 'search', e.target.value)}
|
||||
className={`flex-1 min-w-0 border rounded px-1.5 py-1 text-xs focus:outline-none transition-colors ${leftInputClass || defaultInputStyle}`}
|
||||
/>
|
||||
<ArrowRightCircle size={10} className="text-gray-600 flex-none opacity-50" />
|
||||
<input
|
||||
type="text"
|
||||
placeholder={rightPlaceholder}
|
||||
value={rule.replace}
|
||||
onChange={(e) => handleUpdate(rule.id, 'replace', e.target.value)}
|
||||
className={`flex-1 min-w-0 border rounded px-1.5 py-1 text-xs focus:outline-none transition-colors ${rightInputClass || defaultInputStyle}`}
|
||||
/>
|
||||
<button
|
||||
onClick={() => handleDelete(rule.id)}
|
||||
className="text-gray-600 hover:text-red-400 p-1 rounded hover:bg-red-500/10 transition-colors flex-none"
|
||||
>
|
||||
<Trash2 size={12} />
|
||||
</button>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
interface StrategySelectorProps {
|
||||
currentStrategy: SyncStrategy;
|
||||
onSelect: (strategy: SyncStrategy, label: string) => void;
|
||||
savedRegexReplacements: RegexReplacement[];
|
||||
onSaveRegex: (replacements: RegexReplacement[]) => void;
|
||||
savedPathMapping: PathMappingConfig;
|
||||
onSavePathMapping: (config: PathMappingConfig) => void;
|
||||
savedSchedule: ScheduleSettings;
|
||||
onSaveSchedule: (settings: ScheduleSettings) => Promise<boolean>;
|
||||
syncState: SyncState;
|
||||
@@ -95,8 +222,8 @@ interface StrategySelectorProps {
|
||||
const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
currentStrategy,
|
||||
onSelect,
|
||||
savedRegexReplacements,
|
||||
onSaveRegex,
|
||||
savedPathMapping,
|
||||
onSavePathMapping,
|
||||
savedSchedule,
|
||||
onSaveSchedule,
|
||||
syncState,
|
||||
@@ -105,17 +232,16 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// Local state for regex editing
|
||||
const [localReplacements, setLocalReplacements] = useState<RegexReplacement[]>([]);
|
||||
const [isRegexDirty, setIsRegexDirty] = useState(false);
|
||||
// Local state for path mapping editing (stores all lists for both modes)
|
||||
const [localPathMapping, setLocalPathMapping] = useState<PathMappingConfig>(savedPathMapping);
|
||||
const [isMappingDirty, setIsMappingDirty] = useState(false);
|
||||
|
||||
// Local state for Schedule editing
|
||||
const [localSchedule, setLocalSchedule] = useState<ScheduleSettings>(savedSchedule);
|
||||
const [isScheduleDirty, setIsScheduleDirty] = useState(false);
|
||||
|
||||
// UI State for Schedule Tabs
|
||||
// We initialize active tab based on the saved mode. If DISABLED, default to CRON.
|
||||
const [activeTab, setActiveTab] = useState<ScheduleMode>(
|
||||
const [activeScheduleTab, setActiveScheduleTab] = useState<ScheduleMode>(
|
||||
savedSchedule.mode === ScheduleMode.DISABLED ? ScheduleMode.CRON : savedSchedule.mode
|
||||
);
|
||||
|
||||
@@ -124,32 +250,30 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
|
||||
// Initialize local state when prop updates
|
||||
useEffect(() => {
|
||||
setLocalReplacements(JSON.parse(JSON.stringify(savedRegexReplacements)));
|
||||
setIsRegexDirty(false);
|
||||
}, [savedRegexReplacements]);
|
||||
setLocalPathMapping(JSON.parse(JSON.stringify(savedPathMapping)));
|
||||
setIsMappingDirty(false);
|
||||
}, [savedPathMapping]);
|
||||
|
||||
useEffect(() => {
|
||||
setLocalSchedule(JSON.parse(JSON.stringify(savedSchedule)));
|
||||
// If the saved mode is not disabled, ensure we show that tab.
|
||||
if (savedSchedule.mode !== ScheduleMode.DISABLED) {
|
||||
setActiveTab(savedSchedule.mode);
|
||||
setActiveScheduleTab(savedSchedule.mode);
|
||||
}
|
||||
setIsScheduleDirty(false);
|
||||
}, [savedSchedule]);
|
||||
|
||||
// Check dirty state whenever local changes
|
||||
// Check dirty state whenever local mapping changes
|
||||
useEffect(() => {
|
||||
const isDifferent = JSON.stringify(localReplacements) !== JSON.stringify(savedRegexReplacements);
|
||||
setIsRegexDirty(isDifferent);
|
||||
}, [localReplacements, savedRegexReplacements]);
|
||||
const isDifferent = JSON.stringify(localPathMapping) !== JSON.stringify(savedPathMapping);
|
||||
setIsMappingDirty(isDifferent);
|
||||
}, [localPathMapping, savedPathMapping]);
|
||||
|
||||
// Check dirty state for Schedule (including Active Tab changes)
|
||||
useEffect(() => {
|
||||
// We calculate what the "effective" schedule would be if we saved right now.
|
||||
const effectiveLocal = deriveEffectiveSchedule(localSchedule, activeTab);
|
||||
const effectiveLocal = deriveEffectiveSchedule(localSchedule, activeScheduleTab);
|
||||
const isDifferent = JSON.stringify(effectiveLocal) !== JSON.stringify(savedSchedule);
|
||||
setIsScheduleDirty(isDifferent);
|
||||
}, [localSchedule, savedSchedule, activeTab]);
|
||||
}, [localSchedule, savedSchedule, activeScheduleTab]);
|
||||
|
||||
const selectedOption = STRATEGIES.find(s => s.value === currentStrategy) || STRATEGIES[0];
|
||||
|
||||
@@ -163,47 +287,70 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||
}, []);
|
||||
|
||||
// Determine if tabs have changed from the saved state
|
||||
const initialTab = savedSchedule.mode === ScheduleMode.DISABLED ? ScheduleMode.CRON : savedSchedule.mode;
|
||||
const hasTabChanged = activeTab !== initialTab;
|
||||
const isScheduleActionable = isScheduleDirty || hasTabChanged;
|
||||
const isScheduleActionable = isScheduleDirty || (activeScheduleTab !== (savedSchedule.mode === ScheduleMode.DISABLED ? ScheduleMode.CRON : savedSchedule.mode));
|
||||
|
||||
const handleSelect = (strategy: StrategyOption) => {
|
||||
if (isLocked) return;
|
||||
onSelect(strategy.value, strategy.label);
|
||||
};
|
||||
|
||||
// --- Regex Handlers ---
|
||||
const handleAddRegex = () => {
|
||||
// --- Path Mapping Handlers ---
|
||||
const currentMappingMode = localPathMapping.mode;
|
||||
|
||||
const updateRegexGroup = (section: keyof PathMappingRules, newRules: ReplacementRule[]) => {
|
||||
if (isLocked) return;
|
||||
const newId = Date.now().toString();
|
||||
setLocalReplacements(prev => [...prev, { id: newId, pattern: '', replacement: '' }]);
|
||||
setLocalPathMapping(prev => ({
|
||||
...prev,
|
||||
regex: {
|
||||
...prev.regex,
|
||||
[section]: newRules
|
||||
}
|
||||
}));
|
||||
};
|
||||
|
||||
const handleDeleteRegex = (id: string) => {
|
||||
const updateSimpleGroup = (newRules: ReplacementRule[]) => {
|
||||
if (isLocked) return;
|
||||
setLocalReplacements(prev => prev.filter(r => r.id !== id));
|
||||
setLocalPathMapping(prev => ({
|
||||
...prev,
|
||||
simple: newRules
|
||||
}));
|
||||
};
|
||||
|
||||
const handleUpdateRegex = (id: string, field: 'pattern' | 'replacement', value: string) => {
|
||||
const setMappingMode = (mode: PathMappingMode) => {
|
||||
if (isLocked) return;
|
||||
setLocalReplacements(prev => prev.map(r =>
|
||||
r.id === id ? { ...r, [field]: value } : r
|
||||
));
|
||||
setLocalPathMapping(prev => ({ ...prev, mode }));
|
||||
};
|
||||
|
||||
const handleResetRegex = () => {
|
||||
const handleResetMapping = () => {
|
||||
if (isLocked) return;
|
||||
setLocalReplacements(JSON.parse(JSON.stringify(savedRegexReplacements)));
|
||||
setLocalPathMapping(JSON.parse(JSON.stringify(savedPathMapping)));
|
||||
};
|
||||
|
||||
const handleSaveRegex = () => {
|
||||
const handleSaveMappingClick = () => {
|
||||
if (isLocked) return;
|
||||
const validReplacements = localReplacements.filter(r => r.pattern.trim() !== '');
|
||||
setLocalReplacements(validReplacements);
|
||||
onSaveRegex(validReplacements);
|
||||
const clean = (rules: ReplacementRule[]) => rules.filter(r => r.search.trim() !== '');
|
||||
|
||||
// Clean regex rules
|
||||
const cleanRegex = (rules: PathMappingRules): PathMappingRules => ({
|
||||
localPre: clean(rules.localPre),
|
||||
localPost: clean(rules.localPost),
|
||||
remotePre: clean(rules.remotePre),
|
||||
remotePost: clean(rules.remotePost),
|
||||
});
|
||||
|
||||
const cleanedConfig: PathMappingConfig = {
|
||||
mode: localPathMapping.mode,
|
||||
simple: clean(localPathMapping.simple),
|
||||
regex: cleanRegex(localPathMapping.regex),
|
||||
};
|
||||
|
||||
setLocalPathMapping(cleanedConfig);
|
||||
onSavePathMapping(cleanedConfig);
|
||||
};
|
||||
|
||||
const regexRules = localPathMapping.regex;
|
||||
const simpleRules = localPathMapping.simple;
|
||||
|
||||
// --- Schedule Handlers ---
|
||||
const handleUpdateSchedule = (field: keyof ScheduleSettings, value: any) => {
|
||||
if (isLocked) return;
|
||||
@@ -223,24 +370,18 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
if (isLocked) return;
|
||||
setLocalSchedule(JSON.parse(JSON.stringify(savedSchedule)));
|
||||
if (savedSchedule.mode !== ScheduleMode.DISABLED) {
|
||||
setActiveTab(savedSchedule.mode);
|
||||
setActiveScheduleTab(savedSchedule.mode);
|
||||
} else {
|
||||
setActiveTab(ScheduleMode.CRON);
|
||||
setActiveScheduleTab(ScheduleMode.CRON);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSaveScheduleClick = async () => {
|
||||
if (isLocked) return;
|
||||
|
||||
// Determine the effective settings based on the current view (tab) and inputs
|
||||
const settingsToSave = deriveEffectiveSchedule(localSchedule, activeTab);
|
||||
|
||||
// Call API
|
||||
const settingsToSave = deriveEffectiveSchedule(localSchedule, activeScheduleTab);
|
||||
const success = await onSaveSchedule(settingsToSave);
|
||||
if (success) {
|
||||
setLocalSchedule(settingsToSave);
|
||||
// Dirty state is cleared by the useEffect prop update, or we can clear it optimistically here if needed,
|
||||
// but useEffect [savedSchedule] handles it correctly.
|
||||
}
|
||||
};
|
||||
|
||||
@@ -249,7 +390,6 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
onSync();
|
||||
};
|
||||
|
||||
// Helper to toggle enable/disable for current active tab (Daily/Weekly)
|
||||
const toggleScheduleEnable = (targetMode: ScheduleMode) => {
|
||||
if (isLocked) return;
|
||||
if (localSchedule.mode === targetMode) {
|
||||
@@ -259,7 +399,6 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
}
|
||||
};
|
||||
|
||||
// If syncing or locked, apply grayscale filter to content sections
|
||||
const contentClass = isLocked ? "opacity-50 pointer-events-none grayscale transition-all" : "transition-all";
|
||||
|
||||
return (
|
||||
@@ -280,12 +419,13 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
<div
|
||||
className={`absolute
|
||||
top-14
|
||||
/* Mobile: Open to left */
|
||||
right-0 origin-top-right
|
||||
/* Desktop: Center alignment */
|
||||
md:left-1/2 md:right-auto md:origin-top md:-translate-x-1/2
|
||||
/* Mobile: Open to left (max width of screen) */
|
||||
right-0 w-[90vw] max-w-[90vw] origin-top-right
|
||||
|
||||
w-80 md:w-[32rem] bg-gray-800/95 border border-white/10 rounded-xl shadow-2xl z-50 overflow-hidden backdrop-blur-xl
|
||||
/* Desktop: Center alignment, wider */
|
||||
md:left-1/2 md:right-auto md:origin-top md:-translate-x-1/2 md:w-[60rem] md:max-w-[60rem]
|
||||
|
||||
bg-gray-800/95 border border-white/10 rounded-xl shadow-2xl z-50 overflow-hidden backdrop-blur-xl
|
||||
transition-all duration-200 ease-out
|
||||
${isOpen ? 'opacity-100 scale-100 visible translate-y-0' : 'opacity-0 scale-95 invisible pointer-events-none -translate-y-2'}`}
|
||||
>
|
||||
@@ -329,78 +469,106 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Section 2: Regex Preprocessing */}
|
||||
{/* Section 2: Path Mapping (Tabs + Grid) */}
|
||||
<div className="p-4 bg-gray-900/40 border-b border-white/5 flex-none">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<h3 className="text-[10px] font-bold text-gray-500 uppercase tracking-widest">Regex Rules</h3>
|
||||
{localReplacements.length === 0 && (
|
||||
<button
|
||||
onClick={handleAddRegex}
|
||||
className="p-1 rounded bg-gray-700/50 hover:bg-gray-600 text-gray-400 hover:text-white transition-colors"
|
||||
title="Add Rule"
|
||||
>
|
||||
<Plus size={14} />
|
||||
</button>
|
||||
)}
|
||||
<h3 className="text-[10px] font-bold text-gray-500 uppercase tracking-widest">Path Mapping</h3>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2 mb-4 max-h-40 overflow-y-auto pr-1 custom-scrollbar">
|
||||
{localReplacements.length === 0 ? (
|
||||
<div className="text-xs text-gray-600 italic text-center py-2 border border-dashed border-gray-700/50 rounded-lg">
|
||||
No regex replacements configured.
|
||||
{/* Tabs for Path Mapping Mode */}
|
||||
<div className="flex space-x-1 bg-black/30 p-1 rounded-lg mb-4">
|
||||
{[
|
||||
{ id: PathMappingMode.SIMPLE, label: 'Simple Mapping', icon: Type },
|
||||
{ id: PathMappingMode.REGEX, label: 'Regex Rules', icon: Code2 },
|
||||
].map((tab) => (
|
||||
<button
|
||||
key={tab.id}
|
||||
onClick={() => setMappingMode(tab.id)}
|
||||
className={`flex-1 flex items-center justify-center space-x-2 py-1.5 rounded-md text-xs font-medium transition-all
|
||||
${currentMappingMode === tab.id
|
||||
? 'bg-gray-700 text-plex-orange shadow-sm'
|
||||
: 'text-gray-400 hover:text-gray-200 hover:bg-white/5'
|
||||
}`}
|
||||
>
|
||||
<tab.icon size={12} />
|
||||
<span>{tab.label}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Content Area */}
|
||||
<div className="mb-4">
|
||||
{currentMappingMode === PathMappingMode.SIMPLE ? (
|
||||
// Simple Mode: Single Editor
|
||||
<div className="animate-in fade-in duration-200">
|
||||
<MappingGroupEditor
|
||||
title="Path Mapping"
|
||||
subtitle="Map Local paths to Cloud paths using simple string matching"
|
||||
rules={simpleRules}
|
||||
onChange={updateSimpleGroup}
|
||||
isLocked={isLocked}
|
||||
borderColor={MAPPING_THEME.simple.borderColor}
|
||||
bgColor={MAPPING_THEME.simple.bgColor}
|
||||
leftPlaceholder="Local Path"
|
||||
rightPlaceholder="Cloud Path"
|
||||
leftInputClass={MAPPING_THEME.inputs.local}
|
||||
rightInputClass={MAPPING_THEME.inputs.cloud}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
localReplacements.map((regex) => (
|
||||
<div key={regex.id} className="flex items-center space-x-2 animate-in slide-in-from-left-2 duration-200">
|
||||
<div className="flex-1 min-w-0">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Pattern"
|
||||
value={regex.pattern}
|
||||
onChange={(e) => handleUpdateRegex(regex.id, 'pattern', e.target.value)}
|
||||
className={`w-full bg-gray-900/80 border rounded-md px-2 py-1.5 text-xs text-gray-200 focus:outline-none focus:ring-1 transition-all placeholder-gray-600
|
||||
${!regex.pattern && isRegexDirty ? 'border-red-500/30 focus:border-red-500' : 'border-gray-700 focus:border-plex-orange'}`}
|
||||
// Regex Mode: 2x2 Grid
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 animate-in fade-in duration-200">
|
||||
{/* Row 1: Pre-Processing */}
|
||||
<MappingGroupEditor
|
||||
title="Local Playlist"
|
||||
subtitle="Pre-Processing (Before Sync)"
|
||||
rules={regexRules.localPre}
|
||||
onChange={(rules) => updateRegexGroup('localPre', rules)}
|
||||
isLocked={isLocked}
|
||||
borderColor={MAPPING_THEME.local.borderColor}
|
||||
bgColor={MAPPING_THEME.local.bgColor}
|
||||
/>
|
||||
|
||||
<MappingGroupEditor
|
||||
title="Remote Playlist"
|
||||
subtitle="Pre-Processing (Before Sync)"
|
||||
rules={regexRules.remotePre}
|
||||
onChange={(rules) => updateRegexGroup('remotePre', rules)}
|
||||
isLocked={isLocked}
|
||||
borderColor={MAPPING_THEME.remote.borderColor}
|
||||
bgColor={MAPPING_THEME.remote.bgColor}
|
||||
/>
|
||||
|
||||
{/* Row 2: Post-Processing */}
|
||||
<MappingGroupEditor
|
||||
title="Local Playlist"
|
||||
subtitle="Post-Processing (After Sync / Result)"
|
||||
rules={regexRules.localPost}
|
||||
onChange={(rules) => updateRegexGroup('localPost', rules)}
|
||||
isLocked={isLocked}
|
||||
borderColor={MAPPING_THEME.local.borderColor}
|
||||
bgColor={MAPPING_THEME.local.bgColor}
|
||||
/>
|
||||
|
||||
<MappingGroupEditor
|
||||
title="Remote Playlist"
|
||||
subtitle="Post-Processing (After Sync / Result)"
|
||||
rules={regexRules.remotePost}
|
||||
onChange={(rules) => updateRegexGroup('remotePost', rules)}
|
||||
isLocked={isLocked}
|
||||
borderColor={MAPPING_THEME.remote.borderColor}
|
||||
bgColor={MAPPING_THEME.remote.bgColor}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex-none text-gray-600">
|
||||
<ArrowRightCircle size={12} />
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Replacement"
|
||||
value={regex.replacement}
|
||||
onChange={(e) => handleUpdateRegex(regex.id, 'replacement', e.target.value)}
|
||||
className="w-full bg-gray-900/80 border border-gray-700 rounded-md px-2 py-1.5 text-xs text-gray-200 focus:outline-none focus:border-plex-orange focus:ring-1 focus:ring-plex-orange transition-all placeholder-gray-600"
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => handleDeleteRegex(regex.id)}
|
||||
className="text-gray-600 hover:text-red-400 p-1.5 hover:bg-red-500/10 rounded transition-colors"
|
||||
title="Delete Rule"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex justify-between items-center gap-2">
|
||||
<div className="flex justify-end items-center gap-2">
|
||||
<button
|
||||
onClick={handleAddRegex}
|
||||
className={`flex items-center space-x-1 px-2 py-1 rounded text-[10px] font-bold uppercase tracking-wide transition-colors ${localReplacements.length > 0 ? 'text-plex-orange hover:bg-plex-orange/10' : 'hidden'}`}
|
||||
>
|
||||
<Plus size={10} />
|
||||
<span>Add</span>
|
||||
</button>
|
||||
|
||||
<div className="flex items-center gap-2 ml-auto">
|
||||
<button
|
||||
onClick={handleResetRegex}
|
||||
disabled={!isRegexDirty}
|
||||
onClick={handleResetMapping}
|
||||
disabled={!isMappingDirty}
|
||||
className={`flex items-center justify-center space-x-1.5 px-3 py-1.5 rounded-md text-xs font-medium border transition-all
|
||||
${isRegexDirty
|
||||
${isMappingDirty
|
||||
? 'bg-gray-800 border-gray-600 text-gray-300 hover:bg-gray-700 hover:text-white'
|
||||
: 'bg-transparent border-transparent text-gray-700 cursor-not-allowed'}`}
|
||||
>
|
||||
@@ -408,19 +576,18 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
<span>Revert</span>
|
||||
</button>
|
||||
<button
|
||||
onClick={handleSaveRegex}
|
||||
disabled={!isRegexDirty}
|
||||
onClick={handleSaveMappingClick}
|
||||
disabled={!isMappingDirty}
|
||||
className={`flex items-center justify-center space-x-1.5 px-3 py-1.5 rounded-md text-xs font-bold border transition-all
|
||||
${isRegexDirty
|
||||
${isMappingDirty
|
||||
? 'bg-plex-orange border-plex-orange text-gray-900 hover:bg-yellow-500 shadow-lg shadow-plex-orange/10'
|
||||
: 'bg-gray-800/30 border-gray-800/50 text-gray-600 cursor-not-allowed'}`}
|
||||
>
|
||||
<Save size={12} />
|
||||
<span>Save</span>
|
||||
<span>Save Rules</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Section 3: Scheduled Tasks */}
|
||||
<div className="p-4 bg-gray-900/40 border-b border-white/5 flex-none">
|
||||
@@ -437,9 +604,9 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
].map((tab) => (
|
||||
<button
|
||||
key={tab.id}
|
||||
onClick={() => setActiveTab(tab.id)}
|
||||
onClick={() => setActiveScheduleTab(tab.id)}
|
||||
className={`flex-1 flex items-center justify-center space-x-2 py-1.5 rounded-md text-xs font-medium transition-all
|
||||
${activeTab === tab.id
|
||||
${activeScheduleTab === tab.id
|
||||
? 'bg-gray-700 text-plex-orange shadow-sm'
|
||||
: 'text-gray-400 hover:text-gray-200 hover:bg-white/5'
|
||||
}`}
|
||||
@@ -452,7 +619,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
|
||||
{/* Tab Content */}
|
||||
<div className="mb-4 min-h-[50px]">
|
||||
{activeTab === ScheduleMode.CRON && (
|
||||
{activeScheduleTab === ScheduleMode.CRON && (
|
||||
<div className="space-y-2 animate-in fade-in duration-200">
|
||||
<div className="flex items-center space-x-2">
|
||||
<span className="text-gray-500 font-mono text-xs">Cron:</span>
|
||||
@@ -470,7 +637,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{activeTab === ScheduleMode.DAILY && (
|
||||
{activeScheduleTab === ScheduleMode.DAILY && (
|
||||
<div className="flex flex-col animate-in fade-in duration-200">
|
||||
{/* Top Row: Checkbox + Label */}
|
||||
<div className="flex items-center justify-start space-x-2 mb-2">
|
||||
@@ -497,7 +664,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
</div>
|
||||
)}
|
||||
|
||||
{activeTab === ScheduleMode.WEEKLY && (
|
||||
{activeScheduleTab === ScheduleMode.WEEKLY && (
|
||||
<div className="flex flex-col animate-in fade-in duration-200">
|
||||
{/* Top Row: Checkbox + Label */}
|
||||
<div className="flex items-center justify-start space-x-2 mb-2">
|
||||
@@ -564,7 +731,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Action Buttons (Mirrored from Regex) */}
|
||||
{/* Action Buttons */}
|
||||
<div className="flex items-center gap-2 justify-end pt-3 border-t border-white/5">
|
||||
<button
|
||||
onClick={handleResetSchedule}
|
||||
@@ -600,7 +767,7 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
className={`w-full py-3 rounded-lg text-sm font-bold flex items-center justify-center gap-2 transition-all shadow-lg
|
||||
${isLocked
|
||||
? 'bg-gray-700/30 text-gray-500 cursor-not-allowed border border-gray-700/50'
|
||||
: isRegexDirty
|
||||
: isMappingDirty
|
||||
? 'bg-gray-800 text-gray-500 cursor-not-allowed border border-gray-700'
|
||||
: 'bg-green-600 hover:bg-green-500 text-white border border-green-500 shadow-green-900/20 active:scale-[0.98]'
|
||||
}`}
|
||||
@@ -617,9 +784,9 @@ const StrategySelector: React.FC<StrategySelectorProps> = ({
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
{(isRegexDirty) && (
|
||||
{(isMappingDirty) && (
|
||||
<p className="text-[10px] text-plex-orange text-center mt-2">
|
||||
Please save regex changes before syncing.
|
||||
Please save path mapping changes before syncing.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
|
||||
|
||||
import { Playlist, ServerType, ApiResponse, PlexServerConnection, PlexConnectionSettings, PlexLibrary, SyncStrategy, RegexReplacement, ScheduleSettings, ScheduleMode } from '../types';
|
||||
|
||||
|
||||
import { Playlist, ServerType, ApiResponse, PlexServerConnection, PlexConnectionSettings, PlexLibrary, SyncStrategy, PathMappingConfig, ScheduleSettings, ScheduleMode } from '../types';
|
||||
import { MOCK_LOCAL_PLAYLISTS, MOCK_CLOUD_PLAYLISTS } from './mockData';
|
||||
|
||||
const SIMULATE_DELAY_MS = 800;
|
||||
@@ -127,9 +129,10 @@ const authenticatePlex = async (settings: PlexConnectionSettings, signal?: Abort
|
||||
});
|
||||
}
|
||||
|
||||
const triggerSync = async (strategy: SyncStrategy, regexRules: RegexReplacement[]): Promise<void> => {
|
||||
const triggerSync = async (strategy: SyncStrategy, pathMapping: PathMappingConfig): Promise<void> => {
|
||||
return new Promise((resolve) => {
|
||||
// Simulate a sync process taking 3 seconds
|
||||
// In a real app, pathMapping would be sent to backend
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, 3000);
|
||||
@@ -193,9 +196,9 @@ export const apiService = {
|
||||
}
|
||||
},
|
||||
|
||||
syncPlaylists: async (strategy: SyncStrategy, regexRules: RegexReplacement[]): Promise<ApiResponse<null>> => {
|
||||
syncPlaylists: async (strategy: SyncStrategy, pathMapping: PathMappingConfig): Promise<ApiResponse<null>> => {
|
||||
try {
|
||||
await triggerSync(strategy, regexRules);
|
||||
await triggerSync(strategy, pathMapping);
|
||||
return { data: null, status: 'success', message: 'Sync complete' };
|
||||
} catch (error) {
|
||||
return { data: null, status: 'error', message: 'Sync failed' };
|
||||
|
||||
@@ -35,10 +35,28 @@ export enum SyncState {
|
||||
ERROR = 'ERROR'
|
||||
}
|
||||
|
||||
export interface RegexReplacement {
|
||||
export interface ReplacementRule {
|
||||
id: string;
|
||||
pattern: string;
|
||||
replacement: string;
|
||||
search: string;
|
||||
replace: string;
|
||||
}
|
||||
|
||||
export interface PathMappingRules {
|
||||
localPre: ReplacementRule[];
|
||||
localPost: ReplacementRule[];
|
||||
remotePre: ReplacementRule[];
|
||||
remotePost: ReplacementRule[];
|
||||
}
|
||||
|
||||
export enum PathMappingMode {
|
||||
SIMPLE = 'SIMPLE',
|
||||
REGEX = 'REGEX'
|
||||
}
|
||||
|
||||
export interface PathMappingConfig {
|
||||
mode: PathMappingMode;
|
||||
simple: ReplacementRule[];
|
||||
regex: PathMappingRules;
|
||||
}
|
||||
|
||||
export enum ScheduleMode {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
# Case 1 - Base playlist
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
# Case 2 - Base playlist
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
N:\Music\Anime\リテラチュア\01. リテラチュア - リテラチュア.flac
|
||||
N:\Music\Anime\ダンジョン飯\Party!! (期間生産限定盤)\01. Party!! - Party!! (期間生産限定盤).flac
|
||||
@@ -0,0 +1,5 @@
|
||||
#EXTM3U
|
||||
# Case 3 - Base playlist
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 4 - Base playlist
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
@@ -0,0 +1,4 @@
|
||||
#EXTM3U
|
||||
N:\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\02. Reckless - Theme of New PANTY & STOCKING.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\音ゲー\USAO\MEMORIES\15. Empty Room (Srav3R Remix) - MEMORIES.flac
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 1 - Local playlist
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\ピンポン - ED - 僕らについて\01. “あのヒーローと” 僕らについて - ピンポン - ED - 僕らについて.mp3
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 2 - Local playlist
|
||||
# A comment that should be ignored
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
N:\Music\Anime\ダンジョン飯\Party!! (期間生産限定盤)\01. Party!! - Party!! (期間生産限定盤).flac
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
# Case 3 - Local playlist
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 4 - Local playlist
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
@@ -0,0 +1,5 @@
|
||||
#EXTM3U
|
||||
/mnt/music/Anime/CITY THE ANIMATION/Hello/01. Hello - Hello.flac
|
||||
\\koha9-nas\koha9-nas\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\02. Reckless - Theme of New PANTY & STOCKING.flac
|
||||
N:\MUSIC\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\音ゲー\USAO\MEMORIES\11. Echoes - MEMORIES.flac
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
# Case 1 - Remote playlist
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\ピンポン - ED - 僕らについて\02. “あの夜明け前の” 僕らについて - ピンポン - ED - 僕らについて.mp3
|
||||
@@ -0,0 +1,5 @@
|
||||
#EXTM3U
|
||||
# Case 2 - Remote playlist
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
N:\Music\Anime\リテラチュア\01. リテラチュア - リテラチュア.flac
|
||||
N:\Music\Anime\ダンジョン飯\Party!! (期間生産限定盤)\01. Party!! - Party!! (期間生産限定盤).flac
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
# Case 3 - Remote playlist
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 4 - Remote playlist
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
N:\Music\音ゲー\USAO\MEMORIES\15. Empty Room (Srav3R Remix) - MEMORIES.flac
|
||||
N:\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\02. Reckless - Theme of New PANTY & STOCKING.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\04. Theme of New PANTY & STOCKING (Long Version) - Theme of New PANTY & STOCKING.flac
|
||||
@@ -0,0 +1,8 @@
|
||||
#EXTM3U
|
||||
# Case 1 - Expected merged result (merge_local_primary)
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\ピンポン - ED - 僕らについて\01. “あのヒーローと” 僕らについて - ピンポン - ED - 僕らについて.mp3
|
||||
N:\Music\Anime\ピンポン - ED - 僕らについて\02. “あの夜明け前の” 僕らについて - ピンポン - ED - 僕らについて.mp3
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 1 - Expected merged result (merge_remote_primary)
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\ピンポン - ED - 僕らについて\02. “あの夜明け前の” 僕らについて - ピンポン - ED - 僕らについて.mp3
|
||||
N:\Music\Anime\ピンポン - ED - 僕らについて\01. “あのヒーローと” 僕らについて - ピンポン - ED - 僕らについて.mp3
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
# Case 2 - Expected merged result (merge_local_primary)
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
N:\Music\Anime\ダンジョン飯\Party!! (期間生産限定盤)\01. Party!! - Party!! (期間生産限定盤).flac
|
||||
@@ -0,0 +1,5 @@
|
||||
#EXTM3U
|
||||
# Case 2 - Expected merged result (merge_remote_primary)
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
N:\Music\Anime\リテラチュア\01. リテラチュア - リテラチュア.flac
|
||||
N:\Music\Anime\ダンジョン飯\Party!! (期間生産限定盤)\01. Party!! - Party!! (期間生産限定盤).flac
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
# Case 3 - Expected merged result (merge_local_primary)
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
# Case 3 - Expected merged result (merge_remote_primary)
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 4 - Expected merged result (merge_local_primary)
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
# Case 4 - Expected merged result (merge_remote_primary)
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\Anime\キルラキル\新世界交響楽\01. 新世界交響楽 - 新世界交響楽.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\Seize The Day\01. Seize The Day - Seize The Day.flac
|
||||
N:\Music\Anime\ゆるキャン△ SEASON2\はるのとなり\01. はるのとなり - はるのとなり.flac
|
||||
@@ -0,0 +1,5 @@
|
||||
#EXTM3U
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\02. Reckless - Theme of New PANTY & STOCKING.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\音ゲー\USAO\MEMORIES\11. Echoes - MEMORIES.flac
|
||||
@@ -0,0 +1,7 @@
|
||||
#EXTM3U
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\02. Reckless - Theme of New PANTY & STOCKING.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\音ゲー\USAO\MEMORIES\11. Echoes - MEMORIES.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\04. Theme of New PANTY & STOCKING (Long Version) - Theme of New PANTY & STOCKING.flac
|
||||
@@ -0,0 +1,8 @@
|
||||
#EXTM3U
|
||||
N:\Music\音ゲー\USAO\MEMORIES\15. Empty Room (Srav3R Remix) - MEMORIES.flac
|
||||
N:\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\02. Reckless - Theme of New PANTY & STOCKING.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\04. Theme of New PANTY & STOCKING (Long Version) - Theme of New PANTY & STOCKING.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\02. ちゃんと - Hello.flac
|
||||
N:\Music\音ゲー\USAO\MEMORIES\11. Echoes - MEMORIES.flac
|
||||
@@ -0,0 +1,6 @@
|
||||
#EXTM3U
|
||||
N:\Music\音ゲー\USAO\MEMORIES\15. Empty Room (Srav3R Remix) - MEMORIES.flac
|
||||
N:\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\02. Reckless - Theme of New PANTY & STOCKING.flac
|
||||
N:\Music\Anime\CITY THE ANIMATION\Hello\01. Hello - Hello.flac
|
||||
N:\Music\Anime\少女終末旅行\動く、動く\01. 動く、動く - 動く、動く.flac
|
||||
N:\Music\Anime\New PANTY & STOCKING with GARTERBELT\Theme of New PANTY & STOCKING\04. Theme of New PANTY & STOCKING (Long Version) - Theme of New PANTY & STOCKING.flac
|
||||
@@ -0,0 +1,128 @@
|
||||
# 🎯 正则路径替换测试 - 快速参考
|
||||
|
||||
## ✅ 测试状态
|
||||
```
|
||||
✅ 59/59 测试通过
|
||||
⚡ 执行时间: 0.56s
|
||||
📦 包含: 13 个合并测试 + 46 个正则测试
|
||||
```
|
||||
|
||||
## 🚀 快速运行
|
||||
|
||||
```bash
|
||||
# 运行所有测试
|
||||
pytest tests/
|
||||
|
||||
# 只运行正则测试
|
||||
pytest tests/test_regex_path_replacement.py -v
|
||||
|
||||
# 运行特定测试类
|
||||
pytest tests/test_regex_path_replacement.py::TestApplyRegexRulesToPaths -v
|
||||
|
||||
# 查看覆盖率
|
||||
pytest tests/test_regex_path_replacement.py --cov=app.utils.playlist_merge
|
||||
```
|
||||
|
||||
## 📋 测试分类
|
||||
|
||||
| 测试类 | 数量 | 说明 |
|
||||
|--------|------|------|
|
||||
| TestCompileRegexRules | 7 | 正则编译和验证 |
|
||||
| TestApplyCompiledRulesToPaths | 5 | 应用已编译规则 |
|
||||
| TestApplyRegexRulesToPaths | 17 | 完整替换流程 |
|
||||
| TestPreprocessPlaylistText | 7 | 播放列表预处理 |
|
||||
| TestEdgeCases | 9 | 边界情况处理 |
|
||||
| TestPerformance | 3 | 性能测试 |
|
||||
|
||||
## 💡 常用示例
|
||||
|
||||
### 简单替换
|
||||
```python
|
||||
rules = [{"pattern": r"/old/", "replacement": "/new/"}]
|
||||
"/old/music/track.mp3" → "/new/music/track.mp3"
|
||||
```
|
||||
|
||||
### Windows 路径
|
||||
```python
|
||||
rules = [{"pattern": r"C:\\Music", "replacement": r"D:\\Audio"}]
|
||||
r"C:\Music\track.mp3" → r"D:\Audio\track.mp3"
|
||||
```
|
||||
|
||||
### 捕获组
|
||||
```python
|
||||
rules = [{"pattern": r"/(\d+)/", "replacement": r"/year-\1/"}]
|
||||
"/music/2024/track.mp3" → "/music/year-2024/track.mp3"
|
||||
```
|
||||
|
||||
### NAS 路径转换(真实场景)
|
||||
```python
|
||||
rules = [
|
||||
{"pattern": r"\\\\nas\\Music", "replacement": r"N:\\Music"},
|
||||
{"pattern": r"\\", "replacement": "/"},
|
||||
]
|
||||
r"\\nas\Music\Album\track.mp3" → "N:/Music/Album/track.mp3"
|
||||
```
|
||||
|
||||
## 🎨 测试覆盖的场景
|
||||
|
||||
### ✅ 路径类型
|
||||
- Linux 路径 `/path/to/file`
|
||||
- Windows 路径 `C:\path\to\file`
|
||||
- UNC 路径 `\\server\share\file`
|
||||
- 相对路径 `../path/./file`
|
||||
- URL 编码 `/artist%20name/track.mp3`
|
||||
- Unicode `/音乐/歌曲.mp3`
|
||||
|
||||
### ✅ 正则特性
|
||||
- 简单匹配 `foo`
|
||||
- 特殊字符 `\(\d+\)`
|
||||
- 捕获组 `(pattern)` → `\1`
|
||||
- 不区分大小写 `(?i)pattern`
|
||||
- 字符类 `[A-Z]+` `\d+` `\w+`
|
||||
|
||||
### ✅ 边界情况
|
||||
- 空输入(规则/路径)
|
||||
- 无效正则表达式
|
||||
- 超长路径 (1000+ 字符)
|
||||
- 特殊字符 `[]()&#`
|
||||
- 链式替换
|
||||
|
||||
### ✅ 性能测试
|
||||
- 10,000 首歌曲的播放列表
|
||||
- 5+ 条规则链式执行
|
||||
- 复杂正则模式匹配
|
||||
|
||||
## 📖 相关文档
|
||||
|
||||
- 详细总结: `tests/REGEX_TESTS_SUMMARY.md`
|
||||
- 测试文件: `tests/test_regex_path_replacement.py`
|
||||
- 被测代码: `app/utils/playlist_merge.py`
|
||||
|
||||
## 🔍 调试技巧
|
||||
|
||||
```bash
|
||||
# 显示详细输出
|
||||
pytest tests/test_regex_path_replacement.py -v -s
|
||||
|
||||
# 遇到第一个失败就停止
|
||||
pytest tests/test_regex_path_replacement.py -x
|
||||
|
||||
# 进入调试器
|
||||
pytest tests/test_regex_path_replacement.py --pdb
|
||||
|
||||
# 只运行失败的测试
|
||||
pytest tests/test_regex_path_replacement.py --lf
|
||||
```
|
||||
|
||||
## 🎓 测试即文档
|
||||
|
||||
每个测试都是一个使用示例,查看测试代码了解如何使用正则替换功能!
|
||||
|
||||
```python
|
||||
# 示例: 查看如何使用捕获组
|
||||
def test_capture_group_replacement():
|
||||
paths = ["/music/2024/album/track.mp3"]
|
||||
rules = [{"pattern": r"/music/(\d+)/", "replacement": r"/archive/\1/"}]
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
assert result == ["/archive/2024/album/track.mp3"]
|
||||
```
|
||||
@@ -0,0 +1,283 @@
|
||||
# 正则路径替换功能测试总结
|
||||
|
||||
## 📊 测试统计
|
||||
|
||||
- **总测试数**: 46 个
|
||||
- **测试状态**: ✅ 全部通过
|
||||
- **执行时间**: ~0.4 秒
|
||||
- **覆盖范围**: 正则编译、路径替换、预处理、边界情况、性能测试
|
||||
|
||||
## 🎯 测试文件
|
||||
|
||||
`tests/test_regex_path_replacement.py` - 正则路径替换功能的全面测试套件
|
||||
|
||||
## 📝 测试分类
|
||||
|
||||
### 1. TestCompileRegexRules (7 个测试)
|
||||
测试正则规则编译功能
|
||||
|
||||
- ✅ `test_compile_simple_pattern` - 简单正则模式编译
|
||||
- ✅ `test_compile_multiple_patterns` - 多个正则模式编译
|
||||
- ✅ `test_compile_empty_pattern_skipped` - 跳过空模式
|
||||
- ✅ `test_compile_missing_pattern_skipped` - 跳过缺失模式
|
||||
- ✅ `test_compile_invalid_regex_skipped` - 跳过无效正则表达式
|
||||
- ✅ `test_compile_empty_replacement` - 空替换字符串
|
||||
- ✅ `test_compile_missing_replacement` - 缺失替换字符串(默认为空)
|
||||
|
||||
**关键测试点**:
|
||||
- 编译过程容错性(跳过无效规则)
|
||||
- 边界情况处理(空/缺失值)
|
||||
|
||||
### 2. TestApplyCompiledRulesToPaths (5 个测试)
|
||||
测试应用已编译的正则规则到路径
|
||||
|
||||
- ✅ `test_apply_single_rule` - 应用单个规则
|
||||
- ✅ `test_apply_multiple_rules_in_order` - 按顺序应用多个规则
|
||||
- ✅ `test_apply_no_rules` - 没有规则时返回原路径
|
||||
- ✅ `test_apply_no_match` - 规则不匹配时保持原路径
|
||||
- ✅ `test_apply_partial_match` - 部分路径匹配
|
||||
|
||||
**关键测试点**:
|
||||
- 规则顺序执行
|
||||
- 链式替换(第一个规则的输出作为第二个规则的输入)
|
||||
- 部分匹配处理
|
||||
|
||||
### 3. TestApplyRegexRulesToPaths (17 个测试)
|
||||
测试完整的路径正则替换流程
|
||||
|
||||
#### 基础替换
|
||||
- ✅ `test_simple_replacement` - 简单字符串替换
|
||||
- ✅ `test_windows_path_replacement` - Windows 路径替换
|
||||
- ✅ `test_unc_path_replacement` - UNC 网络路径替换
|
||||
|
||||
#### 高级正则功能
|
||||
- ✅ `test_case_sensitive_replacement` - 大小写敏感替换
|
||||
- ✅ `test_case_insensitive_replacement` - 大小写不敏感替换(`(?i)` 标志)
|
||||
- ✅ `test_regex_special_characters` - 正则特殊字符处理
|
||||
- ✅ `test_capture_group_replacement` - 捕获组替换 `\1`
|
||||
- ✅ `test_multiple_capture_groups` - 多个捕获组交换位置
|
||||
|
||||
#### 实用场景
|
||||
- ✅ `test_delete_pattern` - 删除匹配内容(替换为空)
|
||||
- ✅ `test_multiple_matches_in_path` - 路径中多次匹配
|
||||
- ✅ `test_chained_replacements` - 链式替换(NAS 路径转换)
|
||||
- ✅ `test_url_encoding_path` - URL 编码路径处理
|
||||
- ✅ `test_unicode_path` - Unicode 路径支持
|
||||
|
||||
#### 边界情况
|
||||
- ✅ `test_empty_rules_list` - 空规则列表
|
||||
- ✅ `test_empty_paths_list` - 空路径列表
|
||||
|
||||
**关键测试点**:
|
||||
- 各种路径格式(Windows、Linux、UNC、URL 编码)
|
||||
- 正则高级特性(捕获组、标志)
|
||||
- 国际化支持(Unicode)
|
||||
|
||||
### 4. TestPreprocessPlaylistText (7 个测试)
|
||||
测试预处理播放列表文本(含正则替换)
|
||||
|
||||
- ✅ `test_preprocess_with_replacements` - 带替换的预处理
|
||||
- ✅ `test_preprocess_removes_comments` - 移除注释
|
||||
- ✅ `test_preprocess_empty_text` - 空文本处理
|
||||
- ✅ `test_preprocess_with_blank_lines` - 处理空行
|
||||
- ✅ `test_preprocess_real_world_scenario` - **真实场景:NAS 路径转换**
|
||||
- ✅ `test_preprocess_with_compiled_rules` - 使用预编译规则
|
||||
- ✅ `test_preprocess_preserves_order` - 保持顺序
|
||||
|
||||
**关键测试点**:
|
||||
- 完整的播放列表处理流程
|
||||
- 注释和空行过滤
|
||||
- 真实使用场景验证
|
||||
|
||||
**真实场景示例**:
|
||||
```python
|
||||
# 输入
|
||||
\\koha9-nas\koha9-nas\Music\Rock\track.flac
|
||||
/music/cache/temp.flac
|
||||
|
||||
# 规则
|
||||
1. \\koha9-nas\koha9-nas\Music → N:\Music
|
||||
2. /music/cache/ → /data/music/
|
||||
3. \ → /
|
||||
|
||||
# 输出
|
||||
N:/Music/Rock/track.flac
|
||||
/data/music/temp.flac
|
||||
```
|
||||
|
||||
### 5. TestEdgeCases (9 个测试)
|
||||
测试边界情况和异常场景
|
||||
|
||||
- ✅ `test_very_long_path` - 超长路径(1000+ 字符)
|
||||
- ✅ `test_special_characters_in_path` - 特殊字符 `[]()&#`
|
||||
- ✅ `test_dot_in_path` - 相对路径符号 `../` `./`
|
||||
- ✅ `test_trailing_slash` - 尾部斜杠处理
|
||||
- ✅ `test_duplicate_slashes` - 重复斜杠 `//` `///`
|
||||
- ✅ `test_mixed_path_separators` - 混合路径分隔符 `\` `/`
|
||||
- ✅ `test_regex_metacharacters_in_replacement` - 替换字符串中的元字符
|
||||
- ✅ `test_empty_string_replacement` - 替换为空字符串
|
||||
- ✅ `test_replacement_creates_invalid_path` - 可能产生无效路径
|
||||
|
||||
**关键测试点**:
|
||||
- 极端输入处理
|
||||
- 路径规范化场景
|
||||
- 错误容忍性
|
||||
|
||||
### 6. TestPerformance (3 个测试)
|
||||
测试性能相关场景
|
||||
|
||||
- ✅ `test_large_playlist` - 大型播放列表(10,000 首歌曲)
|
||||
- ✅ `test_many_rules` - 大量规则(5+ 个规则链式执行)
|
||||
- ✅ `test_complex_regex_pattern` - 复杂正则表达式
|
||||
|
||||
**性能示例**:
|
||||
```python
|
||||
# 复杂正则模式
|
||||
Pattern: /music/(.+?) - (.+?) \((\d+)\) \[([^\]]+)\]/
|
||||
Input: /music/Artist - Album (2024) [FLAC]/01. Track.flac
|
||||
Output: /library/FLAC/2024/Artist/Album/01. Track.flac
|
||||
```
|
||||
|
||||
**关键测试点**:
|
||||
- 大数据量处理能力
|
||||
- 复杂模式匹配性能
|
||||
- 规则链执行效率
|
||||
|
||||
## 🔍 覆盖的功能点
|
||||
|
||||
### 核心功能
|
||||
- ✅ 正则规则编译和验证
|
||||
- ✅ 规则按顺序应用到路径
|
||||
- ✅ 播放列表文本预处理
|
||||
- ✅ 捕获组和反向引用
|
||||
- ✅ 大小写敏感/不敏感匹配
|
||||
|
||||
### 路径类型
|
||||
- ✅ Linux/Unix 绝对路径 `/path/to/file`
|
||||
- ✅ Windows 绝对路径 `C:\path\to\file`
|
||||
- ✅ UNC 网络路径 `\\server\share\file`
|
||||
- ✅ 相对路径 `../path/./file`
|
||||
- ✅ URL 编码路径 `/artist%20name/track.mp3`
|
||||
- ✅ Unicode 路径 `/音乐/专辑/歌曲.mp3`
|
||||
|
||||
### 正则特性
|
||||
- ✅ 简单字符串匹配
|
||||
- ✅ 特殊字符转义 `()[].*+?`
|
||||
- ✅ 捕获组 `(pattern)` 和引用 `\1`
|
||||
- ✅ 不区分大小写 `(?i)`
|
||||
- ✅ 量词 `*+?{n}`
|
||||
- ✅ 字符类 `[^/]+` `\d+` `\w+`
|
||||
|
||||
### 边界情况
|
||||
- ✅ 空输入(规则/路径)
|
||||
- ✅ 无效正则表达式
|
||||
- ✅ 不匹配的规则
|
||||
- ✅ 超长路径
|
||||
- ✅ 特殊字符
|
||||
- ✅ 链式替换
|
||||
|
||||
### 容错性
|
||||
- ✅ 跳过空模式
|
||||
- ✅ 跳过无效正则
|
||||
- ✅ 默认替换为空字符串
|
||||
- ✅ 保留不匹配的路径
|
||||
|
||||
## 🎓 测试用例示例
|
||||
|
||||
### 基础替换
|
||||
```python
|
||||
paths = ["/old/path/file.mp3"]
|
||||
rules = [{"pattern": r"/old/", "replacement": "/new/"}]
|
||||
# 结果: ["/new/path/file.mp3"]
|
||||
```
|
||||
|
||||
### 捕获组替换
|
||||
```python
|
||||
paths = ["/music/2024/album/track.mp3"]
|
||||
rules = [{"pattern": r"/music/(\d+)/", "replacement": r"/archive/\1/"}]
|
||||
# 结果: ["/archive/2024/album/track.mp3"]
|
||||
```
|
||||
|
||||
### 链式替换(真实场景)
|
||||
```python
|
||||
paths = [r"\\nas\Music\Album\track.mp3"]
|
||||
rules = [
|
||||
{"pattern": r"\\\\nas\\Music", "replacement": "/mnt/music"},
|
||||
{"pattern": r"\\", "replacement": "/"},
|
||||
]
|
||||
# 结果: ["/mnt/music/Album/track.mp3"]
|
||||
```
|
||||
|
||||
### 复杂模式匹配
|
||||
```python
|
||||
paths = ["/music/Artist - Album (2024) [FLAC]/01. Track.flac"]
|
||||
rules = [
|
||||
{
|
||||
"pattern": r"/music/(.+?) - (.+?) \((\d+)\) \[([^\]]+)\]/",
|
||||
"replacement": r"/library/\4/\3/\1/\2/"
|
||||
}
|
||||
]
|
||||
# 结果: ["/library/FLAC/2024/Artist/Album/01. Track.flac"]
|
||||
```
|
||||
|
||||
## 🚀 运行测试
|
||||
|
||||
### 运行正则替换测试
|
||||
```bash
|
||||
pytest tests/test_regex_path_replacement.py -v
|
||||
```
|
||||
|
||||
### 运行特定测试类
|
||||
```bash
|
||||
pytest tests/test_regex_path_replacement.py::TestApplyRegexRulesToPaths -v
|
||||
```
|
||||
|
||||
### 运行特定测试
|
||||
```bash
|
||||
pytest tests/test_regex_path_replacement.py::TestPreprocessPlaylistText::test_preprocess_real_world_scenario -v
|
||||
```
|
||||
|
||||
### 查看测试覆盖率
|
||||
```bash
|
||||
pytest tests/test_regex_path_replacement.py --cov=app.utils.playlist_merge --cov-report=term
|
||||
```
|
||||
|
||||
## 💡 测试最佳实践
|
||||
|
||||
本测试套件遵循的最佳实践:
|
||||
|
||||
1. **分类清晰** - 按功能层级组织测试类
|
||||
2. **命名规范** - 测试名称清楚描述测试内容
|
||||
3. **独立性** - 每个测试独立运行,无依赖
|
||||
4. **覆盖全面** - 正常流程、边界情况、错误处理全覆盖
|
||||
5. **文档化** - 每个测试都有描述性文档字符串
|
||||
6. **真实场景** - 包含实际使用场景的测试用例
|
||||
7. **性能考虑** - 包含大数据量和复杂模式的性能测试
|
||||
|
||||
## 📈 测试价值
|
||||
|
||||
这套测试为正则路径替换功能提供了:
|
||||
|
||||
- **信心保证** - 46 个测试覆盖各种场景
|
||||
- **回归防护** - 修改代码时快速验证功能完整性
|
||||
- **文档作用** - 测试即使用示例和功能文档
|
||||
- **重构支持** - 安全重构代码而不破坏功能
|
||||
- **Bug 预防** - 边界情况测试防止潜在 Bug
|
||||
|
||||
## 🔧 维护建议
|
||||
|
||||
1. **添加新功能时**同步添加测试
|
||||
2. **发现 Bug 时**先写失败测试,再修复
|
||||
3. **定期运行**完整测试套件
|
||||
4. **保持测试更新**与代码变更同步
|
||||
5. **关注覆盖率**保持 80% 以上
|
||||
|
||||
## 相关文件
|
||||
|
||||
- 测试文件: `tests/test_regex_path_replacement.py`
|
||||
- 被测代码: `app/utils/playlist_merge.py`
|
||||
- 相关函数:
|
||||
- `_compile_regex_rules()`
|
||||
- `_apply_compiled_rules_to_paths()`
|
||||
- `apply_regex_rules_to_paths()`
|
||||
- `preprocess_playlist_text()`
|
||||
@@ -0,0 +1,172 @@
|
||||
# 🎭 UI 集成测试快速开始
|
||||
|
||||
## 📦 安装
|
||||
|
||||
```bash
|
||||
# 1. 安装 Python 依赖
|
||||
pip install -r requirements.txt
|
||||
|
||||
# 2. 安装 Playwright 浏览器
|
||||
playwright install chromium
|
||||
|
||||
# 或安装所有浏览器
|
||||
playwright install
|
||||
```
|
||||
|
||||
## 🚀 运行测试
|
||||
|
||||
### 启动服务器
|
||||
```bash
|
||||
# 终端 1: 启动应用
|
||||
uvicorn app.main:app --reload --port 8000
|
||||
```
|
||||
|
||||
### 运行 UI 测试
|
||||
```bash
|
||||
# 终端 2: 运行测试
|
||||
|
||||
# 无头模式(后台运行,快速)
|
||||
pytest tests/test_ui_regex_rules.py -v
|
||||
|
||||
# 有头模式(显示浏览器,便于调试)
|
||||
pytest tests/test_ui_regex_rules.py -v --headed
|
||||
|
||||
# 慢速模式(每个操作间隔 500ms,方便观察)
|
||||
pytest tests/test_ui_regex_rules.py -v --headed --slowmo=500
|
||||
```
|
||||
|
||||
## 📊 测试内容
|
||||
|
||||
### ✅ 基础交互 (8 个测试)
|
||||
- 页面加载
|
||||
- 添加/删除规则
|
||||
- 保存规则
|
||||
- 规则持久化
|
||||
- 表单验证
|
||||
- 规则顺序
|
||||
|
||||
### ✅ 复杂场景 (2 个测试)
|
||||
- Windows → Linux 路径转换
|
||||
- NAS 路径规范化
|
||||
|
||||
### ✅ 性能测试 (1 个测试)
|
||||
- 添加 20 个规则性能
|
||||
|
||||
## 🎯 快速示例
|
||||
|
||||
```bash
|
||||
# 运行单个测试(有头模式,便于观察)
|
||||
pytest tests/test_ui_regex_rules.py::TestRegexRulesUI::test_add_single_rule -v --headed
|
||||
|
||||
# 运行 NAS 场景测试
|
||||
pytest tests/test_ui_regex_rules.py::TestComplexScenarios::test_nas_path_normalization -v --headed
|
||||
|
||||
# 调试模式(带 Playwright Inspector)
|
||||
PWDEBUG=1 pytest tests/test_ui_regex_rules.py::TestRegexRulesUI::test_add_single_rule
|
||||
```
|
||||
|
||||
## 🐛 调试技巧
|
||||
|
||||
### 1. 截图调试
|
||||
测试失败时会自动保存截图到 `tests/screenshots/`
|
||||
|
||||
### 2. 慢速观察
|
||||
```bash
|
||||
pytest tests/test_ui_regex_rules.py --headed --slowmo=1000 -v
|
||||
```
|
||||
|
||||
### 3. 交互式调试
|
||||
```bash
|
||||
PWDEBUG=1 pytest tests/test_ui_regex_rules.py -k test_add_single_rule
|
||||
```
|
||||
|
||||
### 4. 查看追踪
|
||||
```python
|
||||
# 在测试中添加
|
||||
context.tracing.start(screenshots=True, snapshots=True)
|
||||
# ... 测试代码 ...
|
||||
context.tracing.stop(path="trace.zip")
|
||||
```
|
||||
|
||||
然后查看:
|
||||
```bash
|
||||
playwright show-trace trace.zip
|
||||
```
|
||||
|
||||
## 📝 编写新测试
|
||||
|
||||
```python
|
||||
def test_my_feature(page: Page):
|
||||
"""测试我的功能"""
|
||||
# 1. 与元素交互
|
||||
page.locator("#myButton").click()
|
||||
|
||||
# 2. 填写表单
|
||||
page.locator("input[name='pattern']").fill("test")
|
||||
|
||||
# 3. 验证结果
|
||||
expect(page.locator("#result")).to_have_text("Success")
|
||||
```
|
||||
|
||||
## 🎨 选择器参考
|
||||
|
||||
```python
|
||||
# 通过 ID
|
||||
page.locator("#addRuleBtn")
|
||||
|
||||
# 通过文本
|
||||
page.locator("button:has-text('保存规则')")
|
||||
|
||||
# 通过 CSS 类
|
||||
page.locator(".rule-row")
|
||||
|
||||
# 通过属性
|
||||
page.locator("input[name='pattern']")
|
||||
|
||||
# 组合选择器
|
||||
page.locator(".rule-row input[name='pattern']")
|
||||
|
||||
# 获取第一个/最后一个
|
||||
page.locator(".rule-row").first
|
||||
page.locator(".rule-row").last
|
||||
|
||||
# 获取第 N 个
|
||||
page.locator(".rule-row").nth(2)
|
||||
```
|
||||
|
||||
## ⚡ 常用命令
|
||||
|
||||
```bash
|
||||
# 运行所有 UI 测试
|
||||
pytest tests/test_ui_regex_rules.py -v
|
||||
|
||||
# 运行特定测试类
|
||||
pytest tests/test_ui_regex_rules.py::TestRegexRulesUI -v
|
||||
|
||||
# 运行标记为 slow 的测试
|
||||
pytest tests/test_ui_regex_rules.py -m slow -v
|
||||
|
||||
# 跳过 slow 测试
|
||||
pytest tests/test_ui_regex_rules.py -m "not slow" -v
|
||||
|
||||
# 失败时进入调试器
|
||||
pytest tests/test_ui_regex_rules.py --pdb
|
||||
|
||||
# 只运行失败的测试
|
||||
pytest tests/test_ui_regex_rules.py --lf -v
|
||||
```
|
||||
|
||||
## 📚 相关文档
|
||||
|
||||
- 详细指南: `tests/UI_TESTING_GUIDE.md`
|
||||
- Playwright 文档: https://playwright.dev/python/
|
||||
- pytest-playwright: https://github.com/microsoft/playwright-pytest
|
||||
|
||||
## 🎉 开始测试
|
||||
|
||||
```bash
|
||||
# 一行命令开始
|
||||
uvicorn app.main:app --port 8000 & \
|
||||
sleep 3 && \
|
||||
pytest tests/test_ui_regex_rules.py -v --headed
|
||||
```
|
||||
@@ -0,0 +1,356 @@
|
||||
# UI 集成测试指南
|
||||
|
||||
## 📋 概述
|
||||
|
||||
UI 集成测试使用 **Playwright** 框架来测试正则路径替换功能的用户界面交互。
|
||||
|
||||
## 🚀 快速开始
|
||||
|
||||
### 1. 安装依赖
|
||||
|
||||
```bash
|
||||
# 安装 Playwright 和浏览器驱动
|
||||
pip install pytest-playwright
|
||||
playwright install chromium
|
||||
|
||||
# 或安装所有浏览器
|
||||
playwright install
|
||||
```
|
||||
|
||||
### 2. 启动应用服务器
|
||||
|
||||
在运行 UI 测试前,需要先启动应用:
|
||||
|
||||
```bash
|
||||
# 方式 1: 直接运行
|
||||
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
|
||||
|
||||
# 方式 2: 使用 Docker
|
||||
docker compose up
|
||||
```
|
||||
|
||||
### 3. 运行 UI 测试
|
||||
|
||||
```bash
|
||||
# 无头模式(不显示浏览器)
|
||||
pytest tests/test_ui_regex_rules.py -v
|
||||
|
||||
# 有头模式(显示浏览器,便于调试)
|
||||
pytest tests/test_ui_regex_rules.py -v --headed
|
||||
|
||||
# 慢速模式(方便观察)
|
||||
pytest tests/test_ui_regex_rules.py -v --headed --slowmo=500
|
||||
|
||||
# 运行特定测试
|
||||
pytest tests/test_ui_regex_rules.py::TestRegexRulesUI::test_add_single_rule -v --headed
|
||||
```
|
||||
|
||||
## 📊 测试覆盖
|
||||
|
||||
### 基础 UI 交互测试 (TestRegexRulesUI)
|
||||
|
||||
| 测试 | 描述 |
|
||||
|------|------|
|
||||
| `test_page_loads_successfully` | 页面成功加载 |
|
||||
| `test_add_single_rule` | 添加单个规则 |
|
||||
| `test_add_multiple_rules` | 添加多个规则 |
|
||||
| `test_remove_rule` | 删除规则 |
|
||||
| `test_save_rules` | 保存规则 |
|
||||
| `test_rules_persist_after_save` | 规则持久化验证 |
|
||||
| `test_empty_pattern_validation` | 空模式验证 |
|
||||
| `test_rule_order_preserved` | 规则顺序保持 |
|
||||
|
||||
### 复杂场景测试 (TestComplexScenarios)
|
||||
|
||||
| 测试 | 描述 |
|
||||
|------|------|
|
||||
| `test_windows_to_linux_path_conversion` | Windows → Linux 路径转换 |
|
||||
| `test_nas_path_normalization` | NAS 路径规范化 |
|
||||
|
||||
### 性能测试 (TestPerformance)
|
||||
|
||||
| 测试 | 描述 |
|
||||
|------|------|
|
||||
| `test_add_many_rules_performance` | 添加大量规则性能 |
|
||||
|
||||
## 🎯 测试场景示例
|
||||
|
||||
### 场景 1: 添加单个规则
|
||||
|
||||
```python
|
||||
# 1. 点击"添加规则"按钮
|
||||
# 2. 填写正则表达式: /old/path/
|
||||
# 3. 填写替换文本: /new/path/
|
||||
# 4. 验证输入框内容正确
|
||||
```
|
||||
|
||||
### 场景 2: NAS 路径规范化
|
||||
|
||||
```python
|
||||
# 添加三条规则:
|
||||
# 1. \\koha9-nas\koha9-nas\Music → N:\Music
|
||||
# 2. /music/cache/ → /data/music/
|
||||
# 3. \ → /
|
||||
#
|
||||
# 保存并验证规则持久化
|
||||
```
|
||||
|
||||
### 场景 3: 规则持久化验证
|
||||
|
||||
```python
|
||||
# 1. 添加规则
|
||||
# 2. 保存
|
||||
# 3. 刷新页面
|
||||
# 4. 验证规则仍然存在
|
||||
```
|
||||
|
||||
## 🔧 配置选项
|
||||
|
||||
### pytest.ini 配置
|
||||
|
||||
```ini
|
||||
[pytest]
|
||||
# Playwright 配置
|
||||
addopts =
|
||||
--browser=chromium
|
||||
--headed
|
||||
--slowmo=100
|
||||
```
|
||||
|
||||
### 环境变量
|
||||
|
||||
```bash
|
||||
# 设置测试服务器地址
|
||||
export TEST_SERVER_URL="http://localhost:8000"
|
||||
|
||||
# 设置浏览器类型
|
||||
export BROWSER=chromium # 或 firefox, webkit
|
||||
```
|
||||
|
||||
## 🐛 调试技巧
|
||||
|
||||
### 1. 使用有头模式
|
||||
|
||||
```bash
|
||||
pytest tests/test_ui_regex_rules.py --headed
|
||||
```
|
||||
|
||||
### 2. 使用慢速模式
|
||||
|
||||
```bash
|
||||
pytest tests/test_ui_regex_rules.py --headed --slowmo=1000
|
||||
```
|
||||
|
||||
### 3. 截图调试
|
||||
|
||||
在测试中添加截图:
|
||||
|
||||
```python
|
||||
def test_something(page: Page):
|
||||
page.screenshot(path="debug_screenshot.png")
|
||||
```
|
||||
|
||||
### 4. 使用 Playwright Inspector
|
||||
|
||||
```bash
|
||||
# 启动调试模式
|
||||
PWDEBUG=1 pytest tests/test_ui_regex_rules.py::test_add_single_rule
|
||||
```
|
||||
|
||||
### 5. 查看追踪
|
||||
|
||||
```python
|
||||
# 在 conftest.py 中添加
|
||||
@pytest.fixture
|
||||
def context(browser):
|
||||
context = browser.new_context()
|
||||
context.tracing.start(screenshots=True, snapshots=True)
|
||||
yield context
|
||||
context.tracing.stop(path="trace.zip")
|
||||
```
|
||||
|
||||
然后查看:
|
||||
```bash
|
||||
playwright show-trace trace.zip
|
||||
```
|
||||
|
||||
## 📝 编写新的 UI 测试
|
||||
|
||||
### 基本模板
|
||||
|
||||
```python
|
||||
def test_my_feature(page: Page):
|
||||
"""测试我的功能"""
|
||||
# 1. 导航到页面
|
||||
page.goto("http://localhost:8000")
|
||||
|
||||
# 2. 与元素交互
|
||||
button = page.locator("#myButton")
|
||||
button.click()
|
||||
|
||||
# 3. 验证结果
|
||||
expect(page.locator("#result")).to_have_text("Success")
|
||||
```
|
||||
|
||||
### 等待策略
|
||||
|
||||
```python
|
||||
# 等待元素可见
|
||||
page.wait_for_selector("#element", state="visible")
|
||||
|
||||
# 等待网络空闲
|
||||
page.wait_for_load_state("networkidle")
|
||||
|
||||
# 等待特定时间(尽量避免)
|
||||
page.wait_for_timeout(1000) # 1 秒
|
||||
```
|
||||
|
||||
### 选择器策略
|
||||
|
||||
```python
|
||||
# 推荐: 使用 data-testid
|
||||
page.locator("[data-testid='add-rule-btn']")
|
||||
|
||||
# 通过文本
|
||||
page.locator("button:has-text('保存规则')")
|
||||
|
||||
# 通过 ID
|
||||
page.locator("#addRuleBtn")
|
||||
|
||||
# 通过 CSS 类
|
||||
page.locator(".rule-row")
|
||||
|
||||
# 组合选择器
|
||||
page.locator(".rule-row input[name='pattern']")
|
||||
```
|
||||
|
||||
## 🎨 最佳实践
|
||||
|
||||
### 1. 使用 Page Object 模式
|
||||
|
||||
```python
|
||||
class RulesPage:
|
||||
def __init__(self, page: Page):
|
||||
self.page = page
|
||||
self.add_button = page.locator("#addRuleBtn")
|
||||
self.save_button = page.locator("button:has-text('保存规则')")
|
||||
|
||||
def add_rule(self, pattern: str, replacement: str):
|
||||
self.add_button.click()
|
||||
self.page.wait_for_timeout(100)
|
||||
|
||||
patterns = self.page.locator("input[name='pattern']")
|
||||
replacements = self.page.locator("input[name='replacement']")
|
||||
|
||||
patterns.last.fill(pattern)
|
||||
replacements.last.fill(replacement)
|
||||
|
||||
def save(self):
|
||||
self.save_button.click()
|
||||
self.page.wait_for_load_state("networkidle")
|
||||
|
||||
# 使用
|
||||
def test_with_page_object(page: Page):
|
||||
rules_page = RulesPage(page)
|
||||
rules_page.add_rule(r"/old/", r"/new/")
|
||||
rules_page.save()
|
||||
```
|
||||
|
||||
### 2. 使用 Fixtures 清理状态
|
||||
|
||||
```python
|
||||
@pytest.fixture
|
||||
def clean_rules(page: Page):
|
||||
"""清除所有规则"""
|
||||
page.goto("http://localhost:8000")
|
||||
while page.locator(".rule-row").count() > 0:
|
||||
page.locator(".rule-row button[title='删除此规则']").first.click()
|
||||
page.wait_for_timeout(50)
|
||||
yield
|
||||
```
|
||||
|
||||
### 3. 避免硬编码等待时间
|
||||
|
||||
```python
|
||||
# ❌ 不好
|
||||
page.wait_for_timeout(2000)
|
||||
|
||||
# ✅ 好
|
||||
page.wait_for_selector("#element", state="visible")
|
||||
page.wait_for_load_state("networkidle")
|
||||
```
|
||||
|
||||
### 4. 使用断言而非 if 判断
|
||||
|
||||
```python
|
||||
# ❌ 不好
|
||||
assert page.locator("#element").count() > 0
|
||||
|
||||
# ✅ 好
|
||||
expect(page.locator("#element")).to_be_visible()
|
||||
```
|
||||
|
||||
## 🔄 CI/CD 集成
|
||||
|
||||
### GitHub Actions 示例
|
||||
|
||||
```yaml
|
||||
name: UI Tests
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
ui-tests:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: '3.10'
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
pip install -r requirements.txt
|
||||
pip install pytest-playwright
|
||||
playwright install --with-deps chromium
|
||||
|
||||
- name: Start application
|
||||
run: |
|
||||
uvicorn app.main:app --host 0.0.0.0 --port 8000 &
|
||||
sleep 5
|
||||
|
||||
- name: Run UI tests
|
||||
run: pytest tests/test_ui_regex_rules.py -v
|
||||
|
||||
- name: Upload screenshots on failure
|
||||
if: failure()
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: screenshots
|
||||
path: screenshots/
|
||||
```
|
||||
|
||||
## 📚 参考资料
|
||||
|
||||
- [Playwright 官方文档](https://playwright.dev/python/)
|
||||
- [pytest-playwright 插件](https://github.com/microsoft/playwright-pytest)
|
||||
- [Playwright 最佳实践](https://playwright.dev/python/docs/best-practices)
|
||||
|
||||
## 🆘 常见问题
|
||||
|
||||
### Q: 测试运行时找不到浏览器?
|
||||
A: 运行 `playwright install chromium`
|
||||
|
||||
### Q: 测试失败,如何调试?
|
||||
A: 使用 `--headed --slowmo=500` 参数可视化执行过程
|
||||
|
||||
### Q: 如何在测试中等待异步操作?
|
||||
A: 使用 `page.wait_for_load_state("networkidle")` 或 `page.wait_for_selector()`
|
||||
|
||||
### Q: 如何处理动态加载的内容?
|
||||
A: 使用 `expect().to_be_visible()` 会自动等待元素出现
|
||||
|
||||
### Q: 测试很慢怎么办?
|
||||
A: 减少不必要的 `wait_for_timeout()`,使用事件驱动的等待方法
|
||||
@@ -0,0 +1,175 @@
|
||||
# UI测试迁移指南
|
||||
|
||||
## 概述
|
||||
|
||||
本文档说明了UI测试从旧版本迁移到新React前端的主要变更。
|
||||
|
||||
## 主要变更
|
||||
|
||||
### 1. 输出目录变更
|
||||
|
||||
**旧版本:**
|
||||
```
|
||||
dockerapp/test_playlists/{playlist_name}/
|
||||
```
|
||||
|
||||
**新版本:**
|
||||
```
|
||||
output_playlists/{playlist_name}/
|
||||
```
|
||||
|
||||
### 2. 服务器端口变更
|
||||
|
||||
**旧版本:**
|
||||
- 测试服务器: `http://localhost:8000`
|
||||
|
||||
**新版本:**
|
||||
- Docker映射端口: `http://localhost:8888`
|
||||
- 容器内端口: `8080`
|
||||
|
||||
### 3. UI架构变更
|
||||
|
||||
**旧版本:**
|
||||
- 传统的HTML模板 (Jinja2)
|
||||
- 选择器: `#addRuleBtn`, `select[name='mode']`, `input[name='pattern']`
|
||||
|
||||
**新版本:**
|
||||
- React + TypeScript + Vite
|
||||
- 策略选择器: 中间位置的圆形按钮下拉菜单
|
||||
- 正则规则在StrategySelector组件中管理
|
||||
- 选择器: `button[title='Add Rule']`, `input[placeholder='Regex Pattern']`
|
||||
|
||||
### 4. 测试文件修改
|
||||
|
||||
#### `conftest_ui.py`
|
||||
- 更新BASE_URL为`http://localhost:8888`
|
||||
- 修改server fixture为仅验证服务器运行状态
|
||||
- 要求手动启动Docker Compose服务
|
||||
|
||||
#### `test_ui_case_mix.py`
|
||||
- 添加`SyncStrategy`枚举类
|
||||
- 实现`_open_strategy_selector()`和`_close_strategy_selector()`辅助函数
|
||||
- 更新策略选择逻辑以适配React UI
|
||||
- 更新正则规则添加逻辑
|
||||
- 修改输出路径为`output_playlists/case_mix/`
|
||||
|
||||
#### `test_ui_regex_rules.py`
|
||||
- 完全重写所有测试用例以适配React UI
|
||||
- 添加辅助方法`_open_strategy_selector()`和`_close_strategy_selector()`
|
||||
- 更新所有选择器以匹配新UI结构
|
||||
- 适配Toast通知验证
|
||||
|
||||
## 运行测试
|
||||
|
||||
### 前提条件
|
||||
|
||||
1. **启动Docker Compose服务:**
|
||||
```powershell
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
2. **验证服务运行:**
|
||||
```powershell
|
||||
# 在浏览器中访问
|
||||
# http://localhost:8888
|
||||
```
|
||||
|
||||
3. **安装测试依赖:**
|
||||
```powershell
|
||||
pip install pytest-playwright requests
|
||||
playwright install
|
||||
```
|
||||
|
||||
### 运行测试
|
||||
|
||||
**显示浏览器模式 (调试用):**
|
||||
```powershell
|
||||
pytest tests/test_ui_case_mix.py --headed
|
||||
pytest tests/test_ui_regex_rules.py --headed
|
||||
```
|
||||
|
||||
**无头模式 (CI/CD):**
|
||||
```powershell
|
||||
pytest tests/test_ui_case_mix.py
|
||||
pytest tests/test_ui_regex_rules.py
|
||||
```
|
||||
|
||||
**运行所有UI测试:**
|
||||
```powershell
|
||||
pytest tests/test_ui_*.py --headed
|
||||
```
|
||||
|
||||
## 新UI元素定位
|
||||
|
||||
### 策略选择器
|
||||
|
||||
- **触发按钮:** `button` with SVG icon (圆形按钮)
|
||||
- **下拉菜单:** `div.absolute.top-14`
|
||||
- **策略选项:** `div:has-text('{strategy_label}')` with SVG
|
||||
|
||||
### 正则规则
|
||||
|
||||
- **添加按钮:** `button[title='Add Rule']` 或 `button:has-text('Add Rule')`
|
||||
- **删除按钮:** `button[title='Delete Rule']`
|
||||
- **模式输入:** `input[placeholder='Regex Pattern']`
|
||||
- **替换输入:** `input[placeholder='Replacement']`
|
||||
- **保存按钮:** `button:has-text('Save Changes')`
|
||||
- **重置按钮:** `button:has-text('Revert')`
|
||||
|
||||
### Toast通知
|
||||
|
||||
- **成功通知:** `div:has-text('Regex preprocessing rules have been saved')`
|
||||
- **策略保存:** `div:has-text('Selected strategy "{label}" has been saved')`
|
||||
|
||||
## 策略映射
|
||||
|
||||
| UI显示名称 | SyncStrategy枚举 | 旧版mode值 | 预期输出文件 |
|
||||
|-----------|-----------------|-----------|-------------|
|
||||
| Local Overwrite | LOCAL_OVERWRITE | local_force | case_mix_local_force.m3u |
|
||||
| Cloud Overwrite | CLOUD_OVERWRITE | remote_force | case_mix_remote_force.m3u |
|
||||
| Two-way Merge (Local Priority) | MERGE_LOCAL | merge_local_primary | case_mix_merge_local_primary.m3u |
|
||||
| Two-way Merge (Cloud Priority) | MERGE_CLOUD | merge_remote_primary | case_mix_merge_remote_primary.m3u |
|
||||
|
||||
## 已知问题和注意事项
|
||||
|
||||
1. **同步触发:** 新UI需要通过API显式触发同步操作。测试中使用 `POST /api/sync` 端点:
|
||||
```python
|
||||
import requests
|
||||
sync_response = requests.post(
|
||||
f"{BASE_URL}/api/sync",
|
||||
json={"mode": None} # 使用当前配置的策略
|
||||
)
|
||||
```
|
||||
|
||||
2. **Toast通知:** Toast通知有动画效果,需要适当的等待时间 (300-500ms)。
|
||||
|
||||
3. **下拉菜单:** 策略选择器的下拉菜单需要通过ESC键关闭,或点击外部区域。
|
||||
|
||||
4. **测试隔离:** 每个测试应该清理自己添加的规则,避免影响后续测试。
|
||||
|
||||
## 故障排除
|
||||
|
||||
### 服务器未运行
|
||||
```
|
||||
错误: 无法连接到测试服务器: http://localhost:8888
|
||||
解决: docker compose up -d
|
||||
```
|
||||
|
||||
### 元素未找到
|
||||
```
|
||||
错误: Timeout waiting for locator('button[title="Add Rule"]')
|
||||
解决: 检查策略选择器是否已打开,确保调用了_open_strategy_selector()
|
||||
```
|
||||
|
||||
### 输出文件未生成
|
||||
```
|
||||
错误: AssertionError: {strategy_label}: local_result.m3u8 未生成
|
||||
解决:
|
||||
1. 检查output_playlists/case_mix/目录是否存在
|
||||
2. 验证Docker volume映射配置
|
||||
3. 检查后端日志: docker compose logs
|
||||
```
|
||||
|
||||
## 更新日期
|
||||
|
||||
2024-11-29 - 初始迁移完成
|
||||
@@ -0,0 +1,131 @@
|
||||
"""
|
||||
Pytest fixtures for UI testing
|
||||
|
||||
注意: 此测试套件假设服务已通过 Docker Compose 启动
|
||||
运行前请确保: docker compose up -d
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from playwright.sync_api import Browser, Page
|
||||
|
||||
|
||||
# 测试服务器配置 - Docker映射端口8888到容器内8080
|
||||
TEST_SERVER_HOST = os.getenv("TEST_SERVER_HOST", "localhost")
|
||||
TEST_SERVER_PORT = int(os.getenv("TEST_SERVER_PORT", "8888"))
|
||||
BASE_URL = f"http://{TEST_SERVER_HOST}:{TEST_SERVER_PORT}"
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def test_server():
|
||||
"""
|
||||
验证测试服务器是否运行
|
||||
|
||||
此fixture不启动服务器,而是检查Docker Compose服务是否已启动。
|
||||
请在运行测试前手动启动: docker compose up -d
|
||||
"""
|
||||
# 检查服务器是否已经在运行
|
||||
max_retries = 10
|
||||
for i in range(max_retries):
|
||||
try:
|
||||
import requests
|
||||
response = requests.get(BASE_URL, timeout=3)
|
||||
if response.status_code < 500:
|
||||
print(f"✓ 服务器已在运行: {BASE_URL}")
|
||||
yield BASE_URL
|
||||
return
|
||||
except Exception as e:
|
||||
if i == max_retries - 1:
|
||||
raise RuntimeError(
|
||||
f"无法连接到测试服务器: {BASE_URL}\n"
|
||||
f"请确保已启动 Docker Compose 服务: docker compose up -d\n"
|
||||
f"错误: {e}"
|
||||
)
|
||||
time.sleep(2)
|
||||
|
||||
yield BASE_URL
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def browser_context_args(browser_context_args):
|
||||
"""配置浏览器上下文"""
|
||||
return {
|
||||
**browser_context_args,
|
||||
"viewport": {"width": 1920, "height": 1080},
|
||||
"locale": "zh-CN",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def page(page: Page, test_server):
|
||||
"""
|
||||
配置页面并导航到首页
|
||||
|
||||
自动导航到测试服务器的首页,并等待页面加载完成。
|
||||
"""
|
||||
page.goto(test_server)
|
||||
page.wait_for_load_state("networkidle")
|
||||
|
||||
# 设置默认超时
|
||||
page.set_default_timeout(10000) # 10 秒
|
||||
|
||||
yield page
|
||||
|
||||
# 测试失败时截图
|
||||
if page.context.browser.is_connected():
|
||||
try:
|
||||
screenshots_dir = Path(__file__).parent / "screenshots"
|
||||
screenshots_dir.mkdir(exist_ok=True)
|
||||
|
||||
test_name = os.environ.get("PYTEST_CURRENT_TEST", "unknown").split(":")[-1].split(" ")[0]
|
||||
screenshot_path = screenshots_dir / f"{test_name}.png"
|
||||
|
||||
page.screenshot(path=str(screenshot_path))
|
||||
print(f"截图保存至: {screenshot_path}")
|
||||
except Exception as e:
|
||||
print(f"截图失败: {e}")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def clean_rules(page: Page):
|
||||
"""
|
||||
清除所有规则的 fixture
|
||||
|
||||
在测试前清除所有现有的正则规则,确保测试从干净状态开始。
|
||||
"""
|
||||
# 清除所有规则
|
||||
while page.locator(".rule-row").count() > 0:
|
||||
try:
|
||||
remove_btn = page.locator(".rule-row button[title='删除此规则']").first
|
||||
remove_btn.click()
|
||||
page.wait_for_timeout(50)
|
||||
except:
|
||||
break # 如果没有更多规则可删除
|
||||
|
||||
yield
|
||||
|
||||
# 测试后不清理,让下一个测试自己清理
|
||||
# 这样可以在浏览器中查看测试结果
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_rules():
|
||||
"""提供示例规则数据"""
|
||||
return [
|
||||
{"pattern": r"/old/path/", "replacement": r"/new/path/"},
|
||||
{"pattern": r"C:\\Music", "replacement": r"D:\\Audio"},
|
||||
{"pattern": r"\\\\nas\\share", "replacement": r"Z:"},
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def nas_conversion_rules():
|
||||
"""提供 NAS 路径转换规则"""
|
||||
return [
|
||||
{"pattern": r"\\\\koha9-nas\\koha9-nas\\Music", "replacement": r"N:\\Music"},
|
||||
{"pattern": r"/music/cache/", "replacement": r"/data/music/"},
|
||||
{"pattern": r"\\", "replacement": r"/"},
|
||||
]
|
||||
@@ -0,0 +1,554 @@
|
||||
"""
|
||||
Unit tests for regex path replacement functionality.
|
||||
测试正则替换路径功能的各种场景。
|
||||
"""
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app.utils.playlist_merge import (
|
||||
_compile_regex_rules,
|
||||
_apply_compiled_rules_to_paths,
|
||||
apply_regex_rules_to_paths,
|
||||
preprocess_playlist_text,
|
||||
)
|
||||
|
||||
|
||||
class TestCompileRegexRules:
|
||||
"""测试正则规则编译功能"""
|
||||
|
||||
def test_compile_simple_pattern(self):
|
||||
"""测试编译简单正则模式"""
|
||||
rules = [{"pattern": r"foo", "replacement": "bar"}]
|
||||
compiled = _compile_regex_rules(rules)
|
||||
|
||||
assert len(compiled) == 1
|
||||
assert isinstance(compiled[0][0], re.Pattern)
|
||||
assert compiled[0][1] == "bar"
|
||||
|
||||
def test_compile_multiple_patterns(self):
|
||||
"""测试编译多个正则模式"""
|
||||
rules = [
|
||||
{"pattern": r"foo", "replacement": "bar"},
|
||||
{"pattern": r"\d+", "replacement": "NUM"},
|
||||
{"pattern": r"[A-Z]+", "replacement": "UPPER"},
|
||||
]
|
||||
compiled = _compile_regex_rules(rules)
|
||||
|
||||
assert len(compiled) == 3
|
||||
|
||||
def test_compile_empty_pattern_skipped(self):
|
||||
"""测试跳过空模式"""
|
||||
rules = [
|
||||
{"pattern": "", "replacement": "bar"},
|
||||
{"pattern": r"foo", "replacement": "bar"},
|
||||
]
|
||||
compiled = _compile_regex_rules(rules)
|
||||
|
||||
assert len(compiled) == 1
|
||||
|
||||
def test_compile_missing_pattern_skipped(self):
|
||||
"""测试跳过缺失模式"""
|
||||
rules = [
|
||||
{"replacement": "bar"}, # no pattern
|
||||
{"pattern": r"foo", "replacement": "bar"},
|
||||
]
|
||||
compiled = _compile_regex_rules(rules)
|
||||
|
||||
assert len(compiled) == 1
|
||||
|
||||
def test_compile_invalid_regex_skipped(self):
|
||||
"""测试跳过无效正则表达式"""
|
||||
rules = [
|
||||
{"pattern": r"[invalid(", "replacement": "bar"}, # invalid regex
|
||||
{"pattern": r"foo", "replacement": "bar"},
|
||||
]
|
||||
compiled = _compile_regex_rules(rules)
|
||||
|
||||
# Invalid pattern should be skipped
|
||||
assert len(compiled) == 1
|
||||
|
||||
def test_compile_empty_replacement(self):
|
||||
"""测试空替换字符串"""
|
||||
rules = [{"pattern": r"foo", "replacement": ""}]
|
||||
compiled = _compile_regex_rules(rules)
|
||||
|
||||
assert len(compiled) == 1
|
||||
assert compiled[0][1] == ""
|
||||
|
||||
def test_compile_missing_replacement(self):
|
||||
"""测试缺失替换字符串(默认为空)"""
|
||||
rules = [{"pattern": r"foo"}]
|
||||
compiled = _compile_regex_rules(rules)
|
||||
|
||||
assert len(compiled) == 1
|
||||
assert compiled[0][1] == ""
|
||||
|
||||
|
||||
class TestApplyCompiledRulesToPaths:
|
||||
"""测试应用已编译的正则规则到路径"""
|
||||
|
||||
def test_apply_single_rule(self):
|
||||
"""测试应用单个规则"""
|
||||
paths = ["/music/album/track1.mp3", "/music/album/track2.mp3"]
|
||||
compiled = [(re.compile(r"/music/"), "/data/")]
|
||||
|
||||
result = _apply_compiled_rules_to_paths(paths, compiled)
|
||||
|
||||
assert result == ["/data/album/track1.mp3", "/data/album/track2.mp3"]
|
||||
|
||||
def test_apply_multiple_rules_in_order(self):
|
||||
"""测试按顺序应用多个规则"""
|
||||
paths = ["/temp/music/file.mp3"]
|
||||
compiled = [
|
||||
(re.compile(r"/temp/"), "/data/"),
|
||||
(re.compile(r"/data/"), "/storage/"),
|
||||
]
|
||||
|
||||
result = _apply_compiled_rules_to_paths(paths, compiled)
|
||||
|
||||
# Should apply both rules in sequence
|
||||
assert result == ["/storage/music/file.mp3"]
|
||||
|
||||
def test_apply_no_rules(self):
|
||||
"""测试没有规则时返回原路径"""
|
||||
paths = ["/music/track.mp3"]
|
||||
compiled = []
|
||||
|
||||
result = _apply_compiled_rules_to_paths(paths, compiled)
|
||||
|
||||
assert result == paths
|
||||
|
||||
def test_apply_no_match(self):
|
||||
"""测试规则不匹配时保持原路径"""
|
||||
paths = ["/music/track.mp3"]
|
||||
compiled = [(re.compile(r"/video/"), "/data/")]
|
||||
|
||||
result = _apply_compiled_rules_to_paths(paths, compiled)
|
||||
|
||||
assert result == paths
|
||||
|
||||
def test_apply_partial_match(self):
|
||||
"""测试部分路径匹配"""
|
||||
paths = [
|
||||
"/music/rock/song.mp3",
|
||||
"/video/movie.mp4",
|
||||
"/music/jazz/tune.mp3",
|
||||
]
|
||||
compiled = [(re.compile(r"/music/"), "/audio/")]
|
||||
|
||||
result = _apply_compiled_rules_to_paths(paths, compiled)
|
||||
|
||||
assert result == [
|
||||
"/audio/rock/song.mp3",
|
||||
"/video/movie.mp4",
|
||||
"/audio/jazz/tune.mp3",
|
||||
]
|
||||
|
||||
|
||||
class TestApplyRegexRulesToPaths:
|
||||
"""测试完整的路径正则替换流程(含编译)"""
|
||||
|
||||
def test_simple_replacement(self):
|
||||
"""测试简单字符串替换"""
|
||||
paths = ["/old/path/file.mp3"]
|
||||
rules = [{"pattern": r"/old/", "replacement": "/new/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == ["/new/path/file.mp3"]
|
||||
|
||||
def test_windows_path_replacement(self):
|
||||
"""测试 Windows 路径替换"""
|
||||
paths = [r"C:\Music\Album\track.mp3"]
|
||||
rules = [{"pattern": r"C:\\Music", "replacement": r"D:\\Audio"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == [r"D:\Audio\Album\track.mp3"]
|
||||
|
||||
def test_unc_path_replacement(self):
|
||||
"""测试 UNC 网络路径替换"""
|
||||
paths = [r"\\server\share\music\track.mp3"]
|
||||
rules = [
|
||||
{"pattern": r"\\\\server\\share", "replacement": r"Z:"}
|
||||
]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == [r"Z:\music\track.mp3"]
|
||||
|
||||
def test_case_sensitive_replacement(self):
|
||||
"""测试大小写敏感替换"""
|
||||
paths = ["/Music/Track.mp3", "/music/track.mp3"]
|
||||
rules = [{"pattern": r"/Music/", "replacement": "/Audio/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
# Only exact case match should be replaced
|
||||
assert result == ["/Audio/Track.mp3", "/music/track.mp3"]
|
||||
|
||||
def test_case_insensitive_replacement(self):
|
||||
"""测试大小写不敏感替换"""
|
||||
paths = ["/Music/Track.mp3", "/music/track.mp3"]
|
||||
rules = [{"pattern": r"(?i)/music/", "replacement": "/Audio/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == ["/Audio/Track.mp3", "/Audio/track.mp3"]
|
||||
|
||||
def test_regex_special_characters(self):
|
||||
"""测试正则特殊字符"""
|
||||
paths = ["/music (2024)/album/track.mp3"]
|
||||
rules = [{"pattern": r"/music \(\d+\)/", "replacement": "/music/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == ["/music/album/track.mp3"]
|
||||
|
||||
def test_capture_group_replacement(self):
|
||||
"""测试捕获组替换"""
|
||||
paths = ["/music/2024/album/track.mp3"]
|
||||
rules = [{"pattern": r"/music/(\d+)/", "replacement": r"/archive/\1/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == ["/archive/2024/album/track.mp3"]
|
||||
|
||||
def test_multiple_capture_groups(self):
|
||||
"""测试多个捕获组"""
|
||||
paths = ["/music/Rock/2024/album.mp3"]
|
||||
rules = [
|
||||
{"pattern": r"/music/([^/]+)/(\d+)/", "replacement": r"/\2/\1/"}
|
||||
]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == ["/2024/Rock/album.mp3"]
|
||||
|
||||
def test_delete_pattern(self):
|
||||
"""测试删除匹配内容(替换为空)"""
|
||||
paths = ["/music/temp/album/track.mp3"]
|
||||
rules = [{"pattern": r"/temp", "replacement": ""}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == ["/music/album/track.mp3"]
|
||||
|
||||
def test_multiple_matches_in_path(self):
|
||||
"""测试路径中多次匹配"""
|
||||
paths = ["/old/path/old/file.mp3"]
|
||||
rules = [{"pattern": r"old", "replacement": "new"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
# Should replace all occurrences
|
||||
assert result == ["/new/path/new/file.mp3"]
|
||||
|
||||
def test_chained_replacements(self):
|
||||
"""测试链式替换"""
|
||||
paths = [r"\\nas\Music\Album\track.mp3"]
|
||||
rules = [
|
||||
{"pattern": r"\\\\nas\\Music", "replacement": "/mnt/music"},
|
||||
{"pattern": r"\\", "replacement": "/"},
|
||||
]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == ["/mnt/music/Album/track.mp3"]
|
||||
|
||||
def test_url_encoding_path(self):
|
||||
"""测试 URL 编码路径处理"""
|
||||
paths = ["/music/artist%20name/track.mp3"]
|
||||
rules = [{"pattern": r"%20", "replacement": " "}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == ["/music/artist name/track.mp3"]
|
||||
|
||||
def test_unicode_path(self):
|
||||
"""测试 Unicode 路径"""
|
||||
paths = ["/音乐/专辑/歌曲.mp3"]
|
||||
rules = [{"pattern": r"/音乐/", "replacement": "/music/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == ["/music/专辑/歌曲.mp3"]
|
||||
|
||||
def test_empty_rules_list(self):
|
||||
"""测试空规则列表"""
|
||||
paths = ["/music/track.mp3"]
|
||||
rules = []
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == paths
|
||||
|
||||
def test_empty_paths_list(self):
|
||||
"""测试空路径列表"""
|
||||
paths = []
|
||||
rules = [{"pattern": r"foo", "replacement": "bar"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result == []
|
||||
|
||||
|
||||
class TestPreprocessPlaylistText:
|
||||
"""测试预处理播放列表文本(含正则替换)"""
|
||||
|
||||
def test_preprocess_with_replacements(self):
|
||||
"""测试带替换的预处理"""
|
||||
text = """#EXTM3U
|
||||
/old/path/track1.mp3
|
||||
/old/path/track2.mp3
|
||||
"""
|
||||
rules = [{"pattern": r"/old/", "replacement": "/new/"}]
|
||||
|
||||
result = preprocess_playlist_text(text, rules)
|
||||
|
||||
assert "#EXTM3U" in result
|
||||
assert "/new/path/track1.mp3" in result
|
||||
assert "/new/path/track2.mp3" in result
|
||||
assert "/old/" not in result
|
||||
|
||||
def test_preprocess_removes_comments(self):
|
||||
"""测试预处理移除注释"""
|
||||
text = """#EXTM3U
|
||||
# This is a comment
|
||||
/music/track1.mp3
|
||||
#EXTINF:123,Artist - Track
|
||||
/music/track2.mp3
|
||||
"""
|
||||
rules = []
|
||||
|
||||
result = preprocess_playlist_text(text, rules)
|
||||
lines = [l for l in result.splitlines() if l and not l.startswith("#")]
|
||||
|
||||
assert len(lines) == 2
|
||||
assert "/music/track1.mp3" in lines
|
||||
assert "/music/track2.mp3" in lines
|
||||
|
||||
def test_preprocess_empty_text(self):
|
||||
"""测试预处理空文本"""
|
||||
text = ""
|
||||
rules = [{"pattern": r"foo", "replacement": "bar"}]
|
||||
|
||||
result = preprocess_playlist_text(text, rules)
|
||||
|
||||
assert "#EXTM3U" in result
|
||||
|
||||
def test_preprocess_with_blank_lines(self):
|
||||
"""测试预处理包含空行的文本"""
|
||||
text = """#EXTM3U
|
||||
|
||||
/music/track1.mp3
|
||||
|
||||
|
||||
/music/track2.mp3
|
||||
|
||||
"""
|
||||
rules = []
|
||||
|
||||
result = preprocess_playlist_text(text, rules)
|
||||
lines = [l for l in result.splitlines() if l and not l.startswith("#")]
|
||||
|
||||
assert len(lines) == 2
|
||||
|
||||
def test_preprocess_real_world_scenario(self):
|
||||
"""测试真实场景:NAS 路径转换"""
|
||||
text = """#EXTM3U
|
||||
\\\\koha9-nas\\koha9-nas\\Music\\Rock\\track1.flac
|
||||
\\\\koha9-nas\\koha9-nas\\Music\\Jazz\\track2.mp3
|
||||
/music/cache/temp.flac
|
||||
"""
|
||||
rules = [
|
||||
{"pattern": r"\\\\koha9-nas\\koha9-nas\\Music", "replacement": r"N:\\Music"},
|
||||
{"pattern": r"/music/cache/", "replacement": "/data/music/"},
|
||||
{"pattern": r"\\", "replacement": "/"},
|
||||
]
|
||||
|
||||
result = preprocess_playlist_text(text, rules)
|
||||
lines = [l for l in result.splitlines() if l and not l.startswith("#")]
|
||||
|
||||
# After all replacements, backslashes should be converted to forward slashes
|
||||
assert "N:/Music/Rock/track1.flac" in lines
|
||||
assert "N:/Music/Jazz/track2.mp3" in lines
|
||||
assert "/data/music/temp.flac" in lines
|
||||
|
||||
def test_preprocess_with_compiled_rules(self):
|
||||
"""测试使用预编译规则"""
|
||||
text = """#EXTM3U
|
||||
/old/path/track.mp3
|
||||
"""
|
||||
rules = [{"pattern": r"/old/", "replacement": "/new/"}]
|
||||
compiled = _compile_regex_rules(rules)
|
||||
|
||||
result = preprocess_playlist_text(text, rules, compiled_rules=compiled)
|
||||
|
||||
assert "/new/path/track.mp3" in result
|
||||
|
||||
def test_preprocess_preserves_order(self):
|
||||
"""测试预处理保持顺序"""
|
||||
text = """#EXTM3U
|
||||
/path/track1.mp3
|
||||
/path/track2.mp3
|
||||
/path/track3.mp3
|
||||
"""
|
||||
rules = [{"pattern": r"/path/", "replacement": "/new/"}]
|
||||
|
||||
result = preprocess_playlist_text(text, rules)
|
||||
lines = [l for l in result.splitlines() if l and not l.startswith("#")]
|
||||
|
||||
assert lines[0] == "/new/track1.mp3"
|
||||
assert lines[1] == "/new/track2.mp3"
|
||||
assert lines[2] == "/new/track3.mp3"
|
||||
|
||||
|
||||
class TestEdgeCases:
|
||||
"""测试边界情况和异常场景"""
|
||||
|
||||
def test_very_long_path(self):
|
||||
"""测试超长路径"""
|
||||
long_path = "/music/" + "a" * 1000 + "/track.mp3"
|
||||
paths = [long_path]
|
||||
rules = [{"pattern": r"/music/", "replacement": "/audio/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result[0].startswith("/audio/")
|
||||
assert len(result[0]) > 1000
|
||||
|
||||
def test_special_characters_in_path(self):
|
||||
"""测试路径中的特殊字符"""
|
||||
paths = [
|
||||
"/music/artist [2024]/track (remix).mp3",
|
||||
"/music/artist & band/song #1.mp3",
|
||||
]
|
||||
rules = [{"pattern": r"/music/", "replacement": "/audio/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result[0] == "/audio/artist [2024]/track (remix).mp3"
|
||||
assert result[1] == "/audio/artist & band/song #1.mp3"
|
||||
|
||||
def test_dot_in_path(self):
|
||||
"""测试路径中的点号"""
|
||||
paths = ["/music/../audio/track.mp3", "/music/./track.mp3"]
|
||||
rules = [{"pattern": r"\.\./", "replacement": ""}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result[0] == "/music/audio/track.mp3"
|
||||
|
||||
def test_trailing_slash(self):
|
||||
"""测试尾部斜杠"""
|
||||
paths = ["/music/album/", "/music/track.mp3"]
|
||||
rules = [{"pattern": r"/$", "replacement": ""}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result[0] == "/music/album"
|
||||
assert result[1] == "/music/track.mp3"
|
||||
|
||||
def test_duplicate_slashes(self):
|
||||
"""测试重复斜杠"""
|
||||
paths = ["/music//album///track.mp3"]
|
||||
rules = [{"pattern": r"/+", "replacement": "/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result[0] == "/music/album/track.mp3"
|
||||
|
||||
def test_mixed_path_separators(self):
|
||||
"""测试混合路径分隔符"""
|
||||
paths = [r"C:\Music/Album\track.mp3"]
|
||||
rules = [
|
||||
{"pattern": r"\\", "replacement": "/"},
|
||||
]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result[0] == "C:/Music/Album/track.mp3"
|
||||
|
||||
def test_regex_metacharacters_in_replacement(self):
|
||||
"""测试替换字符串中的正则元字符"""
|
||||
paths = ["/music/track.mp3"]
|
||||
rules = [{"pattern": r"/music/", "replacement": r"/audio$/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
# $ in replacement should be literal
|
||||
assert result[0] == r"/audio$/track.mp3"
|
||||
|
||||
def test_empty_string_replacement(self):
|
||||
"""测试替换为空字符串"""
|
||||
paths = ["/music/temp/album/track.mp3"]
|
||||
rules = [{"pattern": r"temp/", "replacement": ""}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result[0] == "/music/album/track.mp3"
|
||||
|
||||
def test_replacement_creates_invalid_path(self):
|
||||
"""测试替换可能产生无效路径(但仍应执行)"""
|
||||
paths = ["/music/track.mp3"]
|
||||
rules = [{"pattern": r"/", "replacement": ""}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
# Should still perform replacement even if result is odd
|
||||
assert result[0] == "musictrack.mp3"
|
||||
|
||||
|
||||
class TestPerformance:
|
||||
"""测试性能相关场景"""
|
||||
|
||||
def test_large_playlist(self):
|
||||
"""测试大型播放列表"""
|
||||
paths = [f"/music/track{i}.mp3" for i in range(10000)]
|
||||
rules = [{"pattern": r"/music/", "replacement": "/audio/"}]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert len(result) == 10000
|
||||
assert all(p.startswith("/audio/") for p in result)
|
||||
|
||||
def test_many_rules(self):
|
||||
"""测试大量规则"""
|
||||
paths = ["/music/rock/2024/album/track.mp3"]
|
||||
rules = [
|
||||
{"pattern": r"music", "replacement": "audio"},
|
||||
{"pattern": r"rock", "replacement": "genre1"},
|
||||
{"pattern": r"2024", "replacement": "year"},
|
||||
{"pattern": r"album", "replacement": "collection"},
|
||||
{"pattern": r"track", "replacement": "song"},
|
||||
]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result[0] == "/audio/genre1/year/collection/song.mp3"
|
||||
|
||||
def test_complex_regex_pattern(self):
|
||||
"""测试复杂正则表达式"""
|
||||
paths = [
|
||||
"/music/Artist - Album (2024) [FLAC]/01. Track.flac",
|
||||
"/music/Another Artist - Another Album (2023) [MP3]/02. Song.mp3",
|
||||
]
|
||||
rules = [
|
||||
{
|
||||
"pattern": r"/music/(.+?) - (.+?) \((\d+)\) \[([^\]]+)\]/",
|
||||
"replacement": r"/library/\4/\3/\1/\2/"
|
||||
}
|
||||
]
|
||||
|
||||
result = apply_regex_rules_to_paths(paths, rules)
|
||||
|
||||
assert result[0] == "/library/FLAC/2024/Artist/Album/01. Track.flac"
|
||||
assert result[1] == "/library/MP3/2023/Another Artist/Another Album/02. Song.mp3"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
@@ -0,0 +1,291 @@
|
||||
"""
|
||||
UI 集成测试 - case_mix:清空规则、设置规则并执行四种同步策略
|
||||
|
||||
运行前准备:
|
||||
1. 启动Docker服务: docker compose up -d
|
||||
2. 确保服务运行在 http://localhost:8888
|
||||
|
||||
运行:
|
||||
pytest tests/test_ui_case_mix.py --headed # 显示浏览器
|
||||
pytest tests/test_ui_case_mix.py # 无头模式
|
||||
"""
|
||||
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import time
|
||||
|
||||
from playwright.sync_api import Page, expect
|
||||
|
||||
|
||||
BASE_URL = "http://localhost:8888"
|
||||
|
||||
PROJECT_ROOT = Path(__file__).parent.parent
|
||||
OUTPUT_DIR = PROJECT_ROOT / "output_playlists" / "case_mix"
|
||||
EXPECTED_DIR = PROJECT_ROOT / "test_res"
|
||||
|
||||
|
||||
class SyncStrategy(str, Enum):
|
||||
"""同步策略枚举"""
|
||||
LOCAL_OVERWRITE = "LOCAL_OVERWRITE"
|
||||
CLOUD_OVERWRITE = "CLOUD_OVERWRITE"
|
||||
MERGE_LOCAL = "MERGE_LOCAL"
|
||||
MERGE_CLOUD = "MERGE_CLOUD"
|
||||
|
||||
|
||||
def _handle_connection_modal(page: Page):
|
||||
"""处理登录模态框:如果存在则关闭"""
|
||||
# 检查模态框是否存在 (根据 ConnectionModal.tsx 的结构)
|
||||
# 模态框通常有一个全屏的遮罩层
|
||||
modal_overlay = page.locator("div.fixed.inset-0.z-50")
|
||||
|
||||
if modal_overlay.is_visible():
|
||||
print("检测到登录模态框,尝试关闭...")
|
||||
# 尝试找到关闭按钮 (通常在右上角,包含 X 图标)
|
||||
# 在 ConnectionModal.tsx 中,关闭按钮在 Header 里
|
||||
close_button = modal_overlay.locator("button").filter(has=page.locator("svg")).last
|
||||
|
||||
if close_button.is_visible():
|
||||
close_button.click()
|
||||
else:
|
||||
# 如果找不到关闭按钮,尝试按 ESC
|
||||
page.keyboard.press("Escape")
|
||||
|
||||
page.wait_for_timeout(500) # 等待模态框关闭动画
|
||||
|
||||
|
||||
def _open_strategy_selector(page: Page):
|
||||
"""打开策略选择器下拉菜单"""
|
||||
# 1. 先处理可能遮挡的登录模态框
|
||||
_handle_connection_modal(page)
|
||||
|
||||
# 2. 检查下拉菜单是否已经打开
|
||||
# 下拉菜单的特征类名
|
||||
dropdown = page.locator("div.absolute.top-14")
|
||||
|
||||
if dropdown.is_visible():
|
||||
return # 已经打开,无需操作
|
||||
|
||||
# 3. 查找并点击策略选择器按钮
|
||||
# 使用 title 属性定位更准确 (StrategySelector.tsx 中定义了 title="Current Strategy: ...")
|
||||
strategy_button = page.locator("button[title^='Current Strategy']")
|
||||
|
||||
if strategy_button.count() == 0:
|
||||
# 备用定位方式:查找包含特定图标的圆形按钮
|
||||
# 注意:页面上可能有多个按钮,需要小心
|
||||
# 策略按钮在中间,且包含 ChevronDown 小图标
|
||||
strategy_button = page.locator("button").filter(has=page.locator("svg")).nth(1)
|
||||
# nth(0) 可能是 Header 里的连接按钮
|
||||
|
||||
if strategy_button.is_visible():
|
||||
strategy_button.click()
|
||||
page.wait_for_timeout(300) # 等待下拉菜单动画完成
|
||||
else:
|
||||
print("警告: 无法找到策略选择器按钮")
|
||||
|
||||
|
||||
def _clear_all_rules(page: Page):
|
||||
"""清空所有正则规则"""
|
||||
_open_strategy_selector(page)
|
||||
|
||||
# 等待下拉菜单打开
|
||||
dropdown = page.locator("div.absolute.top-14")
|
||||
expect(dropdown).to_be_visible()
|
||||
|
||||
# 查找并点击所有删除按钮
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
while delete_buttons.count() > 0:
|
||||
try:
|
||||
delete_buttons.first.click()
|
||||
page.wait_for_timeout(100)
|
||||
except Exception:
|
||||
break
|
||||
|
||||
# 关闭下拉菜单
|
||||
page.keyboard.press("Escape")
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
|
||||
def _normalize_playlist_lines(file_path: Path) -> list[str]:
|
||||
"""读取播放列表并返回规范化曲目路径列表(忽略注释与空行)"""
|
||||
if not file_path.exists():
|
||||
return []
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
lines = [
|
||||
line.strip()
|
||||
for line in f
|
||||
if line.strip() and not line.startswith("#")
|
||||
]
|
||||
return lines
|
||||
|
||||
|
||||
def _compare_playlists(actual: Path, expected: Path) -> tuple[bool, str]:
|
||||
"""对比实际输出与期望结果,返回 (是否匹配, 差异描述)"""
|
||||
actual_lines = _normalize_playlist_lines(actual)
|
||||
expected_lines = _normalize_playlist_lines(expected)
|
||||
|
||||
if actual_lines == expected_lines:
|
||||
return True, ""
|
||||
|
||||
# 生成差异报告
|
||||
diff_lines = []
|
||||
diff_lines.append(f"实际曲目数: {len(actual_lines)}, 期望曲目数: {len(expected_lines)}")
|
||||
|
||||
only_actual = set(actual_lines) - set(expected_lines)
|
||||
only_expected = set(expected_lines) - set(actual_lines)
|
||||
|
||||
if only_actual:
|
||||
diff_lines.append(f"仅在实际输出中: {only_actual}")
|
||||
if only_expected:
|
||||
diff_lines.append(f"仅在期望结果中: {only_expected}")
|
||||
|
||||
return False, "\n".join(diff_lines)
|
||||
|
||||
|
||||
def test_case_mix_run_all_modes(page: Page):
|
||||
"""
|
||||
1) 清空当前正则规则
|
||||
2) 填写并保存 case_mix 所用规则
|
||||
3) 依次执行四种同步策略,每次同步后立即验证输出并与期望对比
|
||||
"""
|
||||
# 导航到首页并确认加载(端口为 8080)
|
||||
page.goto(BASE_URL + "/")
|
||||
page.wait_for_load_state("networkidle")
|
||||
page.wait_for_timeout(1000) # 等待React应用初始化
|
||||
expect(page).to_have_url(BASE_URL + "/")
|
||||
|
||||
# 处理可能出现的登录模态框
|
||||
_handle_connection_modal(page)
|
||||
|
||||
# 1. 清空规则
|
||||
_clear_all_rules(page)
|
||||
|
||||
# 2. 添加并保存 case_mix 所用规则(顺序很重要)
|
||||
rules = [
|
||||
(r"^\\\\koha9-nas\\koha9-nas\\Music", r"N:\\Music"), # UNC 到盘符
|
||||
(r"^/mnt/music", r"N:\\Music"), # Linux 挂载到盘符
|
||||
(r"(?i)^N:\\MUSIC", r"N:\\Music"), # 大小写规范化
|
||||
(r"/", r"\\"), # 斜杠统一为反斜杠(需在映射后)
|
||||
]
|
||||
|
||||
# 打开策略选择器
|
||||
_open_strategy_selector(page)
|
||||
|
||||
# 添加规则
|
||||
for pattern, replacement in rules:
|
||||
# 点击 "Add Rule" 按钮
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
# 如果没有"Add Rule"按钮,尝试使用带文本的按钮
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
add_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
# 填写最后一组输入框
|
||||
pattern_inputs = page.locator("input[placeholder='Regex Pattern']")
|
||||
replacement_inputs = page.locator("input[placeholder='Replacement']")
|
||||
|
||||
pattern_inputs.last.fill(pattern)
|
||||
replacement_inputs.last.fill(replacement)
|
||||
page.wait_for_timeout(100)
|
||||
|
||||
# 保存规则 - 点击 "Save Changes" 按钮
|
||||
save_button = page.locator("button:has-text('Save Changes')")
|
||||
expect(save_button).to_be_enabled()
|
||||
save_button.click()
|
||||
page.wait_for_timeout(500) # 等待保存完成
|
||||
|
||||
# 验证保存成功 - 检查toast通知
|
||||
toast = page.locator("div:has-text('Regex preprocessing rules have been saved')")
|
||||
if toast.count() > 0:
|
||||
expect(toast.first).to_be_visible()
|
||||
|
||||
# 关闭下拉菜单
|
||||
page.keyboard.press("Escape")
|
||||
page.wait_for_timeout(300)
|
||||
|
||||
# 3. 依次执行四种同步模式,每次执行后立即验证
|
||||
# 策略名称映射: UI中的策略值 -> 测试用例名称
|
||||
strategy_mappings = [
|
||||
(SyncStrategy.LOCAL_OVERWRITE, "Local Overwrite", "case_mix_local_force.m3u"),
|
||||
(SyncStrategy.CLOUD_OVERWRITE, "Cloud Overwrite", "case_mix_remote_force.m3u"),
|
||||
(SyncStrategy.MERGE_LOCAL, "Two-way Merge (Local Priority)", "case_mix_merge_local_primary.m3u"),
|
||||
(SyncStrategy.MERGE_CLOUD, "Two-way Merge (Cloud Priority)", "case_mix_merge_remote_primary.m3u"),
|
||||
]
|
||||
|
||||
# 准备初始 Base(每次测试前恢复)
|
||||
initial_base_content = """#EXTM3U
|
||||
N:\\Music\\Anime\\New PANTY & STOCKING with GARTERBELT\\Theme of New PANTY & STOCKING\\02. Reckless - Theme of New PANTY & STOCKING.flac
|
||||
N:\\Music\\Anime\\CITY THE ANIMATION\\Hello\\01. Hello - Hello.flac
|
||||
N:\\Music\\音ゲー\\USAO\\MEMORIES\\15. Empty Room (Srav3R Remix) - MEMORIES.flac
|
||||
"""
|
||||
|
||||
for strategy_value, strategy_label, expected_file in strategy_mappings:
|
||||
print(f"\n==== 执行同步策略: {strategy_label} ====")
|
||||
|
||||
# 恢复初始 Base(避免前次同步影响)
|
||||
base_next_path = OUTPUT_DIR / "base_next.m3u8"
|
||||
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
||||
with open(base_next_path, "w", encoding="utf-8") as f:
|
||||
f.write(initial_base_content)
|
||||
print(f"已恢复初始 Base: {base_next_path}")
|
||||
|
||||
# 选择策略 - 打开下拉菜单
|
||||
_open_strategy_selector(page)
|
||||
|
||||
# 点击对应的策略选项 - 更精确的定位
|
||||
# 找到包含策略名称的可点击div (class包含cursor-pointer)
|
||||
strategy_option = page.locator("div.cursor-pointer").filter(has_text=strategy_label)
|
||||
expect(strategy_option.first).to_be_visible()
|
||||
strategy_option.first.click()
|
||||
page.wait_for_timeout(500) # 等待策略保存
|
||||
|
||||
# 验证策略选择成功的toast
|
||||
toast = page.locator(f"div:has-text('Selected strategy \"{strategy_label}\" has been saved')")
|
||||
if toast.count() > 0:
|
||||
expect(toast.first).to_be_visible(timeout=3000)
|
||||
|
||||
# 关闭下拉菜单
|
||||
page.keyboard.press("Escape")
|
||||
page.wait_for_timeout(300)
|
||||
|
||||
# 执行同步 - 通过API触发同步操作
|
||||
# 新UI需要显式调用同步API
|
||||
import requests
|
||||
sync_response = requests.post(
|
||||
f"{BASE_URL}/api/sync",
|
||||
json={"mode": None} # 使用当前配置的策略
|
||||
)
|
||||
assert sync_response.status_code == 200, f"同步API调用失败: {sync_response.text}"
|
||||
print(f"同步API响应: {sync_response.json()}")
|
||||
|
||||
time.sleep(1) # 确保文件写入完成
|
||||
|
||||
# 验证输出文件生成
|
||||
local_result = OUTPUT_DIR / "local_result.m3u8"
|
||||
remote_result = OUTPUT_DIR / "remote_result.m3u8"
|
||||
base_next = OUTPUT_DIR / "base_next.m3u8"
|
||||
|
||||
assert local_result.exists(), f"{strategy_label}: local_result.m3u8 未生成"
|
||||
assert remote_result.exists(), f"{strategy_label}: remote_result.m3u8 未生成"
|
||||
assert base_next.exists(), f"{strategy_label}: base_next.m3u8 未生成"
|
||||
|
||||
# 与期望结果对比(使用 local_result.m3u8 作为主要输出)
|
||||
expected_path = EXPECTED_DIR / expected_file
|
||||
match, diff = _compare_playlists(local_result, expected_path)
|
||||
|
||||
# 备份当前输出以便后续检查
|
||||
backup_dir = OUTPUT_DIR / f"backup_{strategy_value}"
|
||||
backup_dir.mkdir(exist_ok=True)
|
||||
shutil.copy(local_result, backup_dir / "local_result.m3u8")
|
||||
shutil.copy(remote_result, backup_dir / "remote_result.m3u8")
|
||||
shutil.copy(base_next, backup_dir / "base_next.m3u8")
|
||||
|
||||
print(f"输出已备份到: {backup_dir}")
|
||||
|
||||
# 断言匹配
|
||||
assert match, f"{strategy_label} 输出与期望不符:\n{diff}"
|
||||
print(f"✓ {strategy_label} 验证通过")
|
||||
|
||||
print("\n==== 全部四种策略测试通过 ====")
|
||||
@@ -0,0 +1,566 @@
|
||||
"""
|
||||
UI 集成测试 - 使用 Playwright 测试正则路径替换功能
|
||||
|
||||
运行前准备:
|
||||
1. 启动Docker服务: docker compose up -d
|
||||
2. 确保服务运行在 http://localhost:8888
|
||||
|
||||
安装:
|
||||
pip install pytest-playwright
|
||||
playwright install
|
||||
|
||||
运行:
|
||||
pytest tests/test_ui_regex_rules.py --headed # 显示浏览器
|
||||
pytest tests/test_ui_regex_rules.py # 无头模式
|
||||
"""
|
||||
|
||||
import re
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from playwright.sync_api import Page, expect
|
||||
|
||||
|
||||
# 测试服务器地址 - Docker映射端口
|
||||
BASE_URL = "http://localhost:8888"
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def test_server():
|
||||
"""启动测试服务器(可选,如果服务器未运行)"""
|
||||
# 如果你的服务器已经在运行,直接返回
|
||||
# 否则可以在这里启动服务器进程
|
||||
yield BASE_URL
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def page(page: Page, test_server):
|
||||
"""配置页面并导航到首页"""
|
||||
page.goto(test_server)
|
||||
page.wait_for_load_state("networkidle")
|
||||
return page
|
||||
|
||||
|
||||
class TestRegexRulesUI:
|
||||
"""测试正则路径替换规则的 UI 交互 - 适配新React UI"""
|
||||
|
||||
def _handle_connection_modal(self, page: Page):
|
||||
"""处理登录模态框"""
|
||||
modal_overlay = page.locator("div.fixed.inset-0.z-50")
|
||||
if modal_overlay.is_visible():
|
||||
close_button = modal_overlay.locator("button").filter(has=page.locator("svg")).last
|
||||
if close_button.is_visible():
|
||||
close_button.click()
|
||||
else:
|
||||
page.keyboard.press("Escape")
|
||||
page.wait_for_timeout(500)
|
||||
|
||||
def _open_strategy_selector(self, page: Page):
|
||||
"""打开策略选择器下拉菜单"""
|
||||
self._handle_connection_modal(page)
|
||||
|
||||
dropdown = page.locator("div.absolute.top-14")
|
||||
if dropdown.is_visible():
|
||||
return
|
||||
|
||||
strategy_button = page.locator("button[title^='Current Strategy']")
|
||||
if strategy_button.count() == 0:
|
||||
strategy_button = page.locator("button").filter(has=page.locator("svg")).nth(1)
|
||||
|
||||
if strategy_button.is_visible():
|
||||
strategy_button.click()
|
||||
page.wait_for_timeout(300)
|
||||
|
||||
def _close_strategy_selector(self, page: Page):
|
||||
"""关闭策略选择器"""
|
||||
page.keyboard.press("Escape")
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
def test_page_loads_successfully(self, page: Page):
|
||||
"""测试页面成功加载"""
|
||||
expect(page).to_have_title(re.compile("Plex.*Sync", re.I))
|
||||
|
||||
# 检查关键元素存在 - 新UI的主要元素
|
||||
# 检查策略选择器按钮存在
|
||||
strategy_button = page.locator("button").filter(has=page.locator("svg")).first
|
||||
expect(strategy_button).to_be_visible()
|
||||
|
||||
def test_add_single_rule(self, page: Page):
|
||||
"""测试添加单个规则"""
|
||||
# 打开策略选择器
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 等待下拉菜单可见
|
||||
dropdown = page.locator("div.absolute.top-14")
|
||||
expect(dropdown).to_be_visible()
|
||||
|
||||
# 获取初始规则数量
|
||||
initial_count = page.locator("input[placeholder='Regex Pattern']").count()
|
||||
|
||||
# 点击添加规则按钮
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
add_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
# 验证规则数量增加
|
||||
new_count = page.locator("input[placeholder='Regex Pattern']").count()
|
||||
assert new_count == initial_count + 1
|
||||
|
||||
# 填写规则内容
|
||||
pattern_inputs = page.locator("input[placeholder='Regex Pattern']")
|
||||
replacement_inputs = page.locator("input[placeholder='Replacement']")
|
||||
|
||||
pattern_inputs.last.fill(r"/old/path/")
|
||||
replacement_inputs.last.fill(r"/new/path/")
|
||||
|
||||
# 验证填写成功
|
||||
assert pattern_inputs.last.input_value() == r"/old/path/"
|
||||
assert replacement_inputs.last.input_value() == r"/new/path/"
|
||||
|
||||
self._close_strategy_selector(page)
|
||||
|
||||
def test_add_multiple_rules(self, page: Page):
|
||||
"""测试添加多个规则"""
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
rules = [
|
||||
(r"\\\\nas\\Music", r"N:\\Music"),
|
||||
(r"/music/cache/", r"/data/music/"),
|
||||
(r"\\", r"/"),
|
||||
]
|
||||
|
||||
# 添加多个规则
|
||||
for pattern, replacement in rules:
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
add_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
pattern_inputs = page.locator("input[placeholder='Regex Pattern']")
|
||||
replacement_inputs = page.locator("input[placeholder='Replacement']")
|
||||
|
||||
pattern_inputs.last.fill(pattern)
|
||||
replacement_inputs.last.fill(replacement)
|
||||
|
||||
# 验证所有规则都已添加
|
||||
pattern_inputs = page.locator("input[placeholder='Regex Pattern']")
|
||||
assert pattern_inputs.count() >= len(rules)
|
||||
|
||||
# 验证规则内容
|
||||
for i in range(len(rules)):
|
||||
idx = pattern_inputs.count() - len(rules) + i
|
||||
assert rules[i][0] in pattern_inputs.nth(idx).input_value()
|
||||
|
||||
self._close_strategy_selector(page)
|
||||
|
||||
def test_remove_rule(self, page: Page):
|
||||
"""测试删除规则"""
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 获取初始规则数量
|
||||
initial_count = page.locator("input[placeholder='Regex Pattern']").count()
|
||||
|
||||
# 添加一个规则
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
add_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
new_count = page.locator("input[placeholder='Regex Pattern']").count()
|
||||
assert new_count == initial_count + 1
|
||||
|
||||
# 找到删除按钮(最后一个规则的删除按钮)
|
||||
remove_buttons = page.locator("button[title='Delete Rule']")
|
||||
if remove_buttons.count() > 0:
|
||||
remove_buttons.last.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
# 验证规则已删除
|
||||
final_count = page.locator("input[placeholder='Regex Pattern']").count()
|
||||
assert final_count == initial_count
|
||||
|
||||
self._close_strategy_selector(page)
|
||||
|
||||
def test_save_rules(self, page: Page):
|
||||
"""测试保存规则"""
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 清除现有规则
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
while delete_buttons.count() > 0:
|
||||
delete_buttons.first.click()
|
||||
page.wait_for_timeout(100)
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
|
||||
# 添加测试规则
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
add_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
pattern_input = page.locator("input[placeholder='Regex Pattern']").last
|
||||
replacement_input = page.locator("input[placeholder='Replacement']").last
|
||||
|
||||
test_pattern = r"/test/path/"
|
||||
test_replacement = r"/new/path/"
|
||||
|
||||
pattern_input.fill(test_pattern)
|
||||
replacement_input.fill(test_replacement)
|
||||
page.wait_for_timeout(100)
|
||||
|
||||
# 点击保存按钮
|
||||
save_button = page.locator("button:has-text('Save Changes')")
|
||||
expect(save_button).to_be_enabled()
|
||||
save_button.click()
|
||||
page.wait_for_timeout(500)
|
||||
|
||||
# 验证成功消息
|
||||
toast = page.locator("div:has-text('Regex preprocessing rules have been saved')")
|
||||
if toast.count() > 0:
|
||||
expect(toast.first).to_be_visible(timeout=3000)
|
||||
|
||||
self._close_strategy_selector(page)
|
||||
|
||||
def test_rules_persist_after_save(self, page: Page):
|
||||
"""测试规则保存后持久化"""
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 清除并添加新规则
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
while delete_buttons.count() > 0:
|
||||
delete_buttons.first.click()
|
||||
page.wait_for_timeout(100)
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
add_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
test_pattern = r"C:\\Music"
|
||||
test_replacement = r"D:\\Audio"
|
||||
|
||||
page.locator("input[placeholder='Regex Pattern']").last.fill(test_pattern)
|
||||
page.locator("input[placeholder='Replacement']").last.fill(test_replacement)
|
||||
page.wait_for_timeout(100)
|
||||
|
||||
# 保存
|
||||
save_button = page.locator("button:has-text('Save Changes')")
|
||||
save_button.click()
|
||||
page.wait_for_timeout(500)
|
||||
|
||||
self._close_strategy_selector(page)
|
||||
|
||||
# 刷新页面
|
||||
page.reload()
|
||||
page.wait_for_load_state("networkidle")
|
||||
page.wait_for_timeout(1000)
|
||||
|
||||
# 重新打开策略选择器
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 验证规则仍然存在
|
||||
pattern_inputs = page.locator("input[placeholder='Regex Pattern']")
|
||||
|
||||
# 检查是否有匹配的规则
|
||||
found = False
|
||||
for i in range(pattern_inputs.count()):
|
||||
if test_pattern in pattern_inputs.nth(i).input_value():
|
||||
found = True
|
||||
# 验证对应的替换值
|
||||
replacement_inputs = page.locator("input[placeholder='Replacement']")
|
||||
replacement_value = replacement_inputs.nth(i).input_value()
|
||||
assert test_replacement in replacement_value
|
||||
break
|
||||
|
||||
assert found, f"未找到保存的规则: {test_pattern}"
|
||||
|
||||
self._close_strategy_selector(page)
|
||||
|
||||
def test_empty_pattern_validation(self, page: Page):
|
||||
"""测试空模式验证 - 新UI在保存时会过滤空规则"""
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 添加规则但不填写
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
add_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
# 只填写替换,不填写模式
|
||||
replacement_input = page.locator("input[placeholder='Replacement']").last
|
||||
replacement_input.fill("/new/path/")
|
||||
page.wait_for_timeout(100)
|
||||
|
||||
# 尝试保存 - 新UI会自动过滤空模式的规则
|
||||
save_button = page.locator("button:has-text('Save Changes')")
|
||||
if save_button.is_enabled():
|
||||
initial_count = page.locator("input[placeholder='Regex Pattern']").count()
|
||||
save_button.click()
|
||||
page.wait_for_timeout(500)
|
||||
|
||||
# 验证空规则被过滤(如果实现了这个逻辑)
|
||||
# 注意: 这取决于后端实现
|
||||
|
||||
self._close_strategy_selector(page)
|
||||
|
||||
def test_rule_order_preserved(self, page: Page):
|
||||
"""测试规则顺序保持"""
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 清除现有规则
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
while delete_buttons.count() > 0:
|
||||
delete_buttons.first.click()
|
||||
page.wait_for_timeout(100)
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
|
||||
# 按顺序添加多个规则
|
||||
rules = [
|
||||
("rule1", "replacement1"),
|
||||
("rule2", "replacement2"),
|
||||
("rule3", "replacement3"),
|
||||
]
|
||||
|
||||
for pattern, replacement in rules:
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
add_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
page.locator("input[placeholder='Regex Pattern']").last.fill(pattern)
|
||||
page.locator("input[placeholder='Replacement']").last.fill(replacement)
|
||||
page.wait_for_timeout(100)
|
||||
|
||||
# 验证顺序
|
||||
pattern_inputs = page.locator("input[placeholder='Regex Pattern']")
|
||||
count = pattern_inputs.count()
|
||||
|
||||
for i, (pattern, _) in enumerate(rules):
|
||||
idx = count - len(rules) + i
|
||||
assert pattern in pattern_inputs.nth(idx).input_value()
|
||||
|
||||
self._close_strategy_selector(page)
|
||||
|
||||
|
||||
class TestComplexScenarios:
|
||||
"""测试复杂场景"""
|
||||
|
||||
def _handle_connection_modal(self, page: Page):
|
||||
"""处理登录模态框"""
|
||||
modal_overlay = page.locator("div.fixed.inset-0.z-50")
|
||||
if modal_overlay.is_visible():
|
||||
close_button = modal_overlay.locator("button").filter(has=page.locator("svg")).last
|
||||
if close_button.is_visible():
|
||||
close_button.click()
|
||||
else:
|
||||
page.keyboard.press("Escape")
|
||||
page.wait_for_timeout(500)
|
||||
|
||||
def _open_strategy_selector(self, page: Page):
|
||||
"""打开策略选择器下拉菜单"""
|
||||
self._handle_connection_modal(page)
|
||||
|
||||
dropdown = page.locator("div.absolute.top-14")
|
||||
if dropdown.is_visible():
|
||||
return
|
||||
|
||||
strategy_button = page.locator("button[title^='Current Strategy']")
|
||||
if strategy_button.count() == 0:
|
||||
strategy_button = page.locator("button").filter(has=page.locator("svg")).nth(1)
|
||||
|
||||
if strategy_button.is_visible():
|
||||
strategy_button.click()
|
||||
page.wait_for_timeout(300)
|
||||
|
||||
def _close_strategy_selector(self, page: Page):
|
||||
"""关闭策略选择器"""
|
||||
page.keyboard.press("Escape")
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
def test_windows_to_linux_path_conversion(self, page: Page):
|
||||
"""测试 Windows 到 Linux 路径转换场景"""
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 清除规则
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
while delete_buttons.count() > 0:
|
||||
delete_buttons.first.click()
|
||||
page.wait_for_timeout(100)
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
|
||||
# 添加转换规则
|
||||
conversion_rules = [
|
||||
(r"C:\\Music", r"/mnt/music"),
|
||||
(r"\\", r"/"),
|
||||
]
|
||||
|
||||
for pattern, replacement in conversion_rules:
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
add_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
page.locator("input[placeholder='Regex Pattern']").last.fill(pattern)
|
||||
page.locator("input[placeholder='Replacement']").last.fill(replacement)
|
||||
page.wait_for_timeout(100)
|
||||
|
||||
# 保存
|
||||
save_button = page.locator("button:has-text('Save Changes')")
|
||||
save_button.click()
|
||||
page.wait_for_timeout(500)
|
||||
|
||||
# 验证保存成功
|
||||
toast = page.locator("div:has-text('Regex preprocessing rules have been saved')")
|
||||
if toast.count() > 0:
|
||||
expect(toast.first).to_be_visible(timeout=3000)
|
||||
|
||||
self._close_strategy_selector(page)
|
||||
|
||||
def test_nas_path_normalization(self, page: Page):
|
||||
"""测试 NAS 路径规范化"""
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 清除规则
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
while delete_buttons.count() > 0:
|
||||
delete_buttons.first.click()
|
||||
page.wait_for_timeout(100)
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
|
||||
# NAS 路径规范化规则
|
||||
nas_rules = [
|
||||
(r"\\\\koha9-nas\\koha9-nas\\Music", r"N:\\Music"),
|
||||
(r"/music/cache/", r"/data/music/"),
|
||||
(r"\\", r"/"),
|
||||
]
|
||||
|
||||
for pattern, replacement in nas_rules:
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
add_button.click()
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
page.locator("input[placeholder='Regex Pattern']").last.fill(pattern)
|
||||
page.locator("input[placeholder='Replacement']").last.fill(replacement)
|
||||
page.wait_for_timeout(100)
|
||||
|
||||
# 保存并验证
|
||||
save_button = page.locator("button:has-text('Save Changes')")
|
||||
save_button.click()
|
||||
page.wait_for_timeout(500)
|
||||
|
||||
# 验证成功
|
||||
toast = page.locator("div:has-text('Regex preprocessing rules have been saved')")
|
||||
if toast.count() > 0:
|
||||
expect(toast.first).to_be_visible(timeout=3000)
|
||||
|
||||
self._close_strategy_selector(page)
|
||||
page.wait_for_load_state("networkidle")
|
||||
|
||||
# 刷新验证持久化
|
||||
page.reload()
|
||||
page.wait_for_load_state("networkidle")
|
||||
|
||||
# 重新打开策略选择器
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 验证所有规则都保存了
|
||||
pattern_inputs = page.locator("input[placeholder='Regex Pattern']")
|
||||
saved_patterns = [pattern_inputs.nth(i).input_value() for i in range(pattern_inputs.count())]
|
||||
|
||||
for pattern, _ in nas_rules:
|
||||
assert any(pattern in saved for saved in saved_patterns), f"规则未保存: {pattern}"
|
||||
|
||||
|
||||
@pytest.mark.slow
|
||||
class TestPerformance:
|
||||
"""性能测试"""
|
||||
|
||||
def _handle_connection_modal(self, page: Page):
|
||||
"""处理登录模态框"""
|
||||
modal_overlay = page.locator("div.fixed.inset-0.z-50")
|
||||
if modal_overlay.is_visible():
|
||||
close_button = modal_overlay.locator("button").filter(has=page.locator("svg")).last
|
||||
if close_button.is_visible():
|
||||
close_button.click()
|
||||
else:
|
||||
page.keyboard.press("Escape")
|
||||
page.wait_for_timeout(500)
|
||||
|
||||
def _open_strategy_selector(self, page: Page):
|
||||
"""打开策略选择器下拉菜单"""
|
||||
self._handle_connection_modal(page)
|
||||
|
||||
dropdown = page.locator("div.absolute.top-14")
|
||||
if dropdown.is_visible():
|
||||
return
|
||||
|
||||
strategy_button = page.locator("button[title^='Current Strategy']")
|
||||
if strategy_button.count() == 0:
|
||||
strategy_button = page.locator("button").filter(has=page.locator("svg")).nth(1)
|
||||
|
||||
if strategy_button.is_visible():
|
||||
strategy_button.click()
|
||||
page.wait_for_timeout(300)
|
||||
|
||||
def test_add_many_rules_performance(self, page: Page):
|
||||
"""测试添加大量规则的性能"""
|
||||
self._open_strategy_selector(page)
|
||||
|
||||
# 清除规则
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
while delete_buttons.count() > 0:
|
||||
delete_buttons.first.click()
|
||||
page.wait_for_timeout(50)
|
||||
delete_buttons = page.locator("button[title='Delete Rule']")
|
||||
|
||||
# 测试添加 20 个规则
|
||||
add_button = page.locator("button[title='Add Rule']")
|
||||
if add_button.count() == 0:
|
||||
add_button = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
start_time = time.time()
|
||||
for i in range(20):
|
||||
# 重新定位按钮以确保引用的有效性
|
||||
current_add_btn = page.locator("button[title='Add Rule']")
|
||||
if current_add_btn.count() == 0:
|
||||
current_add_btn = page.locator("button:has-text('Add Rule')")
|
||||
|
||||
current_add_btn.click()
|
||||
# 给一点时间让 React 更新 DOM,避免操作过快导致浏览器崩溃或状态不同步
|
||||
page.wait_for_timeout(100)
|
||||
|
||||
page.locator("input[placeholder='Regex Pattern']").last.fill(f"pattern{i}")
|
||||
page.locator("input[placeholder='Replacement']").last.fill(f"replacement{i}")
|
||||
|
||||
end_time = time.time()
|
||||
|
||||
# 验证时间合理
|
||||
elapsed = end_time - start_time
|
||||
assert elapsed < 20, f"添加 20 个规则耗时过长: {elapsed:.2f}s" # 验证数量
|
||||
assert page.locator("input[placeholder='Regex Pattern']").count() >= 20
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v", "--headed"])
|
||||
Reference in New Issue
Block a user