|
| 1 | +# AGENTS.md - Development Guide |
| 2 | + |
| 3 | +This document provides guidelines for agents working on the electron-windows codebase. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +electron-windows is an Electron module for managing multiple windows gracefully. It provides window creation, retrieval by name/ID, state persistence, and loading views. |
| 8 | + |
| 9 | +## Commands |
| 10 | + |
| 11 | +### Development |
| 12 | +```bash |
| 13 | +pnpm run dev # Launch Electron demo app |
| 14 | +``` |
| 15 | + |
| 16 | +### Testing |
| 17 | +```bash |
| 18 | +pnpm test # Run unit tests with nyc coverage |
| 19 | +pnpm run test:e2e # Run end-to-end tests with Playwright |
| 20 | +``` |
| 21 | + |
| 22 | +To run a **single test file**: |
| 23 | +```bash |
| 24 | +npx mocha test/unit/electron-windows.test.js |
| 25 | +``` |
| 26 | + |
| 27 | +To run a **single test**: |
| 28 | +```bash |
| 29 | +npx mocha test/unit/electron-windows.test.js --grep "should create window with default options" |
| 30 | +``` |
| 31 | + |
| 32 | +### Linting |
| 33 | +```bash |
| 34 | +pnpm run lint # Run ESLint with auto-fix |
| 35 | +``` |
| 36 | + |
| 37 | +## Code Style Guidelines |
| 38 | + |
| 39 | +### General |
| 40 | +- Language: JavaScript (ES2018), CommonJS modules |
| 41 | +- Always use `'use strict';` at the top of every file |
| 42 | +- Use 2-space indentation |
| 43 | + |
| 44 | +### Imports/Exports |
| 45 | +- Use CommonJS: `const X = require('path')` and `module.exports = X` |
| 46 | +- Order: built-in modules first, then external packages, then local files |
| 47 | +- Empty line between groups |
| 48 | + |
| 49 | +```javascript |
| 50 | +'use strict'; |
| 51 | + |
| 52 | +const fs = require('fs'); |
| 53 | +const path = require('path'); |
| 54 | +const _ = require('lodash'); |
| 55 | +const { BrowserWindow } = require('electron'); |
| 56 | +const windowStateKeeper = require('electron-window-state'); |
| 57 | + |
| 58 | +const MyClass = require('./my-class'); |
| 59 | +``` |
| 60 | + |
| 61 | +### Naming Conventions |
| 62 | +- **Classes**: PascalCase (e.g., `WindowsManager`) |
| 63 | +- **Methods/variables**: camelCase (e.g., `createWindow`, `windowOptions`) |
| 64 | +- **Constants**: SCREAMING_SNAKE_CASE (e.g., `DEFAULT_WIDTH`) |
| 65 | +- **Private methods**: prefix with underscore (e.g., `_setGlobalUserAgent`) |
| 66 | +- **Files**: kebab-case (e.g., `electron-windows.js`) |
| 67 | + |
| 68 | +### Types and JSDoc |
| 69 | +- This project does not use TypeScript |
| 70 | +- Use JSDoc comments for public APIs (though `valid-jsdoc` is disabled) |
| 71 | +- Document params and return values |
| 72 | + |
| 73 | +```javascript |
| 74 | +/** |
| 75 | + * Creates a new window. |
| 76 | + * @param {Object} options - Window configuration |
| 77 | + * @param {string} [options.name='anonymous'] - Window identifier |
| 78 | + * @returns {BrowserWindow} The created window |
| 79 | + */ |
| 80 | +``` |
| 81 | + |
| 82 | +### Error Handling |
| 83 | +- Use `assert` for tests |
| 84 | +- Return early on error conditions |
| 85 | +- Check for destroyed windows before operations |
| 86 | + |
| 87 | +```javascript |
| 88 | +if (window.isDestroyed()) { |
| 89 | + return; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Best Practices |
| 94 | +- Always check if Electron objects are destroyed before using them: |
| 95 | + ```javascript |
| 96 | + if (window && !window.isDestroyed()) { ... } |
| 97 | + if (webContents && !webContents.isDestroyed()) { ... } |
| 98 | + ``` |
| 99 | +- Use lodash utilities (already a dependency): `_.pick`, `_.debounce`, etc. |
| 100 | +- Avoid arrow functions for methods that need `this` binding |
| 101 | + |
| 102 | +## Architecture |
| 103 | + |
| 104 | +- **Main entry**: `index.js` exports from `lib/electron-windows.js` |
| 105 | +- **Core class**: `WindowsManager` in `lib/electron-windows.js` |
| 106 | +- **Tests**: `test/unit/` for unit tests, `test/e2e/` for E2E tests |
| 107 | +- **Mock setup**: `test/unit/mock-setup.js` provides Electron mocks |
| 108 | + |
| 109 | +## Testing Patterns |
| 110 | + |
| 111 | +- Use Mocha's `describe`/`it` structure |
| 112 | +- Use `beforeEach` to reset mocks |
| 113 | +- Use Node's `assert` module for assertions |
| 114 | + |
| 115 | +```javascript |
| 116 | +const assert = require('assert'); |
| 117 | +const WindowsManager = require('../../lib/electron-windows'); |
| 118 | + |
| 119 | +describe('WindowsManager', () => { |
| 120 | + beforeEach(() => { |
| 121 | + require('./mock-setup').reset(); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('create()', () => { |
| 125 | + it('should create window with default options', () => { |
| 126 | + const manager = new WindowsManager(); |
| 127 | + const window = manager.create({}); |
| 128 | + assert.strictEqual(window._name, 'anonymous'); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
| 132 | +``` |
0 commit comments