|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import * as path from 'path'; |
| 8 | +import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit'; |
| 9 | +import { expect, config } from 'chai'; |
| 10 | +import { AuthInfo, Connection } from '@salesforce/core'; |
| 11 | +import { LogGetResult } from '../../../src/commands/apex/get/log'; |
| 12 | +import { LogListResult } from '../../../src/commands/apex/list/log'; |
| 13 | + |
| 14 | +config.truncateThreshold = 0; |
| 15 | + |
| 16 | +describe.only('apex log *', () => { |
| 17 | + let session: TestSession; |
| 18 | + let logId: string | undefined; |
| 19 | + before(async () => { |
| 20 | + session = await TestSession.create({ |
| 21 | + project: { |
| 22 | + gitClone: 'https://github.com/trailheadapps/dreamhouse-lwc.git', |
| 23 | + }, |
| 24 | + devhubAuthStrategy: 'AUTO', |
| 25 | + scratchOrgs: [ |
| 26 | + { |
| 27 | + config: path.join('config', 'project-scratch-def.json'), |
| 28 | + setDefault: true, |
| 29 | + alias: 'org', |
| 30 | + }, |
| 31 | + ], |
| 32 | + }); |
| 33 | + |
| 34 | + execCmd('project:deploy:start -o org --source-dir force-app', { ensureExitCode: 0 }); |
| 35 | + }); |
| 36 | + |
| 37 | + after(async () => { |
| 38 | + await session?.zip(undefined, 'artifacts'); |
| 39 | + await session?.clean(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('will print no logs found', async () => { |
| 43 | + const result = execCmd('apex:list:log', { ensureExitCode: 0 }).shellOutput.stdout; |
| 44 | + expect(result).to.include('No debug logs found in org'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('will print no logs found --json', async () => { |
| 48 | + const result = execCmd<LogGetResult>('apex:list:log --json', { ensureExitCode: 0 }).jsonOutput?.result; |
| 49 | + expect(result?.length).to.equal(0); |
| 50 | + }); |
| 51 | + |
| 52 | + it('will generate the TraceFlag for the user', async () => { |
| 53 | + const conn = await Connection.create({ |
| 54 | + authInfo: await AuthInfo.create({ username: session.orgs.get('default')?.username }), |
| 55 | + }); |
| 56 | + const query = await conn.singleRecordQuery<{ TracedEntityId: string; DebugLevelId: string }>( |
| 57 | + 'SELECT TracedEntityId, DebugLevelId FROM TraceFlag', |
| 58 | + { tooling: true } |
| 59 | + ); |
| 60 | + const now = new Date(); |
| 61 | + // set it to expire in 24 hours and convert to format |
| 62 | + const expiration = new Date(new Date(now).getTime() + 60 * 60 * 24 * 1000) |
| 63 | + .toISOString() |
| 64 | + .replace(/\.\d{3}Z$/, '+0000'); |
| 65 | + |
| 66 | + await conn.tooling.sobject('TraceFlag').create({ |
| 67 | + ExpirationDate: expiration, |
| 68 | + TracedEntityId: query.TracedEntityId, |
| 69 | + LogType: 'USER_DEBUG', |
| 70 | + DebugLevelId: query.DebugLevelId, |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('will run an apex class to generate logs', async () => { |
| 75 | + const result = execCmd( |
| 76 | + `apex:run --file ${path.join('force-app', 'main', 'default', 'classes', 'PagedResult.cls')}`, |
| 77 | + { |
| 78 | + ensureExitCode: 0, |
| 79 | + } |
| 80 | + ).shellOutput.stdout; |
| 81 | + expect(result).to.include('Compiled successfully.'); |
| 82 | + expect(result).to.include('Executed successfully.'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('will list the debug logs', async () => { |
| 86 | + const result = execCmd('apex:list:log', { ensureExitCode: 0 }).shellOutput.stdout; |
| 87 | + expect(result).to.match( |
| 88 | + / APPLICATION DURATION \(MS\) ID\s+LOCATION\s+SIZE \(B\) LOG USER\s+OPERATION REQUEST START TIME\s+STATUS / |
| 89 | + ); |
| 90 | + expect(result).to.match(/User User Api\s+Api\s+\d{4}-\d{2}-.* Success /); |
| 91 | + }); |
| 92 | + |
| 93 | + it('will list the debug logs --json', async () => { |
| 94 | + const result = execCmd<LogListResult>('apex:list:log --json', { ensureExitCode: 0 }).jsonOutput?.result; |
| 95 | + expect(result?.length).to.equal(1); |
| 96 | + const log = result?.filter((l) => l)[0]; |
| 97 | + expect(log).to.have.all.keys( |
| 98 | + 'attributes', |
| 99 | + 'Application', |
| 100 | + 'DurationMilliseconds', |
| 101 | + 'Id', |
| 102 | + 'Location', |
| 103 | + 'LogLength', |
| 104 | + 'LogUser', |
| 105 | + 'Operation', |
| 106 | + 'Request', |
| 107 | + 'StartTime', |
| 108 | + 'Status' |
| 109 | + ); |
| 110 | + expect(log?.Status).to.equal('Success'); |
| 111 | + expect(log?.Location).to.equal('Monitoring'); |
| 112 | + logId = log?.Id; |
| 113 | + expect(logId?.startsWith('07L')).to.be.true; |
| 114 | + }); |
| 115 | + |
| 116 | + it('will get the debug log', async () => { |
| 117 | + const result = execCmd(`apex:get:log -i ${logId}`, { ensureExitCode: 0 }).shellOutput.stdout; |
| 118 | + expect(result).to.include('Number of SOQL queries:'); |
| 119 | + expect(result).to.include('Number of queueable jobs added to the queue:'); |
| 120 | + expect(result).to.include('CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex'); |
| 121 | + expect(result).to.include('|CUMULATIVE_LIMIT_USAGE_END'); |
| 122 | + expect(result).to.include('|EXECUTION_FINISHED'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('will get the debug log --number', async () => { |
| 126 | + const result = execCmd('apex:get:log --number 1', { ensureExitCode: 0 }).shellOutput.stdout; |
| 127 | + expect(result).to.include('Number of SOQL queries:'); |
| 128 | + expect(result).to.include('Number of queueable jobs added to the queue:'); |
| 129 | + expect(result).to.include('CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex'); |
| 130 | + expect(result).to.include('|CUMULATIVE_LIMIT_USAGE_END'); |
| 131 | + expect(result).to.include('|EXECUTION_FINISHED'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('will get the debug log --number --json', async () => { |
| 135 | + const result = execCmd<LogGetResult>('apex:get:log --number 1 --json', { ensureExitCode: 0 }).jsonOutput?.result; |
| 136 | + expect(result?.length).to.equal(1); |
| 137 | + const log = (result?.filter((l) => l)[0] as { log: string }).log; |
| 138 | + expect(log).to.include('Number of SOQL queries:'); |
| 139 | + expect(log).to.include('Number of queueable jobs added to the queue:'); |
| 140 | + expect(log).to.include('CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex'); |
| 141 | + expect(log).to.include('|CUMULATIVE_LIMIT_USAGE_END'); |
| 142 | + expect(log).to.include('|EXECUTION_FINISHED'); |
| 143 | + }); |
| 144 | +}); |
0 commit comments