|
1 | 1 | import {CodeGenerator} from '@babel/generator' |
2 | 2 | import * as t from '@babel/types' |
3 | | -import {type TypeNode} from 'groq-js' |
| 3 | +import {type ObjectTypeNode, type TypeNode} from 'groq-js' |
4 | 4 | import {describe, expect, test} from 'vitest' |
5 | 5 |
|
6 | 6 | import {SchemaTypeGenerator, walkAndCountQueryTypeNodeStats} from '../schemaTypeGenerator.js' |
| 7 | +import { |
| 8 | + buildDeduplicationRegistry, |
| 9 | + collectObjectFingerprints, |
| 10 | + fingerprintTypeNode, |
| 11 | +} from '../typeNodeFingerprint.js' |
7 | 12 |
|
8 | 13 | function generateCode(node: t.Node | undefined) { |
9 | 14 | if (!node) throw new Error('Node is undefined') |
@@ -574,6 +579,130 @@ describe(SchemaTypeGenerator.name, () => { |
574 | 579 | }) |
575 | 580 | }) |
576 | 581 |
|
| 582 | + describe('deduplication', () => { |
| 583 | + // eslint-disable-next-line unicorn/consistent-function-scoping |
| 584 | + function buildSchemaWithDuplicateInlineObjects() { |
| 585 | + // The inline image object has 2+ meaningful attributes (url, alt) beyond _type, |
| 586 | + // which meets the deduplication extraction threshold |
| 587 | + const imageAttributes = { |
| 588 | + _type: {type: 'objectAttribute' as const, value: {type: 'string' as const, value: 'image'}}, |
| 589 | + alt: {optional: true, type: 'objectAttribute' as const, value: {type: 'string' as const}}, |
| 590 | + url: {type: 'objectAttribute' as const, value: {type: 'string' as const}}, |
| 591 | + } |
| 592 | + |
| 593 | + return new SchemaTypeGenerator([ |
| 594 | + { |
| 595 | + attributes: { |
| 596 | + _id: {type: 'objectAttribute', value: {type: 'string'}}, |
| 597 | + _type: {type: 'objectAttribute', value: {type: 'string', value: 'post'}}, |
| 598 | + mainImage: { |
| 599 | + optional: true, |
| 600 | + type: 'objectAttribute', |
| 601 | + value: {attributes: imageAttributes, type: 'object'}, |
| 602 | + }, |
| 603 | + title: {optional: true, type: 'objectAttribute', value: {type: 'string'}}, |
| 604 | + }, |
| 605 | + name: 'post', |
| 606 | + type: 'document', |
| 607 | + }, |
| 608 | + { |
| 609 | + attributes: { |
| 610 | + _id: {type: 'objectAttribute', value: {type: 'string'}}, |
| 611 | + _type: {type: 'objectAttribute', value: {type: 'string', value: 'author'}}, |
| 612 | + avatar: { |
| 613 | + optional: true, |
| 614 | + type: 'objectAttribute', |
| 615 | + value: {attributes: imageAttributes, type: 'object'}, |
| 616 | + }, |
| 617 | + name: {optional: true, type: 'objectAttribute', value: {type: 'string'}}, |
| 618 | + }, |
| 619 | + name: 'author', |
| 620 | + type: 'document', |
| 621 | + }, |
| 622 | + ]) |
| 623 | + } |
| 624 | + |
| 625 | + test('setDeduplicationRegistry enables type references in object generation', () => { |
| 626 | + const gen = buildSchemaWithDuplicateInlineObjects() |
| 627 | + |
| 628 | + // Evaluate two queries that produce the same inline image object |
| 629 | + const {typeNode: tn1} = gen.evaluateQueryTypeNode({ |
| 630 | + query: '*[_type == "post"]{title, mainImage}', |
| 631 | + }) |
| 632 | + const {typeNode: tn2} = gen.evaluateQueryTypeNode({ |
| 633 | + query: '*[_type == "author"]{name, avatar}', |
| 634 | + }) |
| 635 | + |
| 636 | + // Build registry from collected type nodes |
| 637 | + const fingerprints = collectObjectFingerprints([tn1, tn2]) |
| 638 | + const registry = buildDeduplicationRegistry(fingerprints, new Set(['Author', 'Post'])) |
| 639 | + gen.setDeduplicationRegistry(registry) |
| 640 | + |
| 641 | + // After setting registry, generating TS type should reference the extracted type |
| 642 | + const tsType = gen.generateQueryTsType(tn1) |
| 643 | + const code = generateCode(tsType) |
| 644 | + |
| 645 | + // We should have exactly one extracted type (the image type) |
| 646 | + expect(registry.extractedTypes.size).toBe(1) |
| 647 | + |
| 648 | + // Verify the generated code contains a reference to an extracted type |
| 649 | + const extractedNames = [...registry.extractedTypes.values()].map((e) => e.id.name) |
| 650 | + const hasRef = extractedNames.some((name) => code.includes(name)) |
| 651 | + expect(extractedNames).includes('InlineImage') |
| 652 | + expect(hasRef).toBe(true) |
| 653 | + }) |
| 654 | + |
| 655 | + test('generateExtractedTypeTsType expands the object without self-referencing', () => { |
| 656 | + const gen = buildSchemaWithDuplicateInlineObjects() |
| 657 | + |
| 658 | + const imageObj: ObjectTypeNode = { |
| 659 | + attributes: { |
| 660 | + _type: {type: 'objectAttribute', value: {type: 'string', value: 'image'}}, |
| 661 | + alt: {optional: true, type: 'objectAttribute', value: {type: 'string'}}, |
| 662 | + url: {type: 'objectAttribute', value: {type: 'string'}}, |
| 663 | + }, |
| 664 | + type: 'object', |
| 665 | + } |
| 666 | + |
| 667 | + const fp = fingerprintTypeNode(imageObj) |
| 668 | + const fingerprints = new Map([[fp, {candidateName: 'image', count: 2, typeNode: imageObj}]]) |
| 669 | + const registry = buildDeduplicationRegistry(fingerprints, new Set()) |
| 670 | + gen.setDeduplicationRegistry(registry) |
| 671 | + |
| 672 | + // Generate the extracted type body — it should NOT produce a self-reference |
| 673 | + const imageTypeBody = gen.generateExtractedTypeTsType(imageObj, fp) |
| 674 | + |
| 675 | + expect(generateCode(imageTypeBody)).toMatchInlineSnapshot(String.raw` |
| 676 | + "{ |
| 677 | + _type: "image"; |
| 678 | + alt?: string; |
| 679 | + url: string; |
| 680 | + }" |
| 681 | + `) |
| 682 | + }) |
| 683 | + |
| 684 | + test('snapshot of dedup output for queries with shared inline objects', () => { |
| 685 | + const gen = buildSchemaWithDuplicateInlineObjects() |
| 686 | + |
| 687 | + const {typeNode: tn1} = gen.evaluateQueryTypeNode({ |
| 688 | + query: '*[_type == "post"]{title, mainImage}', |
| 689 | + }) |
| 690 | + const {typeNode: tn2} = gen.evaluateQueryTypeNode({ |
| 691 | + query: '*[_type == "author"]{name, avatar}', |
| 692 | + }) |
| 693 | + |
| 694 | + const fingerprints = collectObjectFingerprints([tn1, tn2]) |
| 695 | + const registry = buildDeduplicationRegistry(fingerprints, new Set(['Author', 'Post'])) |
| 696 | + gen.setDeduplicationRegistry(registry) |
| 697 | + |
| 698 | + const code1 = generateCode(gen.generateQueryTsType(tn1)) |
| 699 | + const code2 = generateCode(gen.generateQueryTsType(tn2)) |
| 700 | + |
| 701 | + // Both queries should reference the same extracted type for image |
| 702 | + expect({query1: code1, query2: code2}).toMatchSnapshot() |
| 703 | + }) |
| 704 | + }) |
| 705 | + |
577 | 706 | describe('walkAndCountQueryTypeNodeStats', () => { |
578 | 707 | test('counts unknown type', () => { |
579 | 708 | const node: TypeNode = {type: 'unknown'} |
|
0 commit comments