|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { run } from '../src/cli'; |
| 5 | + |
| 6 | +describe('fn init', () => { |
| 7 | + let tmpRoot: string; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'fn-init-')); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + fs.rmSync(tmpRoot, { recursive: true, force: true }); |
| 15 | + }); |
| 16 | + |
| 17 | + it('scaffolds a node-graphql handler from the bundled template', async () => { |
| 18 | + const code = await run(['init', 'send-welcome', '--no-tty', '--root', tmpRoot]); |
| 19 | + expect(code).toBe(0); |
| 20 | + const handlerJson = path.join(tmpRoot, 'functions/send-welcome/handler.json'); |
| 21 | + const handlerTs = path.join(tmpRoot, 'functions/send-welcome/handler.ts'); |
| 22 | + expect(fs.existsSync(handlerJson)).toBe(true); |
| 23 | + expect(fs.existsSync(handlerTs)).toBe(true); |
| 24 | + const json = JSON.parse(fs.readFileSync(handlerJson, 'utf-8')); |
| 25 | + expect(json.name).toBe('send-welcome'); |
| 26 | + expect(json.type).toBe('node-graphql'); |
| 27 | + expect(json.version).toBe('0.1.0'); |
| 28 | + const ts = fs.readFileSync(handlerTs, 'utf-8'); |
| 29 | + expect(ts).toContain("ctx.log.info('send-welcome invoked'"); |
| 30 | + expect(ts).toContain("import type { FunctionHandler } from '@constructive-io/fn-runtime'"); |
| 31 | + }); |
| 32 | + |
| 33 | + it('scaffolds a python handler when --type=python', async () => { |
| 34 | + const code = await run([ |
| 35 | + 'init', |
| 36 | + 'pyfn', |
| 37 | + '--type', |
| 38 | + 'python', |
| 39 | + '--no-tty', |
| 40 | + '--root', |
| 41 | + tmpRoot, |
| 42 | + ]); |
| 43 | + expect(code).toBe(0); |
| 44 | + expect(fs.existsSync(path.join(tmpRoot, 'functions/pyfn/handler.py'))).toBe(true); |
| 45 | + const json = JSON.parse( |
| 46 | + fs.readFileSync(path.join(tmpRoot, 'functions/pyfn/handler.json'), 'utf-8') |
| 47 | + ); |
| 48 | + expect(json.type).toBe('python'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('refuses to overwrite without --force', async () => { |
| 52 | + await run(['init', 'dup', '--no-tty', '--root', tmpRoot]); |
| 53 | + const code = await run(['init', 'dup', '--no-tty', '--root', tmpRoot]); |
| 54 | + expect(code).toBe(1); |
| 55 | + }); |
| 56 | + |
| 57 | + it('overwrites with --force', async () => { |
| 58 | + await run(['init', 'dup', '--no-tty', '--root', tmpRoot]); |
| 59 | + const code = await run([ |
| 60 | + 'init', |
| 61 | + 'dup', |
| 62 | + '--no-tty', |
| 63 | + '--force', |
| 64 | + '--description', |
| 65 | + 'second', |
| 66 | + '--root', |
| 67 | + tmpRoot, |
| 68 | + ]); |
| 69 | + expect(code).toBe(0); |
| 70 | + const json = JSON.parse( |
| 71 | + fs.readFileSync(path.join(tmpRoot, 'functions/dup/handler.json'), 'utf-8') |
| 72 | + ); |
| 73 | + expect(json.description).toBe('second'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('rejects an unknown --type', async () => { |
| 77 | + const code = await run([ |
| 78 | + 'init', |
| 79 | + 'broken', |
| 80 | + '--type', |
| 81 | + 'rust', |
| 82 | + '--no-tty', |
| 83 | + '--root', |
| 84 | + tmpRoot, |
| 85 | + ]); |
| 86 | + expect(code).toBe(1); |
| 87 | + expect(fs.existsSync(path.join(tmpRoot, 'functions/broken'))).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it('errors when no name is given', async () => { |
| 91 | + const code = await run(['init', '--no-tty', '--root', tmpRoot]); |
| 92 | + expect(code).toBe(1); |
| 93 | + }); |
| 94 | + |
| 95 | + it('honors a custom functionsDir from fn.config.json', async () => { |
| 96 | + fs.writeFileSync( |
| 97 | + path.join(tmpRoot, 'fn.config.json'), |
| 98 | + JSON.stringify({ functionsDir: 'src/handlers', outputDir: 'generated' }) |
| 99 | + ); |
| 100 | + const code = await run(['init', 'cfg', '--no-tty', '--root', tmpRoot]); |
| 101 | + expect(code).toBe(0); |
| 102 | + expect( |
| 103 | + fs.existsSync(path.join(tmpRoot, 'src/handlers/cfg/handler.json')) |
| 104 | + ).toBe(true); |
| 105 | + }); |
| 106 | +}); |
0 commit comments