schema-pull.test.ts56 lines · main
| 1 | import { test } from 'node:test'; |
| 2 | import { strict as assert } from 'node:assert'; |
| 3 | import { mkdtemp, readFile, rm } from 'node:fs/promises'; |
| 4 | import { tmpdir } from 'node:os'; |
| 5 | import { join } from 'node:path'; |
| 6 | import { createServer } from 'node:http'; |
| 7 | import { pullSchemaToDisk } from './schema-pull.js'; |
| 8 | |
| 9 | test('pullSchemaToDisk writes schema.ts from api response', async () => { |
| 10 | const srv = createServer((_req, res) => { |
| 11 | res.writeHead(200, { 'content-type': 'application/json' }); |
| 12 | res.end(JSON.stringify({ schemaTs: "export default schema({});\n" })); |
| 13 | }); |
| 14 | await new Promise<void>((r) => srv.listen(0, '127.0.0.1', () => r())); |
| 15 | const port = (srv.address() as { port: number }).port; |
| 16 | const dir = await mkdtemp(join(tmpdir(), 'briven-pull-')); |
| 17 | try { |
| 18 | await pullSchemaToDisk({ |
| 19 | apiOrigin: `http://127.0.0.1:${port}`, |
| 20 | bearer: 'tk', |
| 21 | projectId: 'p_test', |
| 22 | cwd: dir, |
| 23 | }); |
| 24 | const written = await readFile(join(dir, 'briven', 'schema.ts'), 'utf8'); |
| 25 | assert.match(written, /export default schema/); |
| 26 | const readme = await readFile(join(dir, 'briven', 'functions', 'README.md'), 'utf8'); |
| 27 | assert.match(readme, /your functions live on briven/i); |
| 28 | } finally { |
| 29 | srv.close(); |
| 30 | await rm(dir, { recursive: true, force: true }); |
| 31 | } |
| 32 | }); |
| 33 | |
| 34 | test('pullSchemaToDisk propagates api errors', async () => { |
| 35 | const srv = createServer((_req, res) => { |
| 36 | res.writeHead(500, { 'content-type': 'application/json' }); |
| 37 | res.end(JSON.stringify({ code: 'internal', message: 'boom' })); |
| 38 | }); |
| 39 | await new Promise<void>((r) => srv.listen(0, '127.0.0.1', () => r())); |
| 40 | const port = (srv.address() as { port: number }).port; |
| 41 | const dir = await mkdtemp(join(tmpdir(), 'briven-pull-err-')); |
| 42 | try { |
| 43 | await assert.rejects( |
| 44 | () => |
| 45 | pullSchemaToDisk({ |
| 46 | apiOrigin: `http://127.0.0.1:${port}`, |
| 47 | bearer: 'tk', |
| 48 | projectId: 'p_test', |
| 49 | cwd: dir, |
| 50 | }), |
| 51 | ); |
| 52 | } finally { |
| 53 | srv.close(); |
| 54 | await rm(dir, { recursive: true, force: true }); |
| 55 | } |
| 56 | }); |