{"version":3,"file":"test-logger.mjs","names":[],"sources":["../../src/testing/test-logger.ts"],"sourcesContent":["import { Logger } from '../types/logging'\nimport { LogSnapshotConfig } from '../types/testing'\nimport { asJson } from '../util'\n\n/** Exposes an AlgoKit logger which captures log messages, while wrapping an original logger.\n * This is useful for automated testing.\n */\nexport class TestLogger implements Logger {\n  private originalLogger: Logger | undefined\n  private logs: string[]\n\n  /**\n   * Create a new test logger that wraps the given logger if provided.\n   * @param originalLogger The optional original logger to wrap.\n   */\n  constructor(originalLogger?: Logger) {\n    this.originalLogger = originalLogger\n    this.logs = []\n  }\n\n  /** Returns all logs captured thus far. */\n  get capturedLogs(): string[] {\n    return this.logs\n  }\n\n  /** Clears all logs captured so far. */\n  clear() {\n    this.logs = []\n  }\n\n  /**\n   * Returns a captured log snapshot.\n   * This helps ensure that the provided configuration items won't appear\n   *  with random values in the log snapshot, but rather will get substituted with predictable ids.\n   *\n   * https://jestjs.io/docs/snapshot-testing#2-tests-should-be-deterministic\n   *\n   * @example Jest Example\n   * ```typescript\n   * const logger = new TestLogger()\n   * ...\n   * expect(logger.getLogSnapshot()).toMatchSnapshot()\n   * ```\n   * @param config The snapshot configuration\n   * @returns The snapshotted logs.\n   */\n  getLogSnapshot(config?: LogSnapshotConfig) {\n    const { transactions: transactionIds, accounts, apps } = config ?? {}\n    let snapshot = this.capturedLogs.filter(config?.filterPredicate ?? (() => true)).join('\\n')\n    transactionIds?.forEach(\n      (txn, id) => (snapshot = snapshot.replace(new RegExp(typeof txn === 'string' ? txn : txn.txID(), 'g'), `TXID_${id + 1}`)),\n    )\n    accounts?.forEach(\n      (sender, id) =>\n        (snapshot = snapshot.replace(\n          new RegExp(\n            typeof sender === 'string'\n              ? sender\n              : 'addr' in sender\n                ? sender.addr.toString()\n                : 'address' in sender\n                  ? sender.address().toString()\n                  : sender.toString(),\n            'g',\n          ),\n          `ACCOUNT_${id + 1}`,\n        )),\n    )\n    apps?.forEach((app, id) => (snapshot = snapshot.replace(new RegExp(`\\\\b${app.toString()}\\\\b(?! bytes)`, 'g'), `APP_${id + 1}`)))\n    return snapshot\n  }\n\n  error(message: string, ...optionalParams: unknown[]): void {\n    this.originalLogger?.error(message, ...optionalParams)\n    this.logs.push(`ERROR: ${message}${optionalParams.length ? ` | ${asJson(optionalParams)}` : ''}`)\n  }\n  warn(message: string, ...optionalParams: unknown[]): void {\n    this.originalLogger?.warn(message, ...optionalParams)\n    this.logs.push(`WARN: ${message}${optionalParams.length ? ` | ${asJson(optionalParams)}` : ''}`)\n  }\n  info(message: string, ...optionalParams: unknown[]): void {\n    this.originalLogger?.info(message, ...optionalParams)\n    this.logs.push(`INFO: ${message}${optionalParams.length ? ` | ${asJson(optionalParams)}` : ''}`)\n  }\n  verbose(message: string, ...optionalParams: unknown[]): void {\n    this.originalLogger?.verbose(message, ...optionalParams)\n    this.logs.push(`VERBOSE: ${message}${optionalParams.length ? ` | ${asJson(optionalParams)}` : ''}`)\n  }\n  debug(message: string, ...optionalParams: unknown[]): void {\n    this.originalLogger?.debug(message, ...optionalParams)\n    this.logs.push(`DEBUG: ${message}${optionalParams.length ? ` | ${asJson(optionalParams)}` : ''}`)\n  }\n}\n"],"mappings":";;;;;AAOA,IAAa,aAAb,MAA0C;CACxC;CACA;;;;;CAMA,YAAY,gBAAyB;EACnC,KAAK,iBAAiB;EACtB,KAAK,OAAO,CAAC;CACf;;CAGA,IAAI,eAAyB;EAC3B,OAAO,KAAK;CACd;;CAGA,QAAQ;EACN,KAAK,OAAO,CAAC;CACf;;;;;;;;;;;;;;;;;CAkBA,eAAe,QAA4B;EACzC,MAAM,EAAE,cAAc,gBAAgB,UAAU,SAAS,UAAU,CAAC;EACpE,IAAI,WAAW,KAAK,aAAa,OAAO,QAAQ,0BAA0B,KAAK,CAAC,CAAC,KAAK,IAAI;EAC1F,gBAAgB,SACb,KAAK,OAAQ,WAAW,SAAS,QAAQ,IAAI,OAAO,OAAO,QAAQ,WAAW,MAAM,IAAI,KAAK,GAAG,GAAG,GAAG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