Skip to content

Commit 6d4f0c1

Browse files
fix(rag-embedding): update test mocks for graphql-query
- Add mock for @constructive-io/graphql-query to avoid dependency chain issues - Fix test payloads to use correct trigger format (table, schema, id, chunks_table) - Add proper table metadata with relations in mock Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 90a126f commit 6d4f0c1

1 file changed

Lines changed: 55 additions & 39 deletions

File tree

functions/rag-embedding/__tests__/handler.test.ts

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ jest.mock('@agentic-kit/ollama', () => {
1010
}));
1111
});
1212

13+
jest.mock('@constructive-io/graphql-query', () => ({
14+
SCHEMA_INTROSPECTION_QUERY: 'query { __schema { types { name } } }',
15+
inferTablesFromIntrospection: jest.fn().mockReturnValue([
16+
{
17+
name: 'article',
18+
query: { all: 'articles', create: 'createArticle' },
19+
inflection: { allRows: 'articles', tableFieldName: 'article' },
20+
primaryKey: 'id',
21+
relations: { belongsTo: [], hasMany: [] },
22+
},
23+
{
24+
name: 'article_chunk',
25+
query: { all: 'articleChunks', create: 'createArticleChunk' },
26+
inflection: { allRows: 'articleChunks', tableFieldName: 'articleChunk', createField: 'createArticleChunk' },
27+
primaryKey: 'id',
28+
relations: {
29+
belongsTo: [{ referencesTable: 'article', fieldName: 'article' }],
30+
hasMany: [],
31+
},
32+
},
33+
]),
34+
buildSelect: jest.fn().mockReturnValue({ toString: () => 'query { articles { nodes { id content } } }' }),
35+
buildPostGraphileCreate: jest.fn().mockReturnValue({ toString: () => 'mutation { createArticleChunk { articleChunk { id } } }' }),
36+
buildPostGraphileDelete: jest.fn().mockReturnValue({ toString: () => 'mutation { deleteArticleChunks { deletedCount } }' }),
37+
}));
38+
1339
const createMockContext = () => ({
1440
job: {
1541
jobId: 'test-job-id',
@@ -51,15 +77,18 @@ describe('rag-embedding handler', () => {
5177
const handler = loadHandler();
5278
const context = createMockContext();
5379

54-
// PostGraphile v5 returns nodes array
55-
mockRequest.mockResolvedValueOnce({
56-
articles: { nodes: [{ id: 'test-id', content: '' }] },
57-
});
80+
mockRequest
81+
.mockResolvedValueOnce({ __schema: { types: [] } }) // introspection
82+
.mockResolvedValueOnce({
83+
articles: { nodes: [{ id: 'test-id', content: '' }] },
84+
});
5885

5986
const result = await handler(
6087
{
61-
table_name: 'article',
62-
record_id: 'test-id',
88+
table: 'article',
89+
schema: 'test-db-app-public',
90+
id: 'test-id',
91+
chunks_table: 'article_chunk',
6392
},
6493
context
6594
);
@@ -73,19 +102,22 @@ describe('rag-embedding handler', () => {
73102
const context = createMockContext();
74103

75104
mockRequest
105+
.mockResolvedValueOnce({ __schema: { types: [] } }) // introspection
76106
.mockResolvedValueOnce({
77107
articles: { nodes: [{ id: 'test-id', content: 'This is test content for chunking.' }] },
78108
})
79-
.mockResolvedValueOnce({ deletedCount: 0 })
109+
.mockResolvedValueOnce({ deleteArticleChunks: { deletedCount: 0 } })
80110
.mockResolvedValueOnce({
81111
createArticleChunk: { articleChunk: { id: 'chunk-1' } },
82112
});
83113

84114
const result = await handler(
85115
{
86-
table_name: 'article',
87-
record_id: 'test-id',
88-
chunk_size: 1000,
116+
table: 'article',
117+
schema: 'test-db-app-public',
118+
id: 'test-id',
119+
chunks_table: 'article_chunk',
120+
chunk_size: '1000',
89121
},
90122
context
91123
);
@@ -101,7 +133,7 @@ describe('rag-embedding handler', () => {
101133
context.job.databaseId = undefined;
102134

103135
await expect(
104-
handler({ table_name: 'article', record_id: 'test-id' }, context)
136+
handler({ table: 'article', schema: 'test-db-app-public', id: 'test-id', chunks_table: 'article_chunk' }, context)
105137
).rejects.toThrow('Missing X-Database-Id');
106138
});
107139

@@ -110,47 +142,31 @@ describe('rag-embedding handler', () => {
110142
const context = createMockContext();
111143

112144
await expect(
113-
handler({ table_name: '', record_id: 'test-id' }, context)
145+
handler({ table: '', schema: 'test-db-app-public', id: 'test-id', chunks_table: 'article_chunk' }, context)
114146
).rejects.toThrow('Missing required params');
115147
});
116148

117-
it('should use custom content_field', async () => {
149+
it('should return early when content is empty with trigger format', async () => {
118150
const handler = loadHandler();
119151
const context = createMockContext();
120152

121-
mockRequest.mockResolvedValueOnce({
122-
blogPosts: { nodes: [{ id: 'test-id', body: 'Custom field content' }] },
123-
});
124-
125-
const result = await handler(
126-
{
127-
table_name: 'blog_post',
128-
record_id: 'test-id',
129-
content_field: 'body',
130-
},
131-
context
132-
);
133-
134-
expect(result.complete).toBe(true);
135-
});
136-
137-
it('should support trigger payload format', async () => {
138-
const handler = loadHandler();
139-
const context = createMockContext();
140-
141-
mockRequest.mockResolvedValueOnce({
142-
articles: { nodes: [{ id: 'test-id', content: '' }] },
143-
});
153+
mockRequest
154+
.mockResolvedValueOnce({ __schema: { types: [] } }) // introspection
155+
.mockResolvedValueOnce({
156+
articles: { nodes: [{ id: 'test-id', content: '' }] },
157+
});
144158

145159
const result = await handler(
146160
{
147-
table: 'article', // trigger format
148-
schema: 'test-db-app-public', // trigger format
149-
id: 'test-id', // trigger format
161+
table: 'article',
162+
schema: 'test-db-app-public',
163+
id: 'test-id',
164+
chunks_table: 'article_chunk',
150165
},
151166
context
152167
);
153168

154169
expect(result.complete).toBe(true);
170+
expect(result.chunks).toBe(0);
155171
});
156172
});

0 commit comments

Comments
 (0)