import assert from 'assert';
import path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const { createSessionActionMethods } = await import(
    pathToFileURL(path.join(__dirname, '..', '..', 'web-ui', 'modules', 'app.methods.session-actions.mjs'))
);

test('isResumeCommandAvailable supports codex and codebuddy with sessionId', () => {
    const methods = createSessionActionMethods();
    assert.strictEqual(methods.isResumeCommandAvailable({ source: 'codex', sessionId: 'sess-1' }), true);
    assert.strictEqual(methods.isResumeCommandAvailable({ source: 'codebuddy', sessionId: 'abc123' }), true);
    assert.strictEqual(methods.isResumeCommandAvailable({ source: 'gemini', sessionId: 'gm-123' }), true);
    assert.strictEqual(methods.isResumeCommandAvailable({ source: 'gemini', sessionId: '', filePath: '/home/user/.gemini/tmp/abc/chats/gm-456.json' }), true);
    assert.strictEqual(methods.isResumeCommandAvailable({ source: 'codebuddy', sessionId: '' }), false);
    assert.strictEqual(methods.isResumeCommandAvailable({ source: 'claude', sessionId: 'sess-2' }), true);
    assert.strictEqual(methods.isResumeCommandAvailable({ source: 'claude', sessionId: '', filePath: '/home/user/.claude/projects/demo/sess-3.jsonl' }), true);
});

test('getResumeCommandTitle returns copy resume label', () => {
    const methods = createSessionActionMethods();
    assert.strictEqual(
        methods.getResumeCommandTitle.call(methods, { source: 'claude', sessionId: 'sess-2' }),
        'Copy resume command'
    );
});

test('buildResumeCommand generates codex --yolo resume, codebuddy -r, gemini -r, and claude --dangerously-skip-permissions -r', () => {
    const methods = createSessionActionMethods();
    const ctx = { ...methods, quoteShellArg: methods.quoteShellArg };

    assert.strictEqual(
        methods.buildResumeCommand.call(ctx, { source: 'codex', sessionId: 'sess-1' }),
        'codex --yolo resume sess-1'
    );

    assert.strictEqual(
        methods.buildResumeCommand.call(ctx, { source: 'codebuddy', sessionId: 'abc123' }),
        'codebuddy -r abc123'
    );

    assert.strictEqual(
        methods.buildResumeCommand.call(ctx, { source: 'gemini', sessionId: 'gm-123' }),
        'gemini -r gm-123'
    );

    assert.strictEqual(
        methods.buildResumeCommand.call(ctx, { source: 'claude', sessionId: 'sess-2' }),
        'claude --dangerously-skip-permissions -r sess-2'
    );

    assert.strictEqual(
        methods.buildResumeCommand.call(ctx, { source: 'claude', sessionId: '', filePath: '/home/user/.claude/projects/demo/sess-3.jsonl' }),
        'claude --dangerously-skip-permissions -r sess-3'
    );

    assert.strictEqual(
        methods.buildResumeCommand.call(ctx, { source: 'gemini', sessionId: '', filePath: '/home/user/.gemini/tmp/abc/chats/gm-456.json' }),
        'gemini -r gm-456'
    );
});
