import { describe, it } from 'node:test';
import { expect } from 'chai';
import { dedent } from '../../__testUtils__/dedent.ts';
import { printSourceLocation } from '../printLocation.ts';
import { Source } from '../source.ts';
describe('printSourceLocation', () => {
it('prints minified documents', () => {
const minifiedSource = new Source(
'query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String){someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD...on THIRD_ERROR_HERE}}}',
);
const firstLocation = printSourceLocation(minifiedSource, {
line: 1,
column: minifiedSource.body.indexOf('FIRST_ERROR_HERE') + 1,
});
expect(firstLocation).to.equal(dedent`
GraphQL request:1:53
1 | query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
| ^
| {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
`);
const secondLocation = printSourceLocation(minifiedSource, {
line: 1,
column: minifiedSource.body.indexOf('SECOND_ERROR_HERE') + 1,
});
expect(secondLocation).to.equal(dedent`
GraphQL request:1:114
1 | query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
| {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
| ^
| ..on THIRD_ERROR_HERE}}}
`);
const thirdLocation = printSourceLocation(minifiedSource, {
line: 1,
column: minifiedSource.body.indexOf('THIRD_ERROR_HERE') + 1,
});
expect(thirdLocation).to.equal(dedent`
GraphQL request:1:166
1 | query SomeMinifiedQueryWithErrorInside($foo:String!=FIRST_ERROR_HERE$bar:String)
| {someField(foo:$foo bar:$bar baz:SECOND_ERROR_HERE){fieldA fieldB{fieldC fieldD.
| ..on THIRD_ERROR_HERE}}}
| ^
`);
});
it('prints minified locations at 80-character boundaries', () => {
const source = new Source('a'.repeat(80) + 'b'.repeat(80) + 'c'.repeat(40));
for (const [column, nextLine] of [
[80, 'b'.repeat(80)],
[160, 'c'.repeat(40)],
] as const) {
const result = printSourceLocation(source, { line: 1, column });
const lines = result.split('\n');
expect(lines.at(-2)).to.equal(` | ${' '.repeat(79)}^`);
expect(lines.at(-1)).to.equal(` | ${nextLine}`);
}
});
it('prints minified locations just after 80-character boundaries', () => {
const source = new Source(
'a'.repeat(80) + 'b'.repeat(80) + 'c'.repeat(80) + 'd'.repeat(40),
);
for (const [column, previousLine] of [
[81, 'b'.repeat(80)],
[161, 'c'.repeat(80)],
] as const) {
const result = printSourceLocation(source, { line: 1, column });
const lines = result.split('\n');
expect(lines.at(-3)).to.equal(` | ${previousLine}`);
expect(lines.at(-2)).to.equal(' | ^');
}
});
it('prints EOF after the final minified chunk', () => {
const source = new Source('a'.repeat(80) + 'b'.repeat(80));
const result = printSourceLocation(source, { line: 1, column: 161 });
expect(result).to.equal(
[
'GraphQL request:1:161',
`1 | ${'a'.repeat(80)}`,
` | ${'b'.repeat(80)}`,
` | ${' '.repeat(80)}^`,
].join('\n'),
);
});
it('prints single digit line number with no padding', () => {
const result = printSourceLocation(
new Source('*', 'Test', { line: 9, column: 1 }),
{ line: 1, column: 1 },
);
expect(result).to.equal(dedent`
Test:9:1
9 | *
| ^
`);
});
it('prints line numbers with correct padding', () => {
const result = printSourceLocation(
new Source('*\n', 'Test', { line: 9, column: 1 }),
{ line: 1, column: 1 },
);
expect(result).to.equal(dedent`
Test:9:1
9 | *
| ^
10 |
`);
});
});