api-process-spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { BrowserWindow } from 'electron';
  2. import { app } from 'electron/main';
  3. import { expect } from 'chai';
  4. import * as fs from 'node:fs';
  5. import * as path from 'node:path';
  6. import { defer } from './lib/spec-helpers';
  7. import { closeAllWindows } from './lib/window-helpers';
  8. describe('process module', () => {
  9. function generateSpecs (invoke: <T extends (...args: any[]) => any>(fn: T, ...args: Parameters<T>) => Promise<ReturnType<T>>) {
  10. describe('process.getCreationTime()', () => {
  11. it('returns a creation time', async () => {
  12. const creationTime = await invoke(() => process.getCreationTime());
  13. expect(creationTime).to.be.a('number').and.be.at.least(0);
  14. });
  15. });
  16. describe('process.getCPUUsage()', () => {
  17. it('returns a cpu usage object', async () => {
  18. const cpuUsage = await invoke(() => process.getCPUUsage());
  19. expect(cpuUsage.percentCPUUsage).to.be.a('number');
  20. expect(cpuUsage.cumulativeCPUUsage).to.be.a('number');
  21. expect(cpuUsage.idleWakeupsPerSecond).to.be.a('number');
  22. });
  23. });
  24. describe('process.getBlinkMemoryInfo()', () => {
  25. it('returns blink memory information object', async () => {
  26. const heapStats = await invoke(() => process.getBlinkMemoryInfo());
  27. expect(heapStats.allocated).to.be.a('number');
  28. expect(heapStats.total).to.be.a('number');
  29. });
  30. });
  31. describe('process.getProcessMemoryInfo()', () => {
  32. it('resolves promise successfully with valid data', async () => {
  33. const memoryInfo = await invoke(() => process.getProcessMemoryInfo());
  34. expect(memoryInfo).to.be.an('object');
  35. if (process.platform === 'linux' || process.platform === 'win32') {
  36. expect(memoryInfo.residentSet).to.be.a('number').greaterThan(0);
  37. }
  38. expect(memoryInfo.private).to.be.a('number').greaterThan(0);
  39. // Shared bytes can be zero
  40. expect(memoryInfo.shared).to.be.a('number').greaterThan(-1);
  41. });
  42. });
  43. describe('process.getSystemMemoryInfo()', () => {
  44. it('returns system memory info object', async () => {
  45. const systemMemoryInfo = await invoke(() => process.getSystemMemoryInfo());
  46. expect(systemMemoryInfo.free).to.be.a('number');
  47. expect(systemMemoryInfo.total).to.be.a('number');
  48. });
  49. });
  50. describe('process.getSystemVersion()', () => {
  51. it('returns a string', async () => {
  52. const systemVersion = await invoke(() => process.getSystemVersion());
  53. expect(systemVersion).to.be.a('string');
  54. });
  55. });
  56. describe('process.getHeapStatistics()', () => {
  57. it('returns heap statistics object', async () => {
  58. const heapStats = await invoke(() => process.getHeapStatistics());
  59. expect(heapStats.totalHeapSize).to.be.a('number');
  60. expect(heapStats.totalHeapSizeExecutable).to.be.a('number');
  61. expect(heapStats.totalPhysicalSize).to.be.a('number');
  62. expect(heapStats.totalAvailableSize).to.be.a('number');
  63. expect(heapStats.usedHeapSize).to.be.a('number');
  64. expect(heapStats.heapSizeLimit).to.be.a('number');
  65. expect(heapStats.mallocedMemory).to.be.a('number');
  66. expect(heapStats.peakMallocedMemory).to.be.a('number');
  67. expect(heapStats.doesZapGarbage).to.be.a('boolean');
  68. });
  69. });
  70. describe('process.takeHeapSnapshot()', () => {
  71. // DISABLED-FIXME(nornagon): this seems to take a really long time when run in the
  72. // main process, for unknown reasons.
  73. it('returns true on success', async () => {
  74. const filePath = path.join(app.getPath('temp'), 'test.heapsnapshot');
  75. defer(() => {
  76. try {
  77. fs.unlinkSync(filePath);
  78. } catch {
  79. // ignore error
  80. }
  81. });
  82. const success = await invoke((filePath: string) => process.takeHeapSnapshot(filePath), filePath);
  83. expect(success).to.be.true();
  84. const stats = fs.statSync(filePath);
  85. expect(stats.size).not.to.be.equal(0);
  86. });
  87. it('returns false on failure', async () => {
  88. const success = await invoke((filePath: string) => process.takeHeapSnapshot(filePath), '');
  89. expect(success).to.be.false();
  90. });
  91. });
  92. }
  93. describe('renderer process', () => {
  94. let w: BrowserWindow;
  95. before(async () => {
  96. w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  97. await w.loadURL('about:blank');
  98. });
  99. after(closeAllWindows);
  100. generateSpecs((fn, ...args) => {
  101. const jsonArgs = args.map(value => JSON.stringify(value)).join(',');
  102. return w.webContents.executeJavaScript(`(${fn.toString()})(${jsonArgs})`);
  103. });
  104. describe('process.contextId', () => {
  105. it('is a string', async () => {
  106. const contextId = await w.webContents.executeJavaScript('process.contextId');
  107. expect(contextId).to.be.a('string');
  108. });
  109. });
  110. });
  111. describe('main process', () => {
  112. generateSpecs((fn, ...args) => fn(...args));
  113. });
  114. });