#!/usr/bin/env npx tsx
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const browserLogin_1 = require("../../../src/auth/browserLogin");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
async function testBasicBrowserLogin() {
    console.log('\n=== Basic Browser Login Test ===\n');
    let success = false;
    const configDir = path.join(process.cwd(), '.knowhow');
    const jwtFile = path.join(configDir, '.jwt');
    if (fs.existsSync(jwtFile)) {
        fs.unlinkSync(jwtFile);
        console.log('🧹 Cleaned up existing JWT file');
    }
    try {
        console.log('1. Initializing BrowserLoginService...');
        const browserLogin = new browserLogin_1.BrowserLoginService();
        console.log('2. Starting browser login flow...');
        console.log('   Note: This will open your browser and require manual authentication');
        console.log('   Please complete the authentication process in your browser');
        await browserLogin.login();
        console.log('3. Verifying JWT file was created...');
        if (fs.existsSync(jwtFile)) {
            const jwtContent = fs.readFileSync(jwtFile, 'utf8');
            const stats = fs.statSync(jwtFile);
            const permissions = stats.mode & parseInt('777', 8);
            console.log(`   ✅ JWT file created: ${jwtFile}`);
            console.log(`   ✅ JWT length: ${jwtContent.length} characters`);
            console.log(`   ✅ File permissions: ${permissions.toString(8)} (should be 600)`);
            const parts = jwtContent.split('.');
            if (parts.length === 3) {
                console.log('   ✅ JWT has correct structure (3 parts)');
                success = true;
            }
            else {
                console.log('   ❌ JWT has incorrect structure');
            }
            if (permissions === parseInt('600', 8)) {
                console.log('   ✅ JWT file has correct permissions (600)');
            }
            else {
                console.log(`   ⚠️  JWT file permissions may be incorrect: ${permissions.toString(8)}`);
            }
        }
        else {
            console.log('   ❌ JWT file was not created');
        }
    }
    catch (error) {
        console.error('❌ Test failed with error:', error.message);
        if (error.code === 'USER_CANCELLED') {
            console.log('   ℹ️  Authentication was cancelled by user (this is expected for Ctrl+C)');
        }
        else if (error.code === 'TIMEOUT') {
            console.log('   ⚠️  Authentication timed out (user may not have completed authentication)');
        }
        else if (error.code === 'NETWORK_ERROR') {
            console.log('   ❌ Network error - check API connectivity');
        }
    }
    console.log('\n=== Test Results ===');
    if (success) {
        console.log('✅ Browser login test PASSED');
        console.log('   - Session created successfully');
        console.log('   - Browser opened for authentication');
        console.log('   - JWT retrieved and stored securely');
        process.exit(0);
    }
    else {
        console.log('❌ Browser login test FAILED');
        console.log('   See error details above');
        process.exit(1);
    }
}
process.on('SIGINT', () => {
    console.log('\n\n🛑 Test interrupted by user (Ctrl+C)');
    console.log('   This tests the graceful cancellation feature');
    process.exit(0);
});
testBasicBrowserLogin().catch((error) => {
    console.error('Unhandled error:', error);
    process.exit(1);
});
//# sourceMappingURL=test_browser_login_basic.js.map