|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import { writeFileSync, unlinkSync, existsSync } from 'node:fs'; |
18 | | -import { join } from 'node:path'; |
| 17 | +import { execSync } from 'node:child_process'; |
19 | 18 | import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit'; |
20 | 19 | import { expect } from 'chai'; |
| 20 | +import { |
| 21 | + createProject, |
| 22 | + createProjectWithWebapp, |
| 23 | + createEmptyWebappsDir, |
| 24 | + createWebappDirWithoutMeta, |
| 25 | + writeManifest, |
| 26 | + webappPath, |
| 27 | + ensureSfCli, |
| 28 | + authOrgViaUrl, |
| 29 | +} from './helpers/webappProjectUtils.js'; |
21 | 30 |
|
22 | | -describe('webapp dev NUTs', () => { |
| 31 | +/* ------------------------------------------------------------------ * |
| 32 | + * Tier 1 — No Auth * |
| 33 | + * * |
| 34 | + * Validates flag-level parse errors that fire before any org or * |
| 35 | + * filesystem interaction. No credentials needed; always runs. * |
| 36 | + * ------------------------------------------------------------------ */ |
| 37 | +describe('webapp dev NUTs — Tier 1 (no auth)', () => { |
23 | 38 | let session: TestSession; |
24 | | - const testWebappJson = { |
25 | | - name: 'testWebApp', |
26 | | - label: 'Test Web App', |
27 | | - version: '1.0.0', |
28 | | - outputDir: 'dist', |
29 | | - dev: { |
30 | | - url: 'http://localhost:5173', |
31 | | - }, |
32 | | - }; |
33 | 39 |
|
34 | 40 | before(async () => { |
35 | 41 | session = await TestSession.create({ devhubAuthStrategy: 'NONE' }); |
36 | 42 | }); |
37 | 43 |
|
38 | 44 | after(async () => { |
39 | 45 | await session?.clean(); |
40 | | - // Clean up any test webapplication.json files |
41 | | - const webappJsonPath = join(session?.dir ?? process.cwd(), 'webapplication.json'); |
42 | | - if (existsSync(webappJsonPath)) { |
43 | | - unlinkSync(webappJsonPath); |
44 | | - } |
45 | 46 | }); |
46 | 47 |
|
47 | | - it('should fail without target-org flag', () => { |
48 | | - // Create webapplication.json for this test |
49 | | - const webappJsonPath = join(session.dir, 'webapplication.json'); |
50 | | - writeFileSync(webappJsonPath, JSON.stringify(testWebappJson, null, 2)); |
51 | | - |
52 | | - const result = execCmd('webapp dev --name testWebApp --json', { |
| 48 | + // --target-org is declared as Flags.requiredOrg(). Running without it |
| 49 | + // must fail at parse time with NoDefaultEnvError before any other logic. |
| 50 | + it('should require --target-org', () => { |
| 51 | + const result = execCmd('webapp dev --json', { |
53 | 52 | ensureExitCode: 1, |
54 | 53 | cwd: session.dir, |
55 | 54 | }); |
56 | 55 |
|
57 | 56 | expect(result.jsonOutput?.name).to.equal('NoDefaultEnvError'); |
58 | 57 | expect(result.jsonOutput?.message).to.include('target-org'); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +/* ------------------------------------------------------------------ * |
| 62 | + * Tier 2 — CLI Validation (with auth) * |
| 63 | + * * |
| 64 | + * Validates webapp discovery errors and URL resolution errors. * |
| 65 | + * Auth is only needed so --target-org passes parsing; these tests * |
| 66 | + * exercise local filesystem/network checks — no live org calls. * |
| 67 | + * * |
| 68 | + * Requires TESTKIT_AUTH_URL. Fails when absent (tests are mandatory). * |
| 69 | + * ------------------------------------------------------------------ */ |
| 70 | +describe('webapp dev NUTs — Tier 2 CLI validation', () => { |
| 71 | + let session: TestSession; |
| 72 | + let targetOrg: string; |
| 73 | + |
| 74 | + before(async function () { |
| 75 | + if (!process.env.TESTKIT_AUTH_URL) { |
| 76 | + throw new Error( |
| 77 | + 'TESTKIT_AUTH_URL is required for Tier 2 tests. Set it in .env (local) or CI secrets (GitHub Actions).' |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + session = await TestSession.create({ devhubAuthStrategy: 'NONE' }); |
| 82 | + ensureSfCli(); |
| 83 | + targetOrg = authOrgViaUrl(); |
| 84 | + }); |
59 | 85 |
|
60 | | - // Clean up |
61 | | - unlinkSync(webappJsonPath); |
| 86 | + after(async () => { |
| 87 | + await session?.clean(); |
62 | 88 | }); |
63 | 89 |
|
64 | | - // Note: Additional error scenario tests (manifest validation, dev server config) |
65 | | - // require authenticated orgs, which may not be available in all CI/CD environments. |
66 | | - // These scenarios are covered by unit tests instead. |
67 | | - // |
68 | | - // Scenarios covered in unit tests: |
69 | | - // - Missing webapplication.json manifest (ManifestWatcher.test.ts) |
70 | | - // - Invalid webapplication.json schema (ManifestWatcher.test.ts) |
71 | | - // - Malformed JSON syntax (ManifestWatcher.test.ts) |
72 | | - // - Missing dev server config (DevServerManager.test.ts) |
73 | | - // |
74 | | - // For local testing with authenticated orgs, use manual validation scripts in: |
75 | | - // docs/manual-tests/test-webapp-dev-command.ts |
| 90 | + // ── Discovery errors ────────────────────────────────────────── |
| 91 | + |
| 92 | + // Project has no webapplications folder at all → WebappNotFoundError. |
| 93 | + it('should error when no webapp found (project only, no webapps)', () => { |
| 94 | + const projectDir = createProject(session, 'noWebappProject'); |
| 95 | + |
| 96 | + const result = execCmd(`webapp dev --target-org ${targetOrg} --json`, { |
| 97 | + ensureExitCode: 1, |
| 98 | + cwd: projectDir, |
| 99 | + }); |
| 100 | + |
| 101 | + expect(result.jsonOutput?.name).to.equal('WebappNotFoundError'); |
| 102 | + }); |
| 103 | + |
| 104 | + // Project has webapp "realApp" but --name asks for "NonExistent" → WebappNameNotFoundError. |
| 105 | + it('should error when --name does not match any webapp', () => { |
| 106 | + const projectDir = createProjectWithWebapp(session, 'nameNotFound', 'realApp'); |
| 107 | + |
| 108 | + const result = execCmd(`webapp dev --name NonExistent --target-org ${targetOrg} --json`, { |
| 109 | + ensureExitCode: 1, |
| 110 | + cwd: projectDir, |
| 111 | + }); |
| 112 | + |
| 113 | + expect(result.jsonOutput?.name).to.equal('WebappNameNotFoundError'); |
| 114 | + }); |
| 115 | + |
| 116 | + // cwd is inside webapp "appA" but --name asks for "appB" → WebappNameConflictError. |
| 117 | + // Discovery treats this as ambiguous intent and rejects it. |
| 118 | + it('should error on --name conflict when inside a different webapp', () => { |
| 119 | + const projectDir = createProjectWithWebapp(session, 'nameConflict', 'appA'); |
| 120 | + execSync('sf webapp generate --name appB', { cwd: projectDir, stdio: 'pipe' }); |
| 121 | + |
| 122 | + const cwdInsideAppA = webappPath(projectDir, 'appA'); |
| 123 | + |
| 124 | + const result = execCmd(`webapp dev --name appB --target-org ${targetOrg} --json`, { |
| 125 | + ensureExitCode: 1, |
| 126 | + cwd: cwdInsideAppA, |
| 127 | + }); |
| 128 | + |
| 129 | + expect(result.jsonOutput?.name).to.equal('WebappNameConflictError'); |
| 130 | + }); |
| 131 | + |
| 132 | + // webapplications/ folder exists but is empty → WebappNotFoundError. |
| 133 | + it('should error when webapplications folder is empty', () => { |
| 134 | + const projectDir = createProject(session, 'emptyWebapps'); |
| 135 | + createEmptyWebappsDir(projectDir); |
| 136 | + |
| 137 | + const result = execCmd(`webapp dev --target-org ${targetOrg} --json`, { |
| 138 | + ensureExitCode: 1, |
| 139 | + cwd: projectDir, |
| 140 | + }); |
| 141 | + |
| 142 | + expect(result.jsonOutput?.name).to.equal('WebappNotFoundError'); |
| 143 | + }); |
| 144 | + |
| 145 | + // webapplications/orphanApp/ exists but has no .webapplication-meta.xml → not a valid webapp. |
| 146 | + it('should error when webapp dir has no .webapplication-meta.xml', () => { |
| 147 | + const projectDir = createProject(session, 'noMeta'); |
| 148 | + createWebappDirWithoutMeta(projectDir, 'orphanApp'); |
| 149 | + |
| 150 | + const result = execCmd(`webapp dev --target-org ${targetOrg} --json`, { |
| 151 | + ensureExitCode: 1, |
| 152 | + cwd: projectDir, |
| 153 | + }); |
| 154 | + |
| 155 | + expect(result.jsonOutput?.name).to.equal('WebappNotFoundError'); |
| 156 | + }); |
| 157 | + |
| 158 | + // ── Auto-selection ──────────────────────────────────────────── |
| 159 | + |
| 160 | + // When cwd is inside webapplications/myApp/, discovery auto-selects that |
| 161 | + // webapp without --name. The command proceeds past discovery and fails at |
| 162 | + // URL resolution (no dev server running) — confirming auto-select worked. |
| 163 | + it('should auto-select webapp when run from inside its directory', () => { |
| 164 | + const projectDir = createProjectWithWebapp(session, 'autoSelect', 'myApp'); |
| 165 | + |
| 166 | + writeManifest(projectDir, 'myApp', { |
| 167 | + dev: { url: 'http://localhost:5179' }, |
| 168 | + }); |
| 169 | + |
| 170 | + const cwdInsideApp = webappPath(projectDir, 'myApp'); |
| 171 | + |
| 172 | + // No --name flag; cwd is inside the webapp directory. |
| 173 | + // Discovery auto-selects myApp, then the command fails at URL check |
| 174 | + // (nothing running on 5179). DevServerUrlError proves discovery succeeded. |
| 175 | + const result = execCmd(`webapp dev --target-org ${targetOrg} --json`, { |
| 176 | + ensureExitCode: 1, |
| 177 | + cwd: cwdInsideApp, |
| 178 | + }); |
| 179 | + |
| 180 | + expect(result.jsonOutput?.name).to.equal('DevServerUrlError'); |
| 181 | + }); |
| 182 | + |
| 183 | + // ── URL / dev server errors ─────────────────────────────────── |
| 184 | + |
| 185 | + // --url explicitly provided but nothing is listening → DevServerUrlError. |
| 186 | + // The command refuses to start a dev server when --url is given. |
| 187 | + it('should error when --url is unreachable', () => { |
| 188 | + const projectDir = createProjectWithWebapp(session, 'urlUnreachable', 'myApp'); |
| 189 | + |
| 190 | + const result = execCmd(`webapp dev --name myApp --url http://localhost:5179 --target-org ${targetOrg} --json`, { |
| 191 | + ensureExitCode: 1, |
| 192 | + cwd: projectDir, |
| 193 | + }); |
| 194 | + |
| 195 | + expect(result.jsonOutput?.name).to.equal('DevServerUrlError'); |
| 196 | + }); |
| 197 | + |
| 198 | + // Manifest has dev.url but no dev.command → command can't start the server |
| 199 | + // itself and the URL is unreachable → DevServerUrlError. |
| 200 | + it('should error when dev.url is unreachable and no dev.command', () => { |
| 201 | + const projectDir = createProjectWithWebapp(session, 'urlNoCmd', 'myApp'); |
| 202 | + |
| 203 | + writeManifest(projectDir, 'myApp', { |
| 204 | + dev: { url: 'http://localhost:5179' }, |
| 205 | + }); |
| 206 | + |
| 207 | + const result = execCmd(`webapp dev --name myApp --target-org ${targetOrg} --json`, { |
| 208 | + ensureExitCode: 1, |
| 209 | + cwd: projectDir, |
| 210 | + }); |
| 211 | + |
| 212 | + expect(result.jsonOutput?.name).to.equal('DevServerUrlError'); |
| 213 | + }); |
76 | 214 | }); |
0 commit comments