import sqlite3 import tempfile import unittest from contextlib import closing from datetime import date, datetime, timedelta, timezone from pathlib import Path import app BASE_TIME = datetime(2026, 10, 12, 2, 0, tzinfo=timezone.utc) class SSSAppTests(unittest.TestCase): def make_database(self, directory: str, at: datetime = BASE_TIME) -> Path: db_path = Path(directory) / "sss.db" app.initialize_database(db_path, at) return db_path def bind(self, connection, device="device-test0001", actor="妈妈", at=BASE_TIME): app.bind_device(connection, device, actor, at) return device def play_and_complete( self, connection, device, song_id, at, study_date="2026-10-12", media_type="video", ): play_id = app.start_playback_event( connection, device, song_id, study_date, media_type, at ) result = app.complete_playback_event( connection, device, play_id, 90, at + timedelta(minutes=1) ) return play_id, result def test_curriculum_has_all_three_asset_types(self): self.assertEqual(app.CURRICULUM["meta"]["week_count"], 50) self.assertEqual(app.CURRICULUM["meta"]["song_count"], 100) song = app.CURRICULUM["songs_by_id"]["28-3"] self.assertEqual(song["video_file"], "28-3 The Shape Song.mp4") self.assertEqual(song["audio_file"], "28-3 The Shape Song.mp3") self.assertEqual(song["lyrics_file"], "28-3 The Shape Song.jpg") def test_active_asset_root_finds_macos_suffixed_study_mount(self): with tempfile.TemporaryDirectory() as temp_dir: volumes_root = Path(temp_dir) / "Volumes" configured_root = volumes_root / "study" / "Assets" / "SSS50" fallback_root = volumes_root / "study-1" / "Assets" / "SSS50" (fallback_root / "视频").mkdir(parents=True) self.assertEqual( app.active_asset_root(configured_root, volumes_root), fallback_root ) def test_resource_catalog_lists_every_song_and_three_lazy_assets(self): with tempfile.TemporaryDirectory() as temp_dir: payload = app.resources_payload(Path(temp_dir)) self.assertEqual(payload["summary"]["songs"], 100) self.assertEqual(len(payload["songs"]), 100) first = payload["songs"][0] self.assertEqual(first["id"], "1-1") self.assertEqual(first["week"], 1) self.assertEqual(set(first["urls"]), {"video", "audio", "lyrics"}) self.assertFalse(any(first["available"].values())) resource_html = (app.PUBLIC_DIR / "resources.html").read_text(encoding="utf-8") resource_js = (app.PUBLIC_DIR / "resources.js").read_text(encoding="utf-8") self.assertNotIn('id="resource-viewer"', resource_html) self.assertIn('data-resource-detail', resource_js) self.assertIn('data-resource-video data-video-url=', resource_js) self.assertIn('data-resource-audio data-audio-url=', resource_js) self.assertIn('video.src = video.dataset.videoUrl', resource_js) self.assertIn('audio.src = audio.dataset.audioUrl', resource_js) self.assertNotIn('mode: "info"', resource_js) self.assertNotIn('songs[0]?.id', resource_js) def test_home_uses_one_lazy_media_card_per_song(self): home_html = (app.PUBLIC_DIR / "index.html").read_text(encoding="utf-8") home_js = (app.PUBLIC_DIR / "app.js").read_text(encoding="utf-8") resource_js = (app.PUBLIC_DIR / "resources.js").read_text(encoding="utf-8") stats_js = (app.PUBLIC_DIR / "stats.js").read_text(encoding="utf-8") self.assertIn('id="song-cards"', home_html) self.assertNotIn('id="current-song-id"', home_html) self.assertNotIn('id="audio-previous"', home_html) self.assertNotIn('id="loop-toggle"', home_html) self.assertIn('data-video-url="${song.video_url}"', home_js) self.assertIn('data-audio-url="${song.audio_url}"', home_js) self.assertIn('player.src = player.dataset[urlKey]', home_js) self.assertIn('const urlKey = mediaType === "audio" ? "audioUrl" : "videoUrl"', home_js) self.assertNotIn("独立播放", home_html) self.assertNotIn("一首歌一个窗口", home_html) self.assertIn('id="round-hint"', home_html) self.assertIn('"上午领取", "下午领取", "晚上领取"', home_js) self.assertIn('id="playlist-loop-button"', home_html) self.assertIn('id="weekly-loop-player"', home_html) self.assertIn('loop playsinline webkit-playsinline', home_html) self.assertIn('id="child-mode"', home_html) self.assertIn("async function startPlaylistAt", home_js) self.assertIn('playCardMedia(song.id, "audio", "single")', home_js) self.assertIn('player.src = `/api/week-audio/${weekNumber}`', home_js) self.assertIn('mode: "weekly-loop"', home_js) self.assertIn('elements.weeklyLoopPlayer.pause()', home_js) self.assertIn('function isIPhone5sOnIOS10()', home_js) self.assertIn('function childSongCardMarkup(song, index)', home_js) self.assertIn('appState.childMode ? "audio"', home_js) self.assertIn('course_week: appState.data.week.number', home_js) self.assertIn('data-week-number=', home_js) self.assertIn('SSS 50 周打卡', home_html) self.assertIn('id="identity-button"', home_html) self.assertIn('id="bottom-settings-button"', home_html) self.assertIn('id="start-date" name="start_date" type="date" value="2026-10-12" disabled', home_html) self.assertNotIn('id="settings-button"', home_html) self.assertNotIn('if (mode !== "video") video.pause()', home_js) self.assertNotIn('if (mode !== "audio") audio.pause()', home_js) self.assertNotIn("