Skip to content

Commit

Permalink
feat: progress on v3 update
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Jul 22, 2024
1 parent f4cbe31 commit ec7b15f
Show file tree
Hide file tree
Showing 50 changed files with 526 additions and 955 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib"
}
"typescript.tsdk": "node_modules\\typescript\\lib"
}
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ client.onDisconnect(() => console.log('Disconnected!'));
client.onError(() => console.log('Connection error!'));

// Get monitors, active workspaces, and windows.
const monitors = await client.getMonitors();
const workspaces = await client.getWorkspaces();
const windows = await client.getWindows();
const { monitors } = await client.queryMonitors();
const { workspaces } = await client.queryWorkspaces();
const { windows } = await client.queryWindows();

// Run a WM command.
await client.runCommand('focus workspace 1');
await client.runCommand('focus --workspace 1');

// Run a WM command with a given context container. This way we can target the
// container to operate on. If no context container is specified, it defaults to
// the currently focused container.
await client.runCommand('move left', windows[0]);
// Run a WM command with a given subject container. This way we can target
// the container to operate on. If no subject container is specified, it
// defaults to the currently focused container.
await client.runCommand('move --direction left', windows[0].id);

// Listen to a WM event (e.g. whenever the focused container changes).
await client.subscribe(
WmEventType.FocusChanged,
WmEventType.FOCUS_CHANGED,
(event: FocusChangedEvent) => console.log(event),
);

// Listen to multiple WM events.
await client.subscribeMany(
[WmEventType.WorkspaceActivated, WmEventType.WorkspaceDeactivated],
[WmEventType.WORKSPACE_ACTIVATED, WmEventType.WORKSPACE_DEACTIVATED],
(event: WorkspaceActivatedEvent | WorkspaceDeactivatedEvent) =>
console.log(event),
);
Expand Down
605 changes: 221 additions & 384 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"prettier": "3.0.1",
"tsup": "6.7.0",
"typescript": "5.1.6",
"vitest": "^1.6.0",
"vitest": "2.0.4",
"ws": "8.13.0"
},
"peerDependencies": {
Expand Down
95 changes: 25 additions & 70 deletions src/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,55 @@
import { spawn, exec } from 'child_process';

import { WmClient } from './client';

function delay(ms: number) {
/**
* Promise-based alternative to `setTimeout`.
*/
function wait(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

/**
* Whether the process with the given PID is still running.
*/
async function isProcessRunning(pid: number) {
return await new Promise((resolve, reject) =>
exec(`tasklist /fi "PID eq ${pid}"`, (err, stdout, stderr) => {
exec(`tasklist /fi "PID eq ${pid}"`, (err, stdout, _) => {
if (err) {
console.error(err);
return reject(err);
}
if (stdout.includes(pid.toString())) {
console.log('Process is still running');
console.log('Process is still running.');
return resolve(true);
} else {
console.log('Process has been terminated');
console.log('Process has been terminated.');
return resolve(false);
}
}),
);
}

const INITIAL_CONNECT_TIMEOUT = 5000;
function timeout(duration: number) {
return new Promise((_, reject) => setTimeout(() => reject(), duration));
}

describe.sequential('[CLIENT]', async () => {
let client: WmClient;
client = new WmClient();
beforeAll(async () => {
// wait for connection or timeout to ensure wm is running
await Promise.race([
client.connect(),
timeout(INITIAL_CONNECT_TIMEOUT),
]);
}, 4000);
const client = new WmClient();

// Wait for connection or timeout to ensure WM is running.
beforeAll(() => client.connect(), 5000);

describe('(query)', () => {
it.concurrent('monitors', async () => {
const { monitors } = await client.getMonitors();
expect(monitors?.length).toBeGreaterThan(0);
expect(monitors.length).toBeGreaterThan(0);
});

it.concurrent('windows', async () => {
const { windows } = await client.getWindows();
expect(windows?.length).toBeGreaterThan(0);
expect(windows.length).toBeGreaterThan(0);
});

it.concurrent('workspaces', async () => {
const { workspaces } = await client.getWorkspaces();
expect(workspaces?.length).toBeGreaterThan(0);
expect(workspaces.length).toBeGreaterThan(0);
});

it.concurrent('focused container', async () => {
Expand All @@ -62,53 +59,20 @@ describe.sequential('[CLIENT]', async () => {

it.concurrent('binding mode', async () => {
const { bindingModes } = await client.getBindingModes();
// TODO: how do I trigger this at all?
// extend tests when populatable
// console.log(bindingModes);
expect(bindingModes).toBeDefined();
expect(Array.isArray(bindingModes)).toBe(true);
});
});

describe('(command)', () => {
describe('adjust-border', async () => {
afterAll(async () => {
// reset border changes
await client.adjustBorders({
top: '0',
right: '0',
bottom: '0',
left: '0',
});
});
it('top border', async () => {
await client.adjustBorders({ top: '10px' });
});
it('right border', async () => {
await client.adjustBorders({ right: '20px' });
});
it('bottom border', async () => {
await client.adjustBorders({ bottom: '30px' });
});
it('left border', async () => {
await client.adjustBorders({ left: '40px' });
});
it('all borders', async () => {
await client.adjustBorders({
top: '10px',
right: '20px',
bottom: '30px',
left: '40px',
});
});
it('with mixed units', async () => {
await client.adjustBorders({
top: '10px',
right: '20',
bottom: '30px',
left: '40%',
});
// Reset border changes.
await client.runCommand(
'adjust-borders --top="0px" --right="0px" --bottom="0px" --left="0px"',
);
});
});

it.skip('close', async () => {
const process = spawn('cmd.exe', [], {
stdio: 'inherit',
Expand All @@ -119,7 +83,7 @@ describe.sequential('[CLIENT]', async () => {
process.unref();

// allow time to pass for the window to open, tile and get focus
await delay(4000).then(() => {
await wait(4000).then(() => {
if (!processId) {
throw Error('Cannot test close command without process pid.');
}
Expand Down Expand Up @@ -189,15 +153,6 @@ describe.sequential('[CLIENT]', async () => {
// clean up
await client.focusWorkspace(initialWorkspace);
}, 5000);
it('next_workspace', async () => {
await client.nextWorkspace();
});
it('prev_workspace', async () => {
await client.prevWorkspace();
});
it('recent_workspace', async () => {
await client.recentWorkspace();
});
});
});
});
Loading

0 comments on commit ec7b15f

Please sign in to comment.