Jelajahi Sumber

Add ENABLED flags to refresh-all.ts; test that flags match config.json

All services are listed with explicit true/false rather than being
commented out. test_refresh_all_flags_match_config asserts full
coverage and that each flag agrees with config.json disabled.
ZiB 4 bulan lalu
induk
melakukan
fac46bdce3

+ 17 - 6
room_cal/service_api/typescript/scripts/refresh-all.ts

@@ -1,22 +1,33 @@
 import { refresh as refreshAirbnb } from '../airbnb/refresh-cookies';
 import { refresh as refreshBooking } from '../booking/refresh-cookies';
-// import { refresh as refreshHomestay } from '../homestay/refresh-cookies';  // disabled
+import { refresh as refreshHomestay } from '../homestay/refresh-cookies';
 import { handleOutput } from '../shared/api-client';
 import { parseOutputMode } from '../shared/cookie-utils';
 import * as path from 'path';
 import * as os from 'os';
 
+// Keep in sync with services[*].disabled in config.json.
+// Test: tests/test_refresh_cookies.py::test_refresh_all_flags_match_config
+const ENABLED: Record<string, boolean> = {
+  airbnb: true,
+  booking: true,
+  homestay: false,
+};
+
 const DATA_HOME = process.env.ROOM_CAL_DATA_HOME ?? path.join(os.homedir(), '.local', 'room_cal');
 const mode = parseOutputMode(process.argv);
 
 async function main(): Promise<void> {
-  const [airbnbCreds, bookingCreds] = await Promise.all([
-    refreshAirbnb(mode),
-    refreshBooking(mode),
+  const results = await Promise.all([
+    ENABLED.airbnb  ? refreshAirbnb(mode)  : Promise.resolve(null),
+    ENABLED.booking ? refreshBooking(mode) : Promise.resolve(null),
+    ENABLED.homestay ? refreshHomestay(mode) : Promise.resolve(null),
   ]);
+  const [airbnbCreds, bookingCreds, homestayCreds] = results;
   await Promise.all([
-    handleOutput('airbnb', airbnbCreds, mode, path.join(DATA_HOME, 'airbnb.env')),
-    handleOutput('booking', bookingCreds, mode, path.join(DATA_HOME, 'booking.env')),
+    ENABLED.airbnb   && handleOutput('airbnb',   airbnbCreds!,   mode, path.join(DATA_HOME, 'airbnb.env')),
+    ENABLED.booking  && handleOutput('booking',  bookingCreds!,  mode, path.join(DATA_HOME, 'booking.env')),
+    ENABLED.homestay && handleOutput('homestay', homestayCreds!, mode, path.join(DATA_HOME, 'homestay.env')),
   ]);
   if (mode !== 'stdout') console.error('\nAll credentials refreshed.');
 }

+ 34 - 1
tests/test_refresh_cookies.py

@@ -1,5 +1,6 @@
 import asyncio
 import os
+import re
 import subprocess
 import warnings
 from datetime import date, timedelta
@@ -8,7 +9,7 @@ from pathlib import Path
 
 import pytest
 
-from room_cal._utils import data_home as _data_home
+from room_cal._utils import data_home as _data_home, get_config
 from room_cal.service_api.types import ServiceAuthError
 
 
@@ -63,6 +64,38 @@ def chrome_installed():
     pytest.fail(f'install-browsers exited 0 but no chromium dirs found in {browsers}')
 
 
+def test_refresh_all_flags_match_config():
+  """ENABLED flags in refresh-all.ts must match disabled in config.json, with full coverage."""
+  ts_path = _ts_dir() / 'scripts' / 'refresh-all.ts'
+  src = ts_path.read_text()
+
+  # Parse: ENABLED: Record<string, boolean> = { key: true/false, ... }
+  block = re.search(r'const ENABLED[^=]*=\s*\{([^}]+)\}', src)
+  assert block, 'Could not find ENABLED block in refresh-all.ts'
+  ts_flags: dict[str, bool] = {}
+  for m in re.finditer(r'(\w+)\s*:\s*(true|false)', block.group(1)):
+    ts_flags[m.group(1)] = m.group(2) == 'true'
+
+  cfg_services = get_config().services
+  cfg_keys = set(cfg_services.keys())
+  ts_keys = set(ts_flags.keys())
+
+  missing = cfg_keys - ts_keys
+  assert not missing, f'Services in config.json missing from refresh-all.ts ENABLED: {missing}'
+
+  extra = ts_keys - cfg_keys
+  assert not extra, f'Services in refresh-all.ts ENABLED not in config.json: {extra}'
+
+  mismatches = {
+    k for k in cfg_keys
+    if ts_flags[k] != (not cfg_services[k].disabled)
+  }
+  assert not mismatches, (
+    f'ENABLED flags in refresh-all.ts do not match config.json disabled for: {mismatches}\n'
+    + '\n'.join(f'  {k}: ts={ts_flags[k]} config_disabled={cfg_services[k].disabled}' for k in sorted(mismatches))
+  )
+
+
 def test_install_chrome(chrome_installed):
   """npm run install-browsers puts Chromium binaries in the expected location."""