import { describe, it, expect } from 'vitest';
import { Readable } from 'node:stream';
import { readCredentialsFromPipedStdin } from './piped-credentials.js';

describe('readCredentialsFromPipedStdin — #1394 stdin dual path (real streams)', () => {
  it('reads exactly two lines; email trimmed, password as-is', async () => {
    const r = await readCredentialsFromPipedStdin(
      Readable.from(['  a@b.c  \n', 'pw with spaces \n']),
    );
    expect(r.email).toBe('a@b.c');
    expect(r.password).toBe('pw with spaces ');
  });

  it('empty stdin → nulls (the headless-no-credentials shape)', async () => {
    const r = await readCredentialsFromPipedStdin(Readable.from(['']));
    expect(r).toEqual({ email: null, password: null });
  });

  it('single line only → email set, password null', async () => {
    const r = await readCredentialsFromPipedStdin(Readable.from(['only-email\n']));
    expect(r.email).toBe('only-email');
    expect(r.password).toBeNull();
  });

  it('stops after two lines even if more follow', async () => {
    const r = await readCredentialsFromPipedStdin(
      Readable.from(['e@b.c\n', 'pw\n', 'extra\n', 'more\n']),
    );
    expect(r).toEqual({ email: 'e@b.c', password: 'pw' });
  });
});
