Skip to content

Add Node test suite #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions tests/index.js

This file was deleted.

47 changes: 47 additions & 0 deletions tests/machine-id.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const assert = require('assert');
const {test} = require('node:test');
const {createHash} = require('crypto');
const {machineId, machineIdSync} = require('../dist/index');

const platform = process.platform;
const originalPattern = {
darwin: /^[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}$/,
win32: /^[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}$/,
linux: /^[0-9A-Za-z]{32}$/,
freebsd: /^[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}$/
};
const hashPattern = /^[0-9A-Za-z]{64}$/;

test('machineIdSync(true) returns original unique id', () => {
const id = machineIdSync(true);
assert.match(id, originalPattern[platform]);
});

test('machineId(true) returns the same original id', async () => {
const asyncId = await machineId(true);
const syncId = machineIdSync(true);
assert.strictEqual(asyncId, syncId);
assert.match(asyncId, originalPattern[platform]);
});

test('machineIdSync() returns sha256 hash of the original id', () => {
const original = machineIdSync(true);
const expected = createHash('sha256').update(original).digest('hex');
const hashed = machineIdSync();
assert.match(hashed, hashPattern);
assert.strictEqual(hashed, expected);
});

test('machineId() returns sha256 hash of the original id', async () => {
const original = await machineId(true);
const expected = createHash('sha256').update(original).digest('hex');
const hashed = await machineId();
assert.match(hashed, hashPattern);
assert.strictEqual(hashed, expected);
});

test('CommonJS import exposes machineId functions', () => {
const mod = require('../dist/index');
assert.strictEqual(typeof mod.machineId, 'function');
assert.strictEqual(typeof mod.machineIdSync, 'function');
});