import { describe, expect, it } from 'vitest';
import { rowsForDataBinding } from './spec-inline-data';

describe('rowsForDataBinding', () => {
  it('reads inline values from the given binding', () => {
    const spec = { data: { values: [{ a: 1 }, { a: 2 }] } };
    expect(rowsForDataBinding(spec, spec.data)).toEqual([{ a: 1 }, { a: 2 }]);
  });

  it('resolves a self-defined named binding against top-level datasets', () => {
    const spec = { datasets: { ds: [{ c: 1 }, { c: 2 }] }, data: { name: 'ds' } };
    expect(rowsForDataBinding(spec, { name: 'ds' })).toEqual([{ c: 1 }, { c: 2 }]);
  });

  it('returns null for a library reference, url, generator, or CSV-string values', () => {
    const spec = { datasets: { ds: [{ c: 1 }] } };
    expect(rowsForDataBinding(spec, { name: 'LibraryRef' })).toBeNull(); // not self-defined
    expect(rowsForDataBinding(spec, { url: 'x.csv' })).toBeNull();
    expect(rowsForDataBinding(spec, { sequence: { start: 0, stop: 5 } })).toBeNull();
    expect(rowsForDataBinding(spec, { values: 'a,b\n1,2' })).toBeNull();
    expect(rowsForDataBinding({ mark: 'bar' }, { values: [] })).toBeNull(); // empty rows
  });
});
