this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add tests

+307 -2
+2
package.json
··· 25 25 "lint:fix": "npm run _eslint -- --fix", 26 26 "quality:check": "npm run format:check && npm run lint:check && npm run unused:check", 27 27 "quality:fix": "npm run format:fix && npm run lint:fix && npm run unused:fix", 28 + "test": "node --experimental-test-module-mocks --test", 29 + "test:coverage": "node --experimental-test-coverage --experimental-test-module-mocks --test", 28 30 "unused:check": "npm run _knip", 29 31 "unused:fix": "npm run _knip -- --fix" 30 32 },
+2 -2
source/get-terminal-profiles.js
··· 1 1 import os from 'node:os'; 2 - import bplist from 'bplist-parser'; 2 + import {parseFile as parsePlistFile} from 'bplist-parser'; 3 3 4 4 /** 5 5 * @returns {Promise<string[]>} - List of installed profiles 6 6 */ 7 7 export default async function getTerminalProfiles() { 8 8 const terminalPlistPath = `${os.homedir()}/Library/Preferences/com.apple.Terminal.plist`; 9 - const terminalPreferences = await bplist.parseFile(terminalPlistPath); 9 + const terminalPreferences = await parsePlistFile(terminalPlistPath); 10 10 11 11 return Object.keys(terminalPreferences[0]['Window Settings']).toSorted( 12 12 new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}).compare,
+55
test/get-terminal-default-profile.js
··· 1 + import {beforeEach, describe, it, mock} from 'node:test'; 2 + import assert from 'node:assert/strict'; 3 + 4 + const childProcess = {execFile: mock.fn()}; 5 + mock.module('node:child_process', {namedExports: childProcess}); 6 + 7 + const runAppleScript = mock.fn(); 8 + mock.module('run-applescript', {namedExports: {runAppleScript}}); 9 + 10 + const isTerminalRunning = mock.fn(); 11 + mock.module('../source/is-terminal-running.js', { 12 + defaultExport: isTerminalRunning, 13 + }); 14 + 15 + const {default: getTerminalDefaultProfile} = 16 + await import('../source/get-terminal-default-profile.js'); 17 + 18 + describe('getTerminalDefaultProfile', () => { 19 + beforeEach(() => { 20 + childProcess.execFile.mock.resetCalls(); 21 + runAppleScript.mock.resetCalls(); 22 + isTerminalRunning.mock.resetCalls(); 23 + }); 24 + 25 + it('uses AppleScript when Terminal is running', async () => { 26 + isTerminalRunning.mock.mockImplementation(async () => true); 27 + runAppleScript.mock.mockImplementation(async () => 'Profile'); 28 + 29 + const result = await getTerminalDefaultProfile(); 30 + 31 + assert.equal(result, 'Profile'); 32 + }); 33 + 34 + it('uses defaults command when Terminal is not running', async () => { 35 + isTerminalRunning.mock.mockImplementation(async () => false); 36 + childProcess.execFile.mock.mockImplementation( 37 + (_command, _args, callback) => { 38 + callback(null, {stdout: 'Profile\n', stderr: ''}); 39 + }, 40 + ); 41 + 42 + const result = await getTerminalDefaultProfile(); 43 + 44 + assert.equal(result, 'Profile'); 45 + assert.equal(childProcess.execFile.mock.callCount(), 1); 46 + 47 + const [command, args] = childProcess.execFile.mock.calls[0].arguments; 48 + assert.equal(command, 'defaults'); 49 + assert.deepEqual(args, [ 50 + 'read', 51 + 'com.apple.Terminal', 52 + 'Default Window Settings', 53 + ]); 54 + }); 55 + });
+56
test/get-terminal-profiles.js
··· 1 + import {beforeEach, describe, it, mock} from 'node:test'; 2 + import assert from 'node:assert/strict'; 3 + 4 + const os = {homedir: mock.fn(() => '/home')}; 5 + mock.module('node:os', {namedExports: os, defaultExport: os}); 6 + 7 + const bplistParser = {parseFile: mock.fn()}; 8 + mock.module('bplist-parser', {namedExports: bplistParser}); 9 + 10 + const {default: getTerminalProfiles} = 11 + await import('../source/get-terminal-profiles.js'); 12 + 13 + describe('getTerminalProfiles', () => { 14 + beforeEach(() => { 15 + bplistParser.parseFile.mock.resetCalls(); 16 + }); 17 + 18 + it('returns sorted profile names', async () => { 19 + bplistParser.parseFile.mock.mockImplementation(async () => [ 20 + {'Window Settings': {'Light Profile': {}, 'Dark Profile': {}}}, 21 + ]); 22 + 23 + const profiles = await getTerminalProfiles(); 24 + 25 + assert.deepEqual(profiles, ['Dark Profile', 'Light Profile']); 26 + }); 27 + 28 + it('sorts numerically', async () => { 29 + bplistParser.parseFile.mock.mockImplementation(async () => [ 30 + { 31 + 'Window Settings': { 32 + 'Profile 10': {}, 33 + 'Profile 2': {}, 34 + 'Profile 1': {}, 35 + }, 36 + }, 37 + ]); 38 + 39 + const profiles = await getTerminalProfiles(); 40 + 41 + assert.deepEqual(profiles, ['Profile 1', 'Profile 2', 'Profile 10']); 42 + }); 43 + 44 + it('reads the correct plist path', async () => { 45 + bplistParser.parseFile.mock.mockImplementation(async () => [ 46 + {'Window Settings': {}}, 47 + ]); 48 + 49 + await getTerminalProfiles(); 50 + 51 + assert.equal( 52 + bplistParser.parseFile.mock.calls[0].arguments[0], 53 + '/home/Library/Preferences/com.apple.Terminal.plist', 54 + ); 55 + }); 56 + });
+48
test/is-terminal-running.js
··· 1 + import {beforeEach, describe, it, mock} from 'node:test'; 2 + import assert from 'node:assert/strict'; 3 + 4 + const childProcess = {execFile: mock.fn()}; 5 + mock.module('node:child_process', {namedExports: childProcess}); 6 + 7 + const {default: isTerminalRunning} = 8 + await import('../source/is-terminal-running.js'); 9 + 10 + describe('isTerminalRunning', () => { 11 + beforeEach(() => { 12 + childProcess.execFile.mock.resetCalls(); 13 + }); 14 + 15 + it('returns true when Terminal is running', async () => { 16 + childProcess.execFile.mock.mockImplementation( 17 + (_command, _args, callback) => { 18 + callback(null, {stdout: '1234\n', stderr: ''}); 19 + }, 20 + ); 21 + 22 + assert.equal(await isTerminalRunning(), true); 23 + }); 24 + 25 + it('returns false when Terminal is not running', async () => { 26 + childProcess.execFile.mock.mockImplementation( 27 + (_command, _args, callback) => { 28 + callback(new Error('no process found')); 29 + }, 30 + ); 31 + 32 + assert.equal(await isTerminalRunning(), false); 33 + }); 34 + 35 + it('calls pgrep with correct arguments', async () => { 36 + childProcess.execFile.mock.mockImplementation( 37 + (_command, _args, callback) => { 38 + callback(null, {stdout: '', stderr: ''}); 39 + }, 40 + ); 41 + 42 + await isTerminalRunning(); 43 + 44 + const [command, args] = childProcess.execFile.mock.calls[0].arguments; 45 + assert.equal(command, 'pgrep'); 46 + assert.deepEqual(args, ['-x', 'Terminal']); 47 + }); 48 + });
+73
test/set-terminal-default-profile.js
··· 1 + import {beforeEach, describe, it, mock} from 'node:test'; 2 + import assert from 'node:assert/strict'; 3 + 4 + const childProcess = {execFile: mock.fn()}; 5 + mock.module('node:child_process', {namedExports: childProcess}); 6 + 7 + const runAppleScript = mock.fn(); 8 + mock.module('run-applescript', {namedExports: {runAppleScript}}); 9 + 10 + const isTerminalRunning = mock.fn(); 11 + mock.module('../source/is-terminal-running.js', { 12 + defaultExport: isTerminalRunning, 13 + }); 14 + 15 + const {default: setTerminalDefaultProfile} = 16 + await import('../source/set-terminal-default-profile.js'); 17 + 18 + describe('setTerminalDefaultProfile', () => { 19 + beforeEach(() => { 20 + childProcess.execFile.mock.resetCalls(); 21 + runAppleScript.mock.resetCalls(); 22 + isTerminalRunning.mock.resetCalls(); 23 + }); 24 + 25 + it('uses AppleScript when Terminal is running', async () => { 26 + isTerminalRunning.mock.mockImplementation(async () => true); 27 + runAppleScript.mock.mockImplementation(async () => undefined); 28 + 29 + await setTerminalDefaultProfile('Profile'); 30 + 31 + assert.equal( 32 + runAppleScript.mock.calls[0].arguments[0], 33 + [ 34 + 'tell application "Terminal"', 35 + '\tset default settings to settings set "Profile"', 36 + 'end tell', 37 + ].join('\n'), 38 + ); 39 + }); 40 + 41 + it('uses defaults command when Terminal is not running', async () => { 42 + isTerminalRunning.mock.mockImplementation(async () => false); 43 + childProcess.execFile.mock.mockImplementation( 44 + (_command, _args, callback) => { 45 + callback(null, {stdout: '', stderr: ''}); 46 + }, 47 + ); 48 + 49 + await setTerminalDefaultProfile('Profile'); 50 + 51 + assert.equal(childProcess.execFile.mock.callCount(), 2); 52 + 53 + const [command1, args1] = childProcess.execFile.mock.calls[0].arguments; 54 + assert.equal(command1, 'defaults'); 55 + assert.deepEqual(args1, [ 56 + 'write', 57 + 'com.apple.Terminal', 58 + 'Default Window Settings', 59 + '-string', 60 + 'Profile', 61 + ]); 62 + 63 + const [command2, args2] = childProcess.execFile.mock.calls[1].arguments; 64 + assert.equal(command2, 'defaults'); 65 + assert.deepEqual(args2, [ 66 + 'write', 67 + 'com.apple.Terminal', 68 + 'Startup Window Settings', 69 + '-string', 70 + 'Profile', 71 + ]); 72 + }); 73 + });
+71
test/set-terminal-profile.js
··· 1 + import {beforeEach, describe, it, mock} from 'node:test'; 2 + import assert from 'node:assert/strict'; 3 + 4 + const runAppleScript = mock.fn(); 5 + mock.module('run-applescript', {namedExports: {runAppleScript}}); 6 + 7 + const isTerminalRunning = mock.fn(); 8 + mock.module('../source/is-terminal-running.js', { 9 + defaultExport: isTerminalRunning, 10 + }); 11 + 12 + const setTerminalDefaultProfile = mock.fn(); 13 + mock.module('../source/set-terminal-default-profile.js', { 14 + defaultExport: setTerminalDefaultProfile, 15 + }); 16 + 17 + const {default: setTerminalProfile} = 18 + await import('../source/set-terminal-profile.js'); 19 + 20 + describe('setTerminalProfile', () => { 21 + beforeEach(() => { 22 + runAppleScript.mock.resetCalls(); 23 + isTerminalRunning.mock.resetCalls(); 24 + setTerminalDefaultProfile.mock.resetCalls(); 25 + }); 26 + 27 + it('updates tabs via AppleScript when Terminal is running', async () => { 28 + isTerminalRunning.mock.mockImplementation(async () => true); 29 + runAppleScript.mock.mockImplementation(async () => undefined); 30 + 31 + await setTerminalProfile({profile: 'Profile', setDefault: false}); 32 + 33 + assert.equal( 34 + runAppleScript.mock.calls[0].arguments[0], 35 + [ 36 + 'tell application "Terminal"', 37 + '\tset current settings of tabs of windows to settings set "Profile"', 38 + 'end tell', 39 + ].join('\n'), 40 + ); 41 + }); 42 + 43 + it('does not call AppleScript when Terminal is not running', async () => { 44 + isTerminalRunning.mock.mockImplementation(async () => false); 45 + 46 + await setTerminalProfile({profile: 'Profile', setDefault: false}); 47 + 48 + assert.equal(runAppleScript.mock.callCount(), 0); 49 + }); 50 + 51 + it('sets default profile when setDefault is true', async () => { 52 + isTerminalRunning.mock.mockImplementation(async () => false); 53 + setTerminalDefaultProfile.mock.mockImplementation(async () => undefined); 54 + 55 + await setTerminalProfile({profile: 'Profile', setDefault: true}); 56 + 57 + assert.equal(setTerminalDefaultProfile.mock.callCount(), 1); 58 + assert.equal( 59 + setTerminalDefaultProfile.mock.calls[0].arguments[0], 60 + 'Profile', 61 + ); 62 + }); 63 + 64 + it('does not set default profile when setDefault is falsy', async () => { 65 + isTerminalRunning.mock.mockImplementation(async () => false); 66 + 67 + await setTerminalProfile({profile: 'Profile', setDefault: false}); 68 + 69 + assert.equal(setTerminalDefaultProfile.mock.callCount(), 0); 70 + }); 71 + });