diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 461b4d4..d126a85 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,6 +28,9 @@ jobs: - name: Lint run: npm run lint + - name: Test + run: npm test + - name: Type check run: npx tsc --noEmit diff --git a/package-lock.json b/package-lock.json index 97eab99..7ac6845 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,7 +38,11 @@ "globals": "^16.5.0", "typescript": "~5.9.3", "typescript-eslint": "^8.48.0", - "vite": "^7.3.1" + "vite": "^7.3.1", + "vitest": "^4.0.18" + }, + "engines": { + "node": ">=18" } }, "node_modules/@babel/code-frame": { @@ -1918,6 +1922,13 @@ "win32" ] }, + "node_modules/@standard-schema/spec": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz", + "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==", + "dev": true, + "license": "MIT" + }, "node_modules/@tailwindcss/node": { "version": "4.1.18", "resolved": "https://registry.npmjs.org/@tailwindcss/node/-/node-4.1.18.tgz", @@ -2220,6 +2231,17 @@ "@babel/types": "^7.28.2" } }, + "node_modules/@types/chai": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-5.2.3.tgz", + "integrity": "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/deep-eql": "*", + "assertion-error": "^2.0.1" + } + }, "node_modules/@types/debug": { "version": "4.1.12", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.12.tgz", @@ -2229,6 +2251,13 @@ "@types/ms": "*" } }, + "node_modules/@types/deep-eql": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/deep-eql/-/deep-eql-4.0.2.tgz", + "integrity": "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/estree": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.8.tgz", @@ -2606,6 +2635,117 @@ "vite": "^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0" } }, + "node_modules/@vitest/expect": { + "version": "4.0.18", + "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-4.0.18.tgz", + "integrity": "sha512-8sCWUyckXXYvx4opfzVY03EOiYVxyNrHS5QxX3DAIi5dpJAAkyJezHCP77VMX4HKA2LDT/Jpfo8i2r5BE3GnQQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@standard-schema/spec": "^1.0.0", + "@types/chai": "^5.2.2", + "@vitest/spy": "4.0.18", + "@vitest/utils": "4.0.18", + "chai": "^6.2.1", + "tinyrainbow": "^3.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/mocker": { + "version": "4.0.18", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-4.0.18.tgz", + "integrity": "sha512-HhVd0MDnzzsgevnOWCBj5Otnzobjy5wLBe4EdeeFGv8luMsGcYqDuFRMcttKWZA5vVO8RFjexVovXvAM4JoJDQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/spy": "4.0.18", + "estree-walker": "^3.0.3", + "magic-string": "^0.30.21" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "msw": "^2.4.9", + "vite": "^6.0.0 || ^7.0.0-0" + }, + "peerDependenciesMeta": { + "msw": { + "optional": true + }, + "vite": { + "optional": true + } + } + }, + "node_modules/@vitest/pretty-format": { + "version": "4.0.18", + "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-4.0.18.tgz", + "integrity": "sha512-P24GK3GulZWC5tz87ux0m8OADrQIUVDPIjjj65vBXYG17ZeU3qD7r+MNZ1RNv4l8CGU2vtTRqixrOi9fYk/yKw==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyrainbow": "^3.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/runner": { + "version": "4.0.18", + "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-4.0.18.tgz", + "integrity": "sha512-rpk9y12PGa22Jg6g5M3UVVnTS7+zycIGk9ZNGN+m6tZHKQb7jrP7/77WfZy13Y/EUDd52NDsLRQhYKtv7XfPQw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/utils": "4.0.18", + "pathe": "^2.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/snapshot": { + "version": "4.0.18", + "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-4.0.18.tgz", + "integrity": "sha512-PCiV0rcl7jKQjbgYqjtakly6T1uwv/5BQ9SwBLekVg/EaYeQFPiXcgrC2Y7vDMA8dM1SUEAEV82kgSQIlXNMvA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "4.0.18", + "magic-string": "^0.30.21", + "pathe": "^2.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/spy": { + "version": "4.0.18", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-4.0.18.tgz", + "integrity": "sha512-cbQt3PTSD7P2OARdVW3qWER5EGq7PHlvE+QfzSC0lbwO+xnt7+XH06ZzFjFRgzUX//JmpxrCu92VdwvEPlWSNw==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/utils": { + "version": "4.0.18", + "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-4.0.18.tgz", + "integrity": "sha512-msMRKLMVLWygpK3u2Hybgi4MNjcYJvwTb0Ru09+fOyCXIgT5raYP041DRRdiJiI3k/2U6SEbAETB3YtBrUkCFA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "4.0.18", + "tinyrainbow": "^3.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, "node_modules/acorn": { "version": "8.15.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.15.0.tgz", @@ -2669,6 +2809,16 @@ "dev": true, "license": "Python-2.0" }, + "node_modules/assertion-error": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", + "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + } + }, "node_modules/bail": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/bail/-/bail-2.0.2.tgz", @@ -2782,6 +2932,16 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/chai": { + "version": "6.2.2", + "resolved": "https://registry.npmjs.org/chai/-/chai-6.2.2.tgz", + "integrity": "sha512-NUPRluOfOiTKBKvWPtSD4PhFvWCqOi0BGStNWs57X9js7XGTprSmFoz5F0tWhR4WPjNeR9jXqdC7/UpSJTnlRg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -3013,6 +3173,13 @@ "node": ">=10.13.0" } }, + "node_modules/es-module-lexer": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.7.0.tgz", + "integrity": "sha512-jEQoCwk8hyb2AZziIOLhDqpm5+2ww5uIE6lkO/6jcOCusfk6LhMHpXXfBLXTZ7Ydyt0j4VoUQv6uGNYbdW+kBA==", + "dev": true, + "license": "MIT" + }, "node_modules/esbuild": { "version": "0.27.3", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.3.tgz", @@ -3261,6 +3428,16 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/estree-walker": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", + "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.0" + } + }, "node_modules/esutils": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", @@ -3271,6 +3448,16 @@ "node": ">=0.10.0" } }, + "node_modules/expect-type": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.3.0.tgz", + "integrity": "sha512-knvyeauYhqjOYvQ66MznSMs83wmHrCycNEN6Ao+2AeYEfxUIkuiVxdEa1qlGEPK+We3n0THiDciYSsCcgW/DoA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -5011,6 +5198,17 @@ "dev": true, "license": "MIT" }, + "node_modules/obug": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/obug/-/obug-2.1.1.tgz", + "integrity": "sha512-uTqF9MuPraAQ+IsnPf366RG4cP9RtUi7MLO1N3KEc+wb0a6yKpeL0lmk2IB1jY5KHPAlTc6T/JRdC/YqxHNwkQ==", + "dev": true, + "funding": [ + "https://github.com/sponsors/sxzz", + "https://opencollective.com/debug" + ], + "license": "MIT" + }, "node_modules/optionator": { "version": "0.9.4", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.4.tgz", @@ -5119,6 +5317,13 @@ "node": ">=8" } }, + "node_modules/pathe": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz", + "integrity": "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==", + "dev": true, + "license": "MIT" + }, "node_modules/picocolors": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", @@ -5444,6 +5649,13 @@ "node": ">=8" } }, + "node_modules/siginfo": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/siginfo/-/siginfo-2.0.0.tgz", + "integrity": "sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==", + "dev": true, + "license": "ISC" + }, "node_modules/source-map-js": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", @@ -5463,6 +5675,20 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/stackback": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/stackback/-/stackback-0.0.2.tgz", + "integrity": "sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==", + "dev": true, + "license": "MIT" + }, + "node_modules/std-env": { + "version": "3.10.0", + "resolved": "https://registry.npmjs.org/std-env/-/std-env-3.10.0.tgz", + "integrity": "sha512-5GS12FdOZNliM5mAOxFRg7Ir0pWz8MdpYm6AY6VPkGpbA7ZzmbzNcBJQ0GPvvyWgcY7QAhCgf9Uy89I03faLkg==", + "dev": true, + "license": "MIT" + }, "node_modules/stringify-entities": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/stringify-entities/-/stringify-entities-4.0.4.tgz", @@ -5550,6 +5776,23 @@ "url": "https://opencollective.com/webpack" } }, + "node_modules/tinybench": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.9.0.tgz", + "integrity": "sha512-0+DUvqWMValLmha6lr4kD8iAMK1HzV0/aKnCtWb9v9641TnP/MFb7Pc2bxoxQjTXAErryXVgUOfv2YqNllqGeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinyexec": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.0.2.tgz", + "integrity": "sha512-W/KYk+NFhkmsYpuHq5JykngiOCnxeVL8v8dFnqxSD8qEEdRfXk1SDM6JzNqcERbcGYj9tMrDQBYV9cjgnunFIg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/tinyglobby": { "version": "0.2.15", "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.15.tgz", @@ -5566,6 +5809,16 @@ "url": "https://github.com/sponsors/SuperchupuDev" } }, + "node_modules/tinyrainbow": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-3.0.3.tgz", + "integrity": "sha512-PSkbLUoxOFRzJYjjxHJt9xro7D+iilgMX/C9lawzVuYiIdcihh9DXmVibBe8lmcFrRi/VzlPjBxbN7rH24q8/Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/trim-lines": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/trim-lines/-/trim-lines-3.0.1.tgz", @@ -5901,6 +6154,84 @@ } } }, + "node_modules/vitest": { + "version": "4.0.18", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-4.0.18.tgz", + "integrity": "sha512-hOQuK7h0FGKgBAas7v0mSAsnvrIgAvWmRFjmzpJ7SwFHH3g1k2u37JtYwOwmEKhK6ZO3v9ggDBBm0La1LCK4uQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/expect": "4.0.18", + "@vitest/mocker": "4.0.18", + "@vitest/pretty-format": "4.0.18", + "@vitest/runner": "4.0.18", + "@vitest/snapshot": "4.0.18", + "@vitest/spy": "4.0.18", + "@vitest/utils": "4.0.18", + "es-module-lexer": "^1.7.0", + "expect-type": "^1.2.2", + "magic-string": "^0.30.21", + "obug": "^2.1.1", + "pathe": "^2.0.3", + "picomatch": "^4.0.3", + "std-env": "^3.10.0", + "tinybench": "^2.9.0", + "tinyexec": "^1.0.2", + "tinyglobby": "^0.2.15", + "tinyrainbow": "^3.0.3", + "vite": "^6.0.0 || ^7.0.0", + "why-is-node-running": "^2.3.0" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^20.0.0 || ^22.0.0 || >=24.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@opentelemetry/api": "^1.9.0", + "@types/node": "^20.0.0 || ^22.0.0 || >=24.0.0", + "@vitest/browser-playwright": "4.0.18", + "@vitest/browser-preview": "4.0.18", + "@vitest/browser-webdriverio": "4.0.18", + "@vitest/ui": "4.0.18", + "happy-dom": "*", + "jsdom": "*" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@opentelemetry/api": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser-playwright": { + "optional": true + }, + "@vitest/browser-preview": { + "optional": true + }, + "@vitest/browser-webdriverio": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + } + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -5917,6 +6248,23 @@ "node": ">= 8" } }, + "node_modules/why-is-node-running": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-2.3.0.tgz", + "integrity": "sha512-hUrmaWBdVDcxvYqnyh09zunKzROWjbZTiNy8dBEjkS7ehEDQibXJ7XvlmtbwuTclUiIyN+CyXQD4Vmko8fNm8w==", + "dev": true, + "license": "MIT", + "dependencies": { + "siginfo": "^2.0.0", + "stackback": "0.0.2" + }, + "bin": { + "why-is-node-running": "cli.js" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/word-wrap": { "version": "1.2.5", "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", diff --git a/package.json b/package.json index 5a9a0ca..b94a417 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,8 @@ "dev": "vite", "build": "tsc -b && vite build", "lint": "eslint .", + "test": "vitest run", + "test:watch": "vitest", "preview": "vite preview", "lint:fix": "eslint . --fix" }, @@ -63,7 +65,8 @@ "globals": "^16.5.0", "typescript": "~5.9.3", "typescript-eslint": "^8.48.0", - "vite": "^7.3.1" + "vite": "^7.3.1", + "vitest": "^4.0.18" }, "engines": { "node": ">=18" diff --git a/src/lib/__tests__/exportChat.test.ts b/src/lib/__tests__/exportChat.test.ts new file mode 100644 index 0000000..c8e113b --- /dev/null +++ b/src/lib/__tests__/exportChat.test.ts @@ -0,0 +1,84 @@ +import { describe, it, expect, vi } from 'vitest'; +import { messagesToMarkdown } from '../exportChat'; +import type { ChatMessage } from '../../types'; + +function makeMessage(overrides: Partial = {}): ChatMessage { + return { + id: '1', + role: 'user', + content: 'Hello world', + timestamp: new Date('2026-01-15T10:30:00Z').getTime(), + blocks: [], + isSystemEvent: false, + ...overrides, + } as ChatMessage; +} + +describe('messagesToMarkdown', () => { + it('includes session label as heading', () => { + const md = messagesToMarkdown([], 'Test Session'); + expect(md).toContain('# Test Session'); + }); + + it('includes export timestamp', () => { + const md = messagesToMarkdown([]); + expect(md).toContain('Exported from PinchChat on'); + }); + + it('labels user messages with 👤 User', () => { + const md = messagesToMarkdown([makeMessage({ role: 'user' })]); + expect(md).toContain('👤 User'); + }); + + it('labels assistant messages with 🤖 Assistant', () => { + const md = messagesToMarkdown([makeMessage({ role: 'assistant' })]); + expect(md).toContain('🤖 Assistant'); + }); + + it('labels system events with ⚙️ System Event', () => { + const md = messagesToMarkdown([makeMessage({ role: 'user', isSystemEvent: true })]); + expect(md).toContain('⚙️ System Event'); + }); + + it('renders text blocks', () => { + const md = messagesToMarkdown([makeMessage({ + content: '', + blocks: [{ type: 'text', text: 'Some content here' }], + })]); + expect(md).toContain('Some content here'); + }); + + it('renders tool_use blocks with name and input', () => { + const md = messagesToMarkdown([makeMessage({ + role: 'assistant', + content: '', + blocks: [{ type: 'tool_use', name: 'exec', input: { command: 'ls' } }], + })]); + expect(md).toContain('`exec`'); + expect(md).toContain('"command": "ls"'); + }); + + it('falls back to content when blocks are empty', () => { + const md = messagesToMarkdown([makeMessage({ content: 'Fallback text', blocks: [] })]); + expect(md).toContain('Fallback text'); + }); + + it('renders image blocks as placeholder', () => { + const md = messagesToMarkdown([makeMessage({ + content: '', + blocks: [{ type: 'image', mediaType: 'image/png' }], + })]); + expect(md).toContain('*[Image]*'); + }); + + it('wraps thinking blocks in details tags', () => { + vi.stubGlobal('Date', globalThis.Date); + const md = messagesToMarkdown([makeMessage({ + role: 'assistant', + content: '', + blocks: [{ type: 'thinking', text: 'Let me think...' }], + })]); + expect(md).toContain('
💭 Thinking'); + expect(md).toContain('Let me think...'); + }); +}); diff --git a/src/lib/__tests__/relativeTime.test.ts b/src/lib/__tests__/relativeTime.test.ts new file mode 100644 index 0000000..3ddaf1a --- /dev/null +++ b/src/lib/__tests__/relativeTime.test.ts @@ -0,0 +1,57 @@ +import { describe, it, expect, vi, afterEach } from 'vitest'; +import { relativeTime } from '../relativeTime'; + +describe('relativeTime', () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + it('returns null for undefined', () => { + expect(relativeTime(undefined)).toBeNull(); + }); + + it('returns null for 0', () => { + expect(relativeTime(0)).toBeNull(); + }); + + it('returns "<1m" for timestamps less than 60s ago', () => { + const now = Date.now(); + vi.spyOn(Date, 'now').mockReturnValue(now); + expect(relativeTime(now - 30_000)).toBe('<1m'); + expect(relativeTime(now)).toBe('<1m'); + }); + + it('returns minutes for 1-59 minutes ago', () => { + const now = Date.now(); + vi.spyOn(Date, 'now').mockReturnValue(now); + expect(relativeTime(now - 60_000)).toBe('1m'); + expect(relativeTime(now - 45 * 60_000)).toBe('45m'); + }); + + it('returns hours for 1-23 hours ago', () => { + const now = Date.now(); + vi.spyOn(Date, 'now').mockReturnValue(now); + expect(relativeTime(now - 3_600_000)).toBe('1h'); + expect(relativeTime(now - 23 * 3_600_000)).toBe('23h'); + }); + + it('returns days for 1-29 days ago', () => { + const now = Date.now(); + vi.spyOn(Date, 'now').mockReturnValue(now); + expect(relativeTime(now - 86_400_000)).toBe('1d'); + expect(relativeTime(now - 7 * 86_400_000)).toBe('7d'); + }); + + it('returns months for 30+ days ago', () => { + const now = Date.now(); + vi.spyOn(Date, 'now').mockReturnValue(now); + expect(relativeTime(now - 30 * 86_400_000)).toBe('1mo'); + expect(relativeTime(now - 90 * 86_400_000)).toBe('3mo'); + }); + + it('clamps negative diff to 0 (future timestamps)', () => { + const now = Date.now(); + vi.spyOn(Date, 'now').mockReturnValue(now); + expect(relativeTime(now + 60_000)).toBe('<1m'); + }); +}); diff --git a/src/lib/__tests__/sessionName.test.ts b/src/lib/__tests__/sessionName.test.ts new file mode 100644 index 0000000..6cf1d2a --- /dev/null +++ b/src/lib/__tests__/sessionName.test.ts @@ -0,0 +1,61 @@ +import { describe, it, expect } from 'vitest'; +import { sessionDisplayName } from '../sessionName'; +import type { Session } from '../../types'; + +function makeSession(overrides: Partial = {}): Session { + return { + key: 'agent:main:test', + kind: 'main', + channel: '', + lastActivity: Date.now(), + ...overrides, + } as Session; +} + +describe('sessionDisplayName', () => { + it('returns label when set', () => { + expect(sessionDisplayName(makeSession({ label: 'My Task' }))).toBe('My Task'); + }); + + it('returns "Main" for main sessions without channel', () => { + expect(sessionDisplayName(makeSession({ kind: 'main', channel: '' }))).toBe('Main'); + }); + + it('returns "Main · Discord" for main sessions with channel', () => { + expect(sessionDisplayName(makeSession({ kind: 'main', channel: 'discord' }))).toBe('Main · Discord'); + }); + + it('returns "Cron" for cron sessions without channel', () => { + expect(sessionDisplayName(makeSession({ kind: 'cron', channel: '' }))).toBe('Cron'); + }); + + it('returns "Cron · Telegram" for cron sessions with channel', () => { + expect(sessionDisplayName(makeSession({ kind: 'cron', channel: 'telegram' }))).toBe('Cron · Telegram'); + }); + + it('returns "Task · Discord" for isolated sessions', () => { + expect(sessionDisplayName(makeSession({ kind: 'isolated', channel: 'discord' }))).toBe('Task · Discord'); + }); + + it('returns capitalized channel as fallback', () => { + expect(sessionDisplayName(makeSession({ kind: undefined as unknown as string, channel: 'slack' } as Partial))).toBe('Slack'); + }); + + it('truncates UUID session keys', () => { + const name = sessionDisplayName(makeSession({ + kind: undefined as unknown as string, + channel: '', + key: 'agent:main:a1b2c3d4-e5f6-7890-abcd-ef1234567890', + } as Partial)); + expect(name).toBe('a1b2c3d4…'); + }); + + it('strips agent prefix from non-UUID keys', () => { + const name = sessionDisplayName(makeSession({ + kind: undefined as unknown as string, + channel: '', + key: 'agent:bot:mycustomkey', + } as Partial)); + expect(name).toBe('mycustomkey'); + }); +});