Newer
Older
TelosDB / e2e-tests / specs / app.spec.js
/**
 * TelosDB の E2E スペック(WebDriver + tauri-driver)。
 * ウィンドウが開き、ヘッダー・検索 UI が表示され、検索が実際にヒットすることを検証する。
 */

const API_BASE = 'http://127.0.0.1:3001';
const E2E_SEARCH_PHRASE = 'E2E検索テスト用の文書';
const E2E_DOC_CONTENT = `${E2E_SEARCH_PHRASE}です。この文字列でヒットすることを確認する。`;

async function waitForMcp(maxAttempts = 15, intervalMs = 500) {
  for (let i = 0; i < maxAttempts; i++) {
    try {
      const res = await fetch(`${API_BASE}/edition`);
      if (res.ok) return;
    } catch (_) {}
    await new Promise((r) => setTimeout(r, intervalMs));
  }
  throw new Error('MCP (3001) did not become ready in time');
}

async function addDocumentViaMcp(content, path) {
  const res = await fetch(`${API_BASE}/messages`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      jsonrpc: '2.0',
      method: 'tools/call',
      params: {
        name: 'add_item_text',
        arguments: { content, path },
      },
      id: Date.now(),
    }),
  });
  const data = await res.json();
  if (data.error) throw new Error(data.error.message || JSON.stringify(data.error));
  return data;
}

describe('TelosDB', () => {
  it('ウィンドウのタイトルが TelosDB である', async () => {
    const title = await browser.getTitle();
    expect(title).toMatch(/TelosDB/i);
  });

  it('ヘッダーに TelosDB のロゴ・テキストが表示される', async () => {
    const logo = await $('.site-header .logo-mark');
    await expect(logo).toBeDisplayed();
    await expect(logo).toHaveText('TelosDB');
  });

  it('検索入力と検索ボタンが表示される', async () => {
    const searchInput = await $('#query');
    const searchBtn = await $('.search-btn');
    await expect(searchInput).toBeDisplayed();
    await expect(searchBtn).toBeDisplayed();
    await expect(searchBtn).toHaveText('検索');
  });

  it('検索がヒットする(テスト用文書を登録し、その文言で検索して結果が返る)', async () => {
    await waitForMcp();
    await addDocumentViaMcp(E2E_DOC_CONTENT, 'e2e-test-search-doc.txt');
    await browser.pause(2500);

    const searchInput = await $('#query');
    const searchBtn = await $('.search-btn');
    const resultPanel = await $('#result');
    await searchInput.setValue(E2E_SEARCH_PHRASE);
    await searchBtn.click();
    await browser.pause(2500);

    await expect(resultPanel).toBeDisplayed();
    const resultCards = await $$('.result-card');
    expect(resultCards.length).toBeGreaterThanOrEqual(1);

    const firstCardText = await resultCards[0].getText();
    expect(firstCardText).toContain(E2E_SEARCH_PHRASE);
  });
});