|
| 1 | +const test = require('node:test'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const { spawnSync } = require('child_process'); |
| 6 | + |
| 7 | +const { createTempProject, writeProjectFile } = require('./test-utils'); |
| 8 | + |
| 9 | +const VALIDATOR_PATH = path.join( |
| 10 | + __dirname, |
| 11 | + '..', |
| 12 | + '..', |
| 13 | + 'skills', |
| 14 | + 'integrate-serverlogic', |
| 15 | + 'scripts', |
| 16 | + 'validate-serverlogic.js' |
| 17 | +); |
| 18 | + |
| 19 | +function runValidator(projectRoot) { |
| 20 | + const input = JSON.stringify({ cwd: projectRoot }); |
| 21 | + return spawnSync(process.execPath, [VALIDATOR_PATH], { |
| 22 | + input, |
| 23 | + encoding: 'utf8', |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +function setupProject(projectRoot) { |
| 28 | + writeProjectFile(projectRoot, 'powerpages.config.json', '{}'); |
| 29 | +} |
| 30 | + |
| 31 | +function writeServerLogic(projectRoot, name, jsContent, ymlContent) { |
| 32 | + const dir = `.powerpages-site/server-logic/${name}`; |
| 33 | + writeProjectFile(projectRoot, `${dir}/${name}.js`, jsContent); |
| 34 | + if (ymlContent) { |
| 35 | + writeProjectFile(projectRoot, `${dir}/${name}.serverlogic.yml`, ymlContent); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +const VALID_YML = `adx_serverlogic_adx_webrole: |
| 40 | + - 11111111-1111-1111-1111-111111111111 |
| 41 | +description: Test endpoint |
| 42 | +display_name: Test |
| 43 | +id: 22222222-2222-2222-2222-222222222222 |
| 44 | +name: test-endpoint`; |
| 45 | + |
| 46 | +const VALID_JS = `function get() { |
| 47 | + try { |
| 48 | + Server.Logger.Log("test-endpoint GET called"); |
| 49 | + return JSON.stringify({ status: "success" }); |
| 50 | + } catch (err) { |
| 51 | + Server.Logger.Error("test-endpoint GET failed: " + err.message); |
| 52 | + return JSON.stringify({ status: "error", message: err.message }); |
| 53 | + } |
| 54 | +}`; |
| 55 | + |
| 56 | +test('valid server logic passes validation', (t) => { |
| 57 | + const projectRoot = createTempProject(t); |
| 58 | + setupProject(projectRoot); |
| 59 | + writeServerLogic(projectRoot, 'test-endpoint', VALID_JS, VALID_YML); |
| 60 | + |
| 61 | + const result = runValidator(projectRoot); |
| 62 | + assert.equal(result.status, 0, result.stderr); |
| 63 | +}); |
| 64 | + |
| 65 | +test('async function without await is flagged', (t) => { |
| 66 | + const projectRoot = createTempProject(t); |
| 67 | + setupProject(projectRoot); |
| 68 | + const asyncNoAwaitJs = `async function get() { |
| 69 | + try { |
| 70 | + Server.Logger.Log("test-endpoint GET called"); |
| 71 | + var records = Server.Connector.Dataverse.RetrieveMultipleRecords("contacts", "?$top=10"); |
| 72 | + return JSON.stringify({ status: "success", data: records }); |
| 73 | + } catch (err) { |
| 74 | + Server.Logger.Error("test-endpoint GET failed: " + err.message); |
| 75 | + return JSON.stringify({ status: "error", message: err.message }); |
| 76 | + } |
| 77 | +}`; |
| 78 | + writeServerLogic(projectRoot, 'test-endpoint', asyncNoAwaitJs, VALID_YML); |
| 79 | + |
| 80 | + const result = runValidator(projectRoot); |
| 81 | + assert.equal(result.status, 2); |
| 82 | + assert.match(result.stderr, /function 'get' is marked async but contains no await/); |
| 83 | +}); |
| 84 | + |
| 85 | +test('async function with await passes validation', (t) => { |
| 86 | + const projectRoot = createTempProject(t); |
| 87 | + setupProject(projectRoot); |
| 88 | + const asyncWithAwaitJs = `async function post() { |
| 89 | + try { |
| 90 | + Server.Logger.Log("test-endpoint POST called"); |
| 91 | + var response = await Server.Connector.HttpClient.PostAsync("https://api.example.com/data", JSON.stringify({ key: "value" })); |
| 92 | + return JSON.stringify({ status: "success", data: response }); |
| 93 | + } catch (err) { |
| 94 | + Server.Logger.Error("test-endpoint POST failed: " + err.message); |
| 95 | + return JSON.stringify({ status: "error", message: err.message }); |
| 96 | + } |
| 97 | +}`; |
| 98 | + writeServerLogic(projectRoot, 'test-endpoint', asyncWithAwaitJs, VALID_YML); |
| 99 | + |
| 100 | + const result = runValidator(projectRoot); |
| 101 | + assert.equal(result.status, 0, result.stderr); |
| 102 | +}); |
| 103 | + |
| 104 | +test('missing js file is flagged', (t) => { |
| 105 | + const projectRoot = createTempProject(t); |
| 106 | + setupProject(projectRoot); |
| 107 | + const dir = path.join(projectRoot, '.powerpages-site', 'server-logic', 'test-endpoint'); |
| 108 | + fs.mkdirSync(dir, { recursive: true }); |
| 109 | + writeProjectFile(projectRoot, '.powerpages-site/server-logic/test-endpoint/test-endpoint.serverlogic.yml', VALID_YML); |
| 110 | + |
| 111 | + const result = runValidator(projectRoot); |
| 112 | + assert.equal(result.status, 2); |
| 113 | + assert.match(result.stderr, /missing \.js file/); |
| 114 | +}); |
| 115 | + |
| 116 | +test('missing yml file is flagged', (t) => { |
| 117 | + const projectRoot = createTempProject(t); |
| 118 | + setupProject(projectRoot); |
| 119 | + writeServerLogic(projectRoot, 'test-endpoint', VALID_JS, null); |
| 120 | + |
| 121 | + const result = runValidator(projectRoot); |
| 122 | + assert.equal(result.status, 2); |
| 123 | + assert.match(result.stderr, /missing metadata file/); |
| 124 | +}); |
| 125 | + |
| 126 | +test('missing try/catch is flagged', (t) => { |
| 127 | + const projectRoot = createTempProject(t); |
| 128 | + setupProject(projectRoot); |
| 129 | + const noTryCatchJs = `function get() { |
| 130 | + Server.Logger.Log("test-endpoint GET called"); |
| 131 | + return JSON.stringify({ status: "success" }); |
| 132 | +}`; |
| 133 | + writeServerLogic(projectRoot, 'test-endpoint', noTryCatchJs, VALID_YML); |
| 134 | + |
| 135 | + const result = runValidator(projectRoot); |
| 136 | + assert.equal(result.status, 2); |
| 137 | + assert.match(result.stderr, /missing try\/catch/); |
| 138 | +}); |
| 139 | + |
| 140 | +test('disallowed function name is flagged', (t) => { |
| 141 | + const projectRoot = createTempProject(t); |
| 142 | + setupProject(projectRoot); |
| 143 | + const badFnJs = `function get() { |
| 144 | + try { |
| 145 | + Server.Logger.Log("test-endpoint GET called"); |
| 146 | + return JSON.stringify({ status: "success" }); |
| 147 | + } catch (err) { |
| 148 | + Server.Logger.Error("test-endpoint GET failed: " + err.message); |
| 149 | + return JSON.stringify({ status: "error", message: err.message }); |
| 150 | + } |
| 151 | +} |
| 152 | +
|
| 153 | +function helper() { |
| 154 | + return "not allowed"; |
| 155 | +}`; |
| 156 | + writeServerLogic(projectRoot, 'test-endpoint', badFnJs, VALID_YML); |
| 157 | + |
| 158 | + const result = runValidator(projectRoot); |
| 159 | + assert.equal(result.status, 2); |
| 160 | + assert.match(result.stderr, /found additional top-level functions: helper/); |
| 161 | +}); |
| 162 | + |
| 163 | +test('yml name mismatch is flagged', (t) => { |
| 164 | + const projectRoot = createTempProject(t); |
| 165 | + setupProject(projectRoot); |
| 166 | + const mismatchYml = VALID_YML.replace('name: test-endpoint', 'name: wrong-name'); |
| 167 | + writeServerLogic(projectRoot, 'test-endpoint', VALID_JS, mismatchYml); |
| 168 | + |
| 169 | + const result = runValidator(projectRoot); |
| 170 | + assert.equal(result.status, 2); |
| 171 | + assert.match(result.stderr, /does not match folder name/); |
| 172 | +}); |
0 commit comments