keyfleur/tests/index.test.js
google-labs-jules[bot] 06d8a3bcc3 feat: Rewrite Keyfleur in TypeScript for npm publishing
This commit completes the full rewrite of the Keyfleur application from Python to TypeScript.

Key changes include:
- Core logic (themes, syllable generation, key generation modes) ported to TypeScript.
- CLI interface implemented using yargs.
- Comprehensive unit tests added using Jest, covering core functions and basic CLI operations.
- README.md updated with new installation (npm), usage instructions, and development guide.
- package.json configured for npm publishing, including:
    - Package name: "keyfleur"
    - Version: "1.0.0"
    - CLI command: "keyfleur"
    - Build and test scripts, including "prepublishOnly".
    - Essential metadata for npm.

The application is now structured as a modern TypeScript package, ready for building, testing, and publishing to npm.
2025-05-25 05:02:04 +00:00

79 lines
4.3 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../src/index");
const modes_1 = require("../src/modes"); // Import haiku for fallback check
const themes_1 = require("../src/themes");
describe('index.ts', () => {
describe('poetic_key', () => {
it('should return a non-empty string with default arguments (haiku, haiku)', () => {
const key = (0, index_1.poetic_key)();
expect(typeof key).toBe('string');
expect(key.length).toBeGreaterThan(0);
// Check if it looks like a haiku (three parts)
expect(key.split('-').length).toBe(3);
});
it('should use the specified mode and theme', () => {
const theme = 'nocturnal';
const mode = 'seed';
const key = (0, index_1.poetic_key)(mode, theme);
expect(typeof key).toBe('string');
expect(key.length).toBeGreaterThan(0);
// Seed format: Word-xxxx (hex)
const parts = key.split('-');
expect(parts.length).toBe(2);
expect(themes_1.THEMES[theme]).toContain(parts[0].toLowerCase().slice(0, 4)); // Check if first part is from theme
expect(parts[1]).toMatch(/^[0-9a-f]{4}$/);
});
it('should use the specified mode and default theme if theme is invalid/not provided', () => {
const mode = 'sonnet';
// @ts-expect-error testing invalid theme
const key = (0, index_1.poetic_key)(mode, "invalidTheme");
expect(typeof key).toBe('string');
expect(key.length).toBeGreaterThan(0);
// Sonnet format: WordAB-WordCD
const parts = key.split('-');
expect(parts.length).toBe(2);
expect(parts[0]).toMatch(/^[A-Z][a-z]+[a-z]{2}$/);
expect(parts[1]).toMatch(/^[A-Z][a-z]+[a-z]{2}$/);
// Check if words are from the default 'haiku' theme
const defaultThemeWords = themes_1.THEMES['haiku'];
expect(defaultThemeWords).toContain(parts[0].slice(0, -2).toLowerCase());
expect(defaultThemeWords).toContain(parts[1].slice(0, -2).toLowerCase());
});
it('should fall back to "haiku" mode if an invalid mode is specified', () => {
const theme = 'celestial';
// @ts-expect-error testing invalid mode
const keyWithInvalidMode = (0, index_1.poetic_key)('invalidMode', theme);
const keyWithHaikuMode = (0, modes_1.haiku)(theme); // Generate a haiku with the same theme for structure comparison
expect(typeof keyWithInvalidMode).toBe('string');
expect(keyWithInvalidMode.length).toBeGreaterThan(0);
// Check if it looks like a haiku (three parts)
expect(keyWithInvalidMode.split('-').length).toBe(3);
// This test is tricky because haiku generation is random.
// We can't expect keyWithInvalidMode to be *identical* to keyWithHaikuMode.
// We primarily check that it *behaves* like haiku mode (e.g. structure).
// The fact that it doesn't error out and produces a structured output is key.
});
it('should correctly call mirrora when mode is "mirrora" (no theme)', () => {
const key = (0, index_1.poetic_key)('mirrora');
expect(typeof key).toBe('string');
expect(key.length).toBeGreaterThan(0);
const parts = key.split('-');
expect(parts.length).toBe(2);
expect(parts[0].split('').reverse().join('')).toBe(parts[1]); // yx-xy
});
it('should correctly call a mode that expects a theme when mode and theme are provided', () => {
const key = (0, index_1.poetic_key)('lace', 'oceanic');
expect(typeof key).toBe('string');
expect(key.length).toBeGreaterThan(0);
const parts = key.split('-');
expect(parts.length).toBe(2);
const word = parts[0].slice(0, -2);
const midSyllable = parts[0].slice(-2);
expect(themes_1.THEMES['oceanic']).toContain(word.toLowerCase());
expect(midSyllable.split('').reverse().join('')).toBe(parts[1].slice(0, 2));
expect(word.split('').reverse().join('')).toBe(parts[1].slice(2));
});
});
});