/**
 * 使用者功能冒煙測試
 * 
 * 此測試用於 Dev/QA 環境，快速驗證使用者功能是否正常運作。
 * 測試重點：基本功能可用性，不關注性能。
 */

import { registerWithCredentials, loginWithCredentials } from '../../scenarios/apis/auth.js';
import { getUserInfo, updateUserInfo } from '../../scenarios/apis/user.js';

export const options = {
  scenarios: {
    smoke_user: {
      executor: 'shared-iterations',
      vus: 1,
      iterations: 1,
      maxDuration: '30s',
      tags: { test_type: 'smoke', api: 'user' },
    },
  },
  thresholds: {
    checks: ['rate==1.0'], // 所有檢查必須通過
    http_req_duration: ['p(95)<2000'], // 95% 的請求應在 2 秒內完成
  },
};

export default function () {
  const baseUrl = __ENV.BASE_URL || 'https://localhost:8888';
  const timestamp = Date.now();
  const loginId = `smoke_user_${timestamp}@example.com`;
  const password = 'SmokeTest123!';

  // 1. 註冊並登入以獲取 Token
  const registerResult = registerWithCredentials({
    baseUrl,
    loginId,
    password,
  });

  if (!registerResult.success || !registerResult.tokens) {
    console.error('Smoke test failed: Registration failed');
    return;
  }

  const accessToken = registerResult.tokens.accessToken;

  // 2. 取得使用者資訊
  const getInfoResult = getUserInfo({
    baseUrl,
    accessToken,
  });

  if (!getInfoResult.success) {
    console.error('Smoke test failed: Get user info failed');
    return;
  }

  // 3. 更新使用者資訊
  const updateInfoResult = updateUserInfo({
    baseUrl,
    accessToken,
    updateData: {
      nickname: `SmokeTest_${timestamp}`,
      preferred_language: 'zh-tw',
    },
  });

  if (!updateInfoResult.success) {
    console.error('Smoke test failed: Update user info failed');
    return;
  }

  console.log('Smoke test passed: All user operations succeeded');
}

