Files
TORCH/test/utils.test.mjs
T
google-labs-jules[bot]andPR0M3TH3AN ba5d86142c Refactor: Move ensureDir to src/utils.mjs
Moved the `ensureDir` utility function from `src/ops.mjs` to `src/utils.mjs` to promote code reuse and centralization.
Updated `src/ops.mjs` and `src/services/memory/index.js` to use the shared utility.
Added unit tests for `ensureDir` in `test/utils.test.mjs`.

Co-authored-by: PR0M3TH3AN <[email protected]>
2026-02-18 03:28:09 +00:00

71 lines
2.5 KiB
JavaScript

import { describe, it, after } from 'node:test';
import assert from 'node:assert';
import fs from 'node:fs';
import path from 'node:path';
import { getIsoWeekStr, ensureDir } from '../src/utils.mjs';
describe('Date Utilities', () => {
describe('getIsoWeekStr', () => {
it('returns correct ISO week for simple cases', () => {
assert.strictEqual(getIsoWeekStr('2023-01-02'), '2023-W01'); // Mon
assert.strictEqual(getIsoWeekStr('2023-01-08'), '2023-W01'); // Sun
assert.strictEqual(getIsoWeekStr('2023-01-09'), '2023-W02'); // Mon
});
it('handles year boundaries correctly', () => {
// 2023-01-01 is Sunday. Belong to last week of 2022.
assert.strictEqual(getIsoWeekStr('2023-01-01'), '2022-W52');
// 2024-01-01 is Monday. Week 1 of 2024.
assert.strictEqual(getIsoWeekStr('2024-01-01'), '2024-W01');
// 2023-12-31 is Sunday. Week 52 of 2023.
assert.strictEqual(getIsoWeekStr('2023-12-31'), '2023-W52');
});
it('handles leap years', () => {
assert.strictEqual(getIsoWeekStr('2024-02-29'), '2024-W09');
});
it('handles empty input (defaults to today)', () => {
// Just check it returns a string in correct format
const result = getIsoWeekStr();
assert.match(result, /^\d{4}-W\d{2}$/);
});
it('handles invalid input gracefully', () => {
assert.strictEqual(getIsoWeekStr('invalid-date'), '');
});
});
});
describe('File Utilities', () => {
describe('ensureDir', () => {
const tempBase = fs.mkdtempSync(path.join(process.cwd(), 'test-utils-'));
after(() => {
fs.rmSync(tempBase, { recursive: true, force: true });
});
it('creates directory if it does not exist', () => {
const targetDir = path.join(tempBase, 'new-dir');
assert.strictEqual(fs.existsSync(targetDir), false);
ensureDir(targetDir);
assert.strictEqual(fs.existsSync(targetDir), true);
});
it('does nothing if directory already exists', () => {
const targetDir = path.join(tempBase, 'existing-dir');
fs.mkdirSync(targetDir);
assert.strictEqual(fs.existsSync(targetDir), true);
ensureDir(targetDir);
assert.strictEqual(fs.existsSync(targetDir), true);
});
it('creates nested directories recursively', () => {
const targetDir = path.join(tempBase, 'nested', 'deep', 'dir');
assert.strictEqual(fs.existsSync(targetDir), false);
ensureDir(targetDir);
assert.strictEqual(fs.existsSync(targetDir), true);
});
});
});