Newer
Older
TelosDB / tests / e2e / specs / settings-autostart.spec.js
/**
 * 設定パネル「ログイン時自動起動」の E2E テスト(計画 auto_start に基づく)。
 * テストファースト: 表示・トグル・保存・永続化を検証する。
 *
 * 実行: npm run test:e2e
 * このスペックのみ: npx wdio run wdio.conf.js --spec tests/e2e/specs/settings-autostart.spec.js
 */

import { waitForAppReady } from '../helpers/wait-for-app.js';
import { resetRunOnLoginSetting } from '../helpers/e2e-cleanup.mjs';

const API_BASE = 'http://127.0.0.1:3001';

const openSettingsPanel = async () => {
  const navSettings = await $('button[data-panel="settings"]');
  await navSettings.click();
  const panel = await $('#panel-settings');
  await panel.waitForDisplayed({ timeout: 5000 });
  await expect(panel).not.toHaveElementClass('hidden');
};

describe('設定パネル ログイン時自動起動', () => {
  before(async function () {
    this.timeout(120000);
    await waitForAppReady(browser);
    await resetRunOnLoginSetting(API_BASE);
  });

  after(async () => {
    await resetRunOnLoginSetting(API_BASE);
  });

  it('設定パネルに「ログイン時自動起動」の枠とチェックボックスが表示される', async () => {
    await openSettingsPanel();
    const legends = await $$('legend');
    const autostartLegend = await Promise.all(Array.from(legends).map(async (el) => ({ el, text: await el.getText() }))).then((arr) => arr.find((x) => x.text === 'ログイン時自動起動'));
    expect(autostartLegend).toBeDefined();
    await expect(autostartLegend.el).toBeDisplayed();
    const toggleLabel = await $('label[for="setting-run-on-login"]');
    await expect(toggleLabel).toBeDisplayed();
    const checkbox = await $('#setting-run-on-login');
    expect(await checkbox.isExisting()).toBe(true);
  });

  it('ログイン時自動起動をオンにして保存すると「保存しました」が表示される', async () => {
    await openSettingsPanel();
    const toggleLabel = await $('label[for="setting-run-on-login"]');
    const checkbox = await $('#setting-run-on-login');
    const saveBtn = await $('#settings-save-btn');
    await expect(toggleLabel).toBeDisplayed();
    await expect(saveBtn).toBeDisplayed();
    if (!(await checkbox.isSelected())) await toggleLabel.click();
    await saveBtn.click();
    const feedback = await $('#settings-feedback');
    await expect(feedback).toHaveText('保存しました');
  });

  it('オンで保存後、いったん検索パネルに切り替えてから設定を再表示するとトグルがオンのままである', async () => {
    await openSettingsPanel();
    const toggleLabel = await $('label[for="setting-run-on-login"]');
    const checkbox = await $('#setting-run-on-login');
    if (!(await checkbox.isSelected())) await toggleLabel.click();
    await (await $('#settings-save-btn')).click();
    await browser.pause(500);
    await (await $('button[data-panel="search"]')).click();
    await browser.pause(300);
    await openSettingsPanel();
    const checkboxAgain = await $('#setting-run-on-login');
    await (await $('label[for="setting-run-on-login"]')).waitForDisplayed({ timeout: 5000 });
    await expect(checkboxAgain).toBeSelected();
  });

  it.skip('オフで保存後、いったん検索パネルに切り替えてから設定を再表示するとトグルがオフのままである', async () => {
    await openSettingsPanel();
    const checkbox = await $('#setting-run-on-login');
    if (await checkbox.isSelected()) {
      await browser.execute((id) => {
        const el = document.getElementById(id);
        if (el) {
          el.checked = false;
          el.dispatchEvent(new Event('change', { bubbles: true }));
        }
      }, 'setting-run-on-login');
    }
    await (await $('#settings-save-btn')).click();
    const feedback = await $('#settings-feedback');
    await browser.waitUntil(async () => (await feedback.getText()).includes('保存しました'), { timeout: 5000 });
    await browser.pause(800);
    await (await $('button[data-panel="search"]')).click();
    await browser.pause(300);
    await openSettingsPanel();
    const checkboxAgain = await $('#setting-run-on-login');
    await (await $('label[for="setting-run-on-login"]')).waitForDisplayed({ timeout: 5000 });
    await expect(checkboxAgain).not.toBeSelected();
  });
});