feat: add timer engine, cascade system, urgency system with 40 passing tests
This commit is contained in:
parent
1b148757af
commit
6ac54d76fd
41
web/.gitignore
vendored
Normal file
41
web/.gitignore
vendored
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||||
|
|
||||||
|
# dependencies
|
||||||
|
/node_modules
|
||||||
|
/.pnp
|
||||||
|
.pnp.*
|
||||||
|
.yarn/*
|
||||||
|
!.yarn/patches
|
||||||
|
!.yarn/plugins
|
||||||
|
!.yarn/releases
|
||||||
|
!.yarn/versions
|
||||||
|
|
||||||
|
# testing
|
||||||
|
/coverage
|
||||||
|
|
||||||
|
# next.js
|
||||||
|
/.next/
|
||||||
|
/out/
|
||||||
|
|
||||||
|
# production
|
||||||
|
/build
|
||||||
|
|
||||||
|
# misc
|
||||||
|
.DS_Store
|
||||||
|
*.pem
|
||||||
|
|
||||||
|
# debug
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
.pnpm-debug.log*
|
||||||
|
|
||||||
|
# env files (can opt-in for committing if needed)
|
||||||
|
.env*
|
||||||
|
|
||||||
|
# vercel
|
||||||
|
.vercel
|
||||||
|
|
||||||
|
# typescript
|
||||||
|
*.tsbuildinfo
|
||||||
|
next-env.d.ts
|
||||||
36
web/README.md
Normal file
36
web/README.md
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
First, run the development server:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run dev
|
||||||
|
# or
|
||||||
|
yarn dev
|
||||||
|
# or
|
||||||
|
pnpm dev
|
||||||
|
# or
|
||||||
|
bun dev
|
||||||
|
```
|
||||||
|
|
||||||
|
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
||||||
|
|
||||||
|
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
|
||||||
|
|
||||||
|
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
|
||||||
|
|
||||||
|
## Learn More
|
||||||
|
|
||||||
|
To learn more about Next.js, take a look at the following resources:
|
||||||
|
|
||||||
|
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
|
||||||
|
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
||||||
|
|
||||||
|
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
|
||||||
|
|
||||||
|
## Deploy on Vercel
|
||||||
|
|
||||||
|
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
||||||
|
|
||||||
|
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
|
||||||
18
web/eslint.config.mjs
Normal file
18
web/eslint.config.mjs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { defineConfig, globalIgnores } from "eslint/config";
|
||||||
|
import nextVitals from "eslint-config-next/core-web-vitals";
|
||||||
|
import nextTs from "eslint-config-next/typescript";
|
||||||
|
|
||||||
|
const eslintConfig = defineConfig([
|
||||||
|
...nextVitals,
|
||||||
|
...nextTs,
|
||||||
|
// Override default ignores of eslint-config-next.
|
||||||
|
globalIgnores([
|
||||||
|
// Default ignores of eslint-config-next:
|
||||||
|
".next/**",
|
||||||
|
"out/**",
|
||||||
|
"build/**",
|
||||||
|
"next-env.d.ts",
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
|
||||||
|
export default eslintConfig;
|
||||||
7
web/next.config.ts
Normal file
7
web/next.config.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import type { NextConfig } from "next";
|
||||||
|
|
||||||
|
const nextConfig: NextConfig = {
|
||||||
|
/* config options here */
|
||||||
|
};
|
||||||
|
|
||||||
|
export default nextConfig;
|
||||||
8868
web/package-lock.json
generated
Normal file
8868
web/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
40
web/package.json
Normal file
40
web/package.json
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
{
|
||||||
|
"name": "web",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"dev": "next dev",
|
||||||
|
"build": "next build",
|
||||||
|
"start": "next start",
|
||||||
|
"lint": "eslint",
|
||||||
|
"test": "vitest run",
|
||||||
|
"test:watch": "vitest",
|
||||||
|
"typecheck": "tsc --noEmit"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"date-fns": "^4.1.0",
|
||||||
|
"idb": "^8.0.3",
|
||||||
|
"lucide-react": "^0.575.0",
|
||||||
|
"next": "16.1.6",
|
||||||
|
"react": "19.2.3",
|
||||||
|
"react-dom": "19.2.3",
|
||||||
|
"uuid": "^13.0.0",
|
||||||
|
"zod": "^4.3.6",
|
||||||
|
"zustand": "^5.0.11"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@tailwindcss/postcss": "^4",
|
||||||
|
"@testing-library/jest-dom": "^6.9.1",
|
||||||
|
"@testing-library/react": "^16.3.2",
|
||||||
|
"@types/node": "^20",
|
||||||
|
"@types/react": "^19",
|
||||||
|
"@types/react-dom": "^19",
|
||||||
|
"@types/uuid": "^10.0.0",
|
||||||
|
"eslint": "^9",
|
||||||
|
"eslint-config-next": "16.1.6",
|
||||||
|
"jsdom": "^28.1.0",
|
||||||
|
"tailwindcss": "^4",
|
||||||
|
"typescript": "^5",
|
||||||
|
"vitest": "^4.0.18"
|
||||||
|
}
|
||||||
|
}
|
||||||
7
web/postcss.config.mjs
Normal file
7
web/postcss.config.mjs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
const config = {
|
||||||
|
plugins: {
|
||||||
|
"@tailwindcss/postcss": {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default config;
|
||||||
1
web/public/file.svg
Normal file
1
web/public/file.svg
Normal file
@ -0,0 +1 @@
|
|||||||
|
<svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M14.5 13.5V5.41a1 1 0 0 0-.3-.7L9.8.29A1 1 0 0 0 9.08 0H1.5v13.5A2.5 2.5 0 0 0 4 16h8a2.5 2.5 0 0 0 2.5-2.5m-1.5 0v-7H8v-5H3v12a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1M9.5 5V2.12L12.38 5zM5.13 5h-.62v1.25h2.12V5zm-.62 3h7.12v1.25H4.5zm.62 3h-.62v1.25h7.12V11z" clip-rule="evenodd" fill="#666" fill-rule="evenodd"/></svg>
|
||||||
|
After Width: | Height: | Size: 391 B |
1
web/public/globe.svg
Normal file
1
web/public/globe.svg
Normal file
@ -0,0 +1 @@
|
|||||||
|
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><g clip-path="url(#a)"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.27 14.1a6.5 6.5 0 0 0 3.67-3.45q-1.24.21-2.7.34-.31 1.83-.97 3.1M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16m.48-1.52a7 7 0 0 1-.96 0H7.5a4 4 0 0 1-.84-1.32q-.38-.89-.63-2.08a40 40 0 0 0 3.92 0q-.25 1.2-.63 2.08a4 4 0 0 1-.84 1.31zm2.94-4.76q1.66-.15 2.95-.43a7 7 0 0 0 0-2.58q-1.3-.27-2.95-.43a18 18 0 0 1 0 3.44m-1.27-3.54a17 17 0 0 1 0 3.64 39 39 0 0 1-4.3 0 17 17 0 0 1 0-3.64 39 39 0 0 1 4.3 0m1.1-1.17q1.45.13 2.69.34a6.5 6.5 0 0 0-3.67-3.44q.65 1.26.98 3.1M8.48 1.5l.01.02q.41.37.84 1.31.38.89.63 2.08a40 40 0 0 0-3.92 0q.25-1.2.63-2.08a4 4 0 0 1 .85-1.32 7 7 0 0 1 .96 0m-2.75.4a6.5 6.5 0 0 0-3.67 3.44 29 29 0 0 1 2.7-.34q.31-1.83.97-3.1M4.58 6.28q-1.66.16-2.95.43a7 7 0 0 0 0 2.58q1.3.27 2.95.43a18 18 0 0 1 0-3.44m.17 4.71q-1.45-.12-2.69-.34a6.5 6.5 0 0 0 3.67 3.44q-.65-1.27-.98-3.1" fill="#666"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h16v16H0z"/></clipPath></defs></svg>
|
||||||
|
After Width: | Height: | Size: 1.0 KiB |
1
web/public/next.svg
Normal file
1
web/public/next.svg
Normal file
@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 394 80"><path fill="#000" d="M262 0h68.5v12.7h-27.2v66.6h-13.6V12.7H262V0ZM149 0v12.7H94v20.4h44.3v12.6H94v21h55v12.6H80.5V0h68.7zm34.3 0h-17.8l63.8 79.4h17.9l-32-39.7 32-39.6h-17.9l-23 28.6-23-28.6zm18.3 56.7-9-11-27.1 33.7h17.8l18.3-22.7z"/><path fill="#000" d="M81 79.3 17 0H0v79.3h13.6V17l50.2 62.3H81Zm252.6-.4c-1 0-1.8-.4-2.5-1s-1.1-1.6-1.1-2.6.3-1.8 1-2.5 1.6-1 2.6-1 1.8.3 2.5 1a3.4 3.4 0 0 1 .6 4.3 3.7 3.7 0 0 1-3 1.8zm23.2-33.5h6v23.3c0 2.1-.4 4-1.3 5.5a9.1 9.1 0 0 1-3.8 3.5c-1.6.8-3.5 1.3-5.7 1.3-2 0-3.7-.4-5.3-1s-2.8-1.8-3.7-3.2c-.9-1.3-1.4-3-1.4-5h6c.1.8.3 1.6.7 2.2s1 1.2 1.6 1.5c.7.4 1.5.5 2.4.5 1 0 1.8-.2 2.4-.6a4 4 0 0 0 1.6-1.8c.3-.8.5-1.8.5-3V45.5zm30.9 9.1a4.4 4.4 0 0 0-2-3.3 7.5 7.5 0 0 0-4.3-1.1c-1.3 0-2.4.2-3.3.5-.9.4-1.6 1-2 1.6a3.5 3.5 0 0 0-.3 4c.3.5.7.9 1.3 1.2l1.8 1 2 .5 3.2.8c1.3.3 2.5.7 3.7 1.2a13 13 0 0 1 3.2 1.8 8.1 8.1 0 0 1 3 6.5c0 2-.5 3.7-1.5 5.1a10 10 0 0 1-4.4 3.5c-1.8.8-4.1 1.2-6.8 1.2-2.6 0-4.9-.4-6.8-1.2-2-.8-3.4-2-4.5-3.5a10 10 0 0 1-1.7-5.6h6a5 5 0 0 0 3.5 4.6c1 .4 2.2.6 3.4.6 1.3 0 2.5-.2 3.5-.6 1-.4 1.8-1 2.4-1.7a4 4 0 0 0 .8-2.4c0-.9-.2-1.6-.7-2.2a11 11 0 0 0-2.1-1.4l-3.2-1-3.8-1c-2.8-.7-5-1.7-6.6-3.2a7.2 7.2 0 0 1-2.4-5.7 8 8 0 0 1 1.7-5 10 10 0 0 1 4.3-3.5c2-.8 4-1.2 6.4-1.2 2.3 0 4.4.4 6.2 1.2 1.8.8 3.2 2 4.3 3.4 1 1.4 1.5 3 1.5 5h-5.8z"/></svg>
|
||||||
|
After Width: | Height: | Size: 1.3 KiB |
1
web/public/vercel.svg
Normal file
1
web/public/vercel.svg
Normal file
@ -0,0 +1 @@
|
|||||||
|
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1155 1000"><path d="m577.3 0 577.4 1000H0z" fill="#fff"/></svg>
|
||||||
|
After Width: | Height: | Size: 128 B |
1
web/public/window.svg
Normal file
1
web/public/window.svg
Normal file
@ -0,0 +1 @@
|
|||||||
|
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.5 2.5h13v10a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1zM0 1h16v11.5a2.5 2.5 0 0 1-2.5 2.5h-11A2.5 2.5 0 0 1 0 12.5zm3.75 4.5a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5M7 4.75a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0m1.75.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5" fill="#666"/></svg>
|
||||||
|
After Width: | Height: | Size: 385 B |
BIN
web/src/app/favicon.ico
Normal file
BIN
web/src/app/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
26
web/src/app/globals.css
Normal file
26
web/src/app/globals.css
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
@import "tailwindcss";
|
||||||
|
|
||||||
|
:root {
|
||||||
|
--background: #ffffff;
|
||||||
|
--foreground: #171717;
|
||||||
|
}
|
||||||
|
|
||||||
|
@theme inline {
|
||||||
|
--color-background: var(--background);
|
||||||
|
--color-foreground: var(--foreground);
|
||||||
|
--font-sans: var(--font-geist-sans);
|
||||||
|
--font-mono: var(--font-geist-mono);
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
:root {
|
||||||
|
--background: #0a0a0a;
|
||||||
|
--foreground: #ededed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
background: var(--background);
|
||||||
|
color: var(--foreground);
|
||||||
|
font-family: Arial, Helvetica, sans-serif;
|
||||||
|
}
|
||||||
34
web/src/app/layout.tsx
Normal file
34
web/src/app/layout.tsx
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import type { Metadata } from "next";
|
||||||
|
import { Geist, Geist_Mono } from "next/font/google";
|
||||||
|
import "./globals.css";
|
||||||
|
|
||||||
|
const geistSans = Geist({
|
||||||
|
variable: "--font-geist-sans",
|
||||||
|
subsets: ["latin"],
|
||||||
|
});
|
||||||
|
|
||||||
|
const geistMono = Geist_Mono({
|
||||||
|
variable: "--font-geist-mono",
|
||||||
|
subsets: ["latin"],
|
||||||
|
});
|
||||||
|
|
||||||
|
export const metadata: Metadata = {
|
||||||
|
title: "Create Next App",
|
||||||
|
description: "Generated by create next app",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function RootLayout({
|
||||||
|
children,
|
||||||
|
}: Readonly<{
|
||||||
|
children: React.ReactNode;
|
||||||
|
}>) {
|
||||||
|
return (
|
||||||
|
<html lang="en">
|
||||||
|
<body
|
||||||
|
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
);
|
||||||
|
}
|
||||||
65
web/src/app/page.tsx
Normal file
65
web/src/app/page.tsx
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import Image from "next/image";
|
||||||
|
|
||||||
|
export default function Home() {
|
||||||
|
return (
|
||||||
|
<div className="flex min-h-screen items-center justify-center bg-zinc-50 font-sans dark:bg-black">
|
||||||
|
<main className="flex min-h-screen w-full max-w-3xl flex-col items-center justify-between py-32 px-16 bg-white dark:bg-black sm:items-start">
|
||||||
|
<Image
|
||||||
|
className="dark:invert"
|
||||||
|
src="/next.svg"
|
||||||
|
alt="Next.js logo"
|
||||||
|
width={100}
|
||||||
|
height={20}
|
||||||
|
priority
|
||||||
|
/>
|
||||||
|
<div className="flex flex-col items-center gap-6 text-center sm:items-start sm:text-left">
|
||||||
|
<h1 className="max-w-xs text-3xl font-semibold leading-10 tracking-tight text-black dark:text-zinc-50">
|
||||||
|
To get started, edit the page.tsx file.
|
||||||
|
</h1>
|
||||||
|
<p className="max-w-md text-lg leading-8 text-zinc-600 dark:text-zinc-400">
|
||||||
|
Looking for a starting point or more instructions? Head over to{" "}
|
||||||
|
<a
|
||||||
|
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||||
|
className="font-medium text-zinc-950 dark:text-zinc-50"
|
||||||
|
>
|
||||||
|
Templates
|
||||||
|
</a>{" "}
|
||||||
|
or the{" "}
|
||||||
|
<a
|
||||||
|
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||||
|
className="font-medium text-zinc-950 dark:text-zinc-50"
|
||||||
|
>
|
||||||
|
Learning
|
||||||
|
</a>{" "}
|
||||||
|
center.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col gap-4 text-base font-medium sm:flex-row">
|
||||||
|
<a
|
||||||
|
className="flex h-12 w-full items-center justify-center gap-2 rounded-full bg-foreground px-5 text-background transition-colors hover:bg-[#383838] dark:hover:bg-[#ccc] md:w-[158px]"
|
||||||
|
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
>
|
||||||
|
<Image
|
||||||
|
className="dark:invert"
|
||||||
|
src="/vercel.svg"
|
||||||
|
alt="Vercel logomark"
|
||||||
|
width={16}
|
||||||
|
height={16}
|
||||||
|
/>
|
||||||
|
Deploy Now
|
||||||
|
</a>
|
||||||
|
<a
|
||||||
|
className="flex h-12 w-full items-center justify-center rounded-full border border-solid border-black/[.08] px-5 transition-colors hover:border-transparent hover:bg-black/[.04] dark:border-white/[.145] dark:hover:bg-[#1a1a1a] md:w-[158px]"
|
||||||
|
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
>
|
||||||
|
Documentation
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
134
web/src/lib/cascade.test.ts
Normal file
134
web/src/lib/cascade.test.ts
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import {
|
||||||
|
calculateCascadeWarnings,
|
||||||
|
getNextWarning,
|
||||||
|
checkWarnings,
|
||||||
|
getCascadeIntervals,
|
||||||
|
formatMinutesBefore,
|
||||||
|
CASCADE_PRESETS,
|
||||||
|
} from './cascade';
|
||||||
|
|
||||||
|
describe('cascade', () => {
|
||||||
|
const ONE_MIN = 60 * 1000;
|
||||||
|
const ONE_HOUR = 60 * ONE_MIN;
|
||||||
|
|
||||||
|
describe('CASCADE_PRESETS', () => {
|
||||||
|
it('aggressive has 9 intervals', () => {
|
||||||
|
expect(CASCADE_PRESETS.aggressive).toHaveLength(9);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('standard has 5 intervals', () => {
|
||||||
|
expect(CASCADE_PRESETS.standard).toHaveLength(5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('none has 0 intervals', () => {
|
||||||
|
expect(CASCADE_PRESETS.none).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('calculateCascadeWarnings', () => {
|
||||||
|
it('creates warnings for all intervals', () => {
|
||||||
|
const targetTime = Date.now() + 3 * ONE_HOUR;
|
||||||
|
const warnings = calculateCascadeWarnings(targetTime, [120, 60, 30, 15, 5]);
|
||||||
|
expect(warnings).toHaveLength(5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('marks past warnings as fired', () => {
|
||||||
|
const now = Date.now();
|
||||||
|
const targetTime = now + 10 * ONE_MIN; // 10 min from now
|
||||||
|
const warnings = calculateCascadeWarnings(targetTime, [30, 15, 5], now);
|
||||||
|
// 30m and 15m warnings are in the past (target is only 10m away)
|
||||||
|
const firedCount = warnings.filter((w) => w.fired).length;
|
||||||
|
expect(firedCount).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty array for empty intervals', () => {
|
||||||
|
const warnings = calculateCascadeWarnings(Date.now() + ONE_HOUR, []);
|
||||||
|
expect(warnings).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sorts warnings by minutesBefore descending (earliest first)', () => {
|
||||||
|
const targetTime = Date.now() + 5 * ONE_HOUR;
|
||||||
|
const warnings = calculateCascadeWarnings(targetTime, [5, 60, 15, 120]);
|
||||||
|
expect(warnings[0].minutesBefore).toBe(120);
|
||||||
|
expect(warnings[3].minutesBefore).toBe(5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('all warnings are in the future when target is far away', () => {
|
||||||
|
const targetTime = Date.now() + 24 * ONE_HOUR;
|
||||||
|
const warnings = calculateCascadeWarnings(targetTime, [120, 60, 30]);
|
||||||
|
expect(warnings.every((w) => !w.fired)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getNextWarning', () => {
|
||||||
|
it('returns first unfired warning', () => {
|
||||||
|
const targetTime = Date.now() + 2 * ONE_HOUR;
|
||||||
|
const warnings = calculateCascadeWarnings(targetTime, [120, 60, 30, 15, 5]);
|
||||||
|
const next = getNextWarning(warnings);
|
||||||
|
expect(next).not.toBeNull();
|
||||||
|
expect(next!.fired).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null when all warnings are fired', () => {
|
||||||
|
const targetTime = Date.now() - ONE_MIN; // in the past
|
||||||
|
const warnings = calculateCascadeWarnings(targetTime, [5, 1]);
|
||||||
|
const next = getNextWarning(warnings);
|
||||||
|
expect(next).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('checkWarnings', () => {
|
||||||
|
it('fires warnings whose time has come', () => {
|
||||||
|
const now = Date.now();
|
||||||
|
const targetTime = now + 20 * ONE_MIN;
|
||||||
|
const warnings = calculateCascadeWarnings(targetTime, [30, 15, 5], now);
|
||||||
|
// At now, 30m warning is past (target - 30m < now)
|
||||||
|
// Advance time to 10m before target (5m warning should fire)
|
||||||
|
const laterNow = targetTime - 10 * ONE_MIN;
|
||||||
|
const fired = checkWarnings(warnings, laterNow);
|
||||||
|
expect(fired.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not re-fire already fired warnings', () => {
|
||||||
|
const now = Date.now();
|
||||||
|
const targetTime = now + 2 * ONE_MIN;
|
||||||
|
const warnings = calculateCascadeWarnings(targetTime, [5], now);
|
||||||
|
// 5m warning is already past since target is 2m away
|
||||||
|
const fired1 = checkWarnings(warnings, now);
|
||||||
|
const fired2 = checkWarnings(warnings, now + 1000);
|
||||||
|
// Second check should not re-fire
|
||||||
|
expect(fired2).toHaveLength(0);
|
||||||
|
expect(fired1.length).toBeGreaterThanOrEqual(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getCascadeIntervals', () => {
|
||||||
|
it('returns preset intervals for standard', () => {
|
||||||
|
const intervals = getCascadeIntervals({ preset: 'standard', intervals: [] });
|
||||||
|
expect(intervals).toEqual(CASCADE_PRESETS.standard);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns custom intervals sorted descending', () => {
|
||||||
|
const intervals = getCascadeIntervals({ preset: 'custom', intervals: [5, 30, 10] });
|
||||||
|
expect(intervals).toEqual([30, 10, 5]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('formatMinutesBefore', () => {
|
||||||
|
it('formats minutes under 60', () => {
|
||||||
|
expect(formatMinutesBefore(5)).toBe('5m');
|
||||||
|
expect(formatMinutesBefore(15)).toBe('15m');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('formats exact hours', () => {
|
||||||
|
expect(formatMinutesBefore(60)).toBe('1h');
|
||||||
|
expect(formatMinutesBefore(120)).toBe('2h');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('formats hours and minutes', () => {
|
||||||
|
expect(formatMinutesBefore(90)).toBe('1h 30m');
|
||||||
|
expect(formatMinutesBefore(150)).toBe('2h 30m');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
108
web/src/lib/cascade.ts
Normal file
108
web/src/lib/cascade.ts
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
// ── Pre-Warning Cascade System ─────────────────────────────────
|
||||||
|
// Cascade presets aligned with PRD: Aggressive, Standard, Light, Minimal, None, Custom
|
||||||
|
|
||||||
|
export type CascadePreset = 'aggressive' | 'standard' | 'light' | 'minimal' | 'none' | 'custom';
|
||||||
|
|
||||||
|
export interface CascadeWarning {
|
||||||
|
id: string;
|
||||||
|
minutesBefore: number;
|
||||||
|
fired: boolean;
|
||||||
|
firedAt: number | null;
|
||||||
|
scheduledTime: number; // epoch ms
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CascadeConfig {
|
||||||
|
preset: CascadePreset;
|
||||||
|
intervals: number[]; // minutes before target time
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preset definitions (minutes before target)
|
||||||
|
export const CASCADE_PRESETS: Record<CascadePreset, number[]> = {
|
||||||
|
aggressive: [240, 180, 120, 90, 60, 30, 15, 5, 1],
|
||||||
|
standard: [120, 60, 30, 15, 5],
|
||||||
|
light: [60, 15, 5],
|
||||||
|
minimal: [15],
|
||||||
|
none: [],
|
||||||
|
custom: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
export const CASCADE_PRESET_LABELS: Record<CascadePreset, string> = {
|
||||||
|
aggressive: 'Aggressive',
|
||||||
|
standard: 'Standard',
|
||||||
|
light: 'Light',
|
||||||
|
minimal: 'Minimal',
|
||||||
|
none: 'None (fire only)',
|
||||||
|
custom: 'Custom',
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate all warning timestamps from target time and cascade intervals.
|
||||||
|
* Filters out warnings that would be in the past relative to `now`.
|
||||||
|
*/
|
||||||
|
export function calculateCascadeWarnings(
|
||||||
|
targetTime: number,
|
||||||
|
intervals: number[],
|
||||||
|
now: number = Date.now()
|
||||||
|
): CascadeWarning[] {
|
||||||
|
return intervals
|
||||||
|
.sort((a, b) => b - a) // largest first (earliest warning)
|
||||||
|
.map((minutesBefore, idx) => {
|
||||||
|
const scheduledTime = targetTime - minutesBefore * 60 * 1000;
|
||||||
|
return {
|
||||||
|
id: `w-${idx}-${minutesBefore}m`,
|
||||||
|
minutesBefore,
|
||||||
|
fired: scheduledTime <= now,
|
||||||
|
firedAt: scheduledTime <= now ? scheduledTime : null,
|
||||||
|
scheduledTime,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the next unfired warning from a cascade.
|
||||||
|
*/
|
||||||
|
export function getNextWarning(warnings: CascadeWarning[]): CascadeWarning | null {
|
||||||
|
return warnings.find((w) => !w.fired) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check which warnings should fire given the current time.
|
||||||
|
* Returns newly-fired warning IDs.
|
||||||
|
*/
|
||||||
|
export function checkWarnings(
|
||||||
|
warnings: CascadeWarning[],
|
||||||
|
now: number = Date.now()
|
||||||
|
): string[] {
|
||||||
|
const newlyFired: string[] = [];
|
||||||
|
for (const warning of warnings) {
|
||||||
|
if (!warning.fired && warning.scheduledTime <= now) {
|
||||||
|
warning.fired = true;
|
||||||
|
warning.firedAt = now;
|
||||||
|
newlyFired.push(warning.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newlyFired;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get intervals for a preset, or custom intervals if preset is 'custom'.
|
||||||
|
*/
|
||||||
|
export function getCascadeIntervals(config: CascadeConfig): number[] {
|
||||||
|
if (config.preset === 'custom') {
|
||||||
|
return [...config.intervals].sort((a, b) => b - a);
|
||||||
|
}
|
||||||
|
return CASCADE_PRESETS[config.preset];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format minutes into human-readable string.
|
||||||
|
*/
|
||||||
|
export function formatMinutesBefore(minutes: number): string {
|
||||||
|
if (minutes >= 60) {
|
||||||
|
const hours = Math.floor(minutes / 60);
|
||||||
|
const remaining = minutes % 60;
|
||||||
|
if (remaining === 0) return `${hours}h`;
|
||||||
|
return `${hours}h ${remaining}m`;
|
||||||
|
}
|
||||||
|
return `${minutes}m`;
|
||||||
|
}
|
||||||
213
web/src/lib/timer-engine.test.ts
Normal file
213
web/src/lib/timer-engine.test.ts
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
import { describe, it, expect, vi } from 'vitest';
|
||||||
|
import {
|
||||||
|
createAlarm,
|
||||||
|
createCountdown,
|
||||||
|
createPomodoro,
|
||||||
|
pauseTimer,
|
||||||
|
resumeTimer,
|
||||||
|
fireTimer,
|
||||||
|
snoozeTimer,
|
||||||
|
dismissTimer,
|
||||||
|
completeTimer,
|
||||||
|
advancePomodoro,
|
||||||
|
getRemainingMs,
|
||||||
|
isTimerActive,
|
||||||
|
shouldTimerFire,
|
||||||
|
DEFAULT_POMODORO_CONFIG,
|
||||||
|
} from './timer-engine';
|
||||||
|
|
||||||
|
describe('timer-engine', () => {
|
||||||
|
const ONE_MIN = 60 * 1000;
|
||||||
|
const ONE_HOUR = 60 * ONE_MIN;
|
||||||
|
|
||||||
|
describe('createAlarm', () => {
|
||||||
|
it('creates an alarm with correct fields', () => {
|
||||||
|
const targetTime = Date.now() + ONE_HOUR;
|
||||||
|
const timer = createAlarm({ label: 'Meeting', targetTime });
|
||||||
|
expect(timer.type).toBe('alarm');
|
||||||
|
expect(timer.label).toBe('Meeting');
|
||||||
|
expect(timer.state).toBe('active');
|
||||||
|
expect(timer.targetTime).toBe(targetTime);
|
||||||
|
expect(timer.urgency).toBe('standard');
|
||||||
|
expect(timer.cascade.preset).toBe('standard');
|
||||||
|
expect(timer.warnings.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('respects custom urgency and cascade', () => {
|
||||||
|
const targetTime = Date.now() + 5 * ONE_HOUR;
|
||||||
|
const timer = createAlarm({
|
||||||
|
label: 'Flight',
|
||||||
|
targetTime,
|
||||||
|
urgency: 'critical',
|
||||||
|
cascade: { preset: 'aggressive', intervals: [] },
|
||||||
|
});
|
||||||
|
expect(timer.urgency).toBe('critical');
|
||||||
|
expect(timer.cascade.preset).toBe('aggressive');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('createCountdown', () => {
|
||||||
|
it('creates a countdown with correct duration', () => {
|
||||||
|
const timer = createCountdown({ label: 'Pasta', durationMs: 10 * ONE_MIN });
|
||||||
|
expect(timer.type).toBe('countdown');
|
||||||
|
expect(timer.duration).toBe(10 * ONE_MIN);
|
||||||
|
expect(timer.state).toBe('active');
|
||||||
|
expect(timer.targetTime).toBeGreaterThan(Date.now() - 1000);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('createPomodoro', () => {
|
||||||
|
it('creates a pomodoro with default config', () => {
|
||||||
|
const timer = createPomodoro();
|
||||||
|
expect(timer.type).toBe('pomodoro');
|
||||||
|
expect(timer.pomodoroConfig).toEqual(DEFAULT_POMODORO_CONFIG);
|
||||||
|
expect(timer.pomodoroState?.currentRound).toBe(1);
|
||||||
|
expect(timer.pomodoroState?.isBreak).toBe(false);
|
||||||
|
expect(timer.duration).toBe(25 * ONE_MIN);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('accepts custom config', () => {
|
||||||
|
const timer = createPomodoro({ config: { workMinutes: 50, rounds: 2 } });
|
||||||
|
expect(timer.pomodoroConfig?.workMinutes).toBe(50);
|
||||||
|
expect(timer.pomodoroConfig?.rounds).toBe(2);
|
||||||
|
expect(timer.duration).toBe(50 * ONE_MIN);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('state transitions', () => {
|
||||||
|
it('idle → active (via create)', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: 5 * ONE_MIN });
|
||||||
|
expect(timer.state).toBe('active');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('active → paused', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: 5 * ONE_MIN });
|
||||||
|
const paused = pauseTimer(timer);
|
||||||
|
expect(paused.state).toBe('paused');
|
||||||
|
expect(paused.pausedAt).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('paused → active (resume)', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: 5 * ONE_MIN });
|
||||||
|
const paused = pauseTimer(timer);
|
||||||
|
const resumed = resumeTimer(paused);
|
||||||
|
expect(resumed.state).toBe('active');
|
||||||
|
expect(resumed.pausedAt).toBeNull();
|
||||||
|
expect(resumed.startedAt).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('active → firing', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: 5 * ONE_MIN });
|
||||||
|
const fired = fireTimer(timer);
|
||||||
|
expect(fired.state).toBe('firing');
|
||||||
|
expect(fired.firedAt).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('firing → snoozed', () => {
|
||||||
|
const timer = fireTimer(createCountdown({ label: 'Test', durationMs: 0 }));
|
||||||
|
const snoozed = snoozeTimer(timer, 5);
|
||||||
|
expect(snoozed.state).toBe('snoozed');
|
||||||
|
expect(snoozed.snoozeCount).toBe(1);
|
||||||
|
expect(snoozed.snoozedUntil).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('firing → dismissed', () => {
|
||||||
|
const timer = fireTimer(createCountdown({ label: 'Test', durationMs: 0 }));
|
||||||
|
const dismissed = dismissTimer(timer);
|
||||||
|
expect(dismissed.state).toBe('dismissed');
|
||||||
|
expect(dismissed.dismissedAt).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not pause a dismissed timer', () => {
|
||||||
|
const timer = dismissTimer(createCountdown({ label: 'Test', durationMs: 0 }));
|
||||||
|
const result = pauseTimer(timer);
|
||||||
|
expect(result.state).toBe('dismissed');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not snooze a non-firing timer', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: 5 * ONE_MIN });
|
||||||
|
const result = snoozeTimer(timer, 5);
|
||||||
|
expect(result.state).toBe('active');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('pomodoro transitions', () => {
|
||||||
|
it('advances from work to break', () => {
|
||||||
|
const timer = createPomodoro();
|
||||||
|
const fired = fireTimer(timer);
|
||||||
|
const next = advancePomodoro(fired);
|
||||||
|
expect(next).not.toBeNull();
|
||||||
|
expect(next!.pomodoroState?.isBreak).toBe(true);
|
||||||
|
expect(next!.pomodoroState?.completedRounds).toBe(1);
|
||||||
|
expect(next!.state).toBe('active');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('advances from break to next work round', () => {
|
||||||
|
let timer = createPomodoro();
|
||||||
|
timer = fireTimer(timer);
|
||||||
|
timer = advancePomodoro(timer)!; // → break
|
||||||
|
timer = fireTimer(timer);
|
||||||
|
timer = advancePomodoro(timer)!; // → work round 2
|
||||||
|
expect(timer.pomodoroState?.isBreak).toBe(false);
|
||||||
|
expect(timer.pomodoroState?.currentRound).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('completes after all rounds', () => {
|
||||||
|
let timer = createPomodoro({ config: { rounds: 1, workMinutes: 1, breakMinutes: 1, longBreakMinutes: 1 } });
|
||||||
|
// Round 1 work
|
||||||
|
timer = fireTimer(timer);
|
||||||
|
timer = advancePomodoro(timer)!; // → long break (1 round = done after break)
|
||||||
|
expect(timer.pomodoroState?.isLongBreak).toBe(true);
|
||||||
|
timer = fireTimer(timer);
|
||||||
|
timer = advancePomodoro(timer)!; // → complete
|
||||||
|
expect(timer.state).toBe('completed');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns null for non-pomodoro timer', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: 5 * ONE_MIN });
|
||||||
|
expect(advancePomodoro(timer)).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('utility functions', () => {
|
||||||
|
it('getRemainingMs returns positive value for active timer', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: 5 * ONE_MIN });
|
||||||
|
const remaining = getRemainingMs(timer);
|
||||||
|
expect(remaining).toBeGreaterThan(0);
|
||||||
|
expect(remaining).toBeLessThanOrEqual(5 * ONE_MIN);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('getRemainingMs returns remaining for paused timer', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: 5 * ONE_MIN });
|
||||||
|
const paused = pauseTimer(timer);
|
||||||
|
const remaining = getRemainingMs(paused);
|
||||||
|
expect(remaining).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('isTimerActive returns true for active/warning/snoozed', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: 5 * ONE_MIN });
|
||||||
|
expect(isTimerActive(timer)).toBe(true);
|
||||||
|
expect(isTimerActive({ ...timer, state: 'warning' })).toBe(true);
|
||||||
|
expect(isTimerActive({ ...timer, state: 'snoozed' })).toBe(true);
|
||||||
|
expect(isTimerActive({ ...timer, state: 'dismissed' })).toBe(false);
|
||||||
|
expect(isTimerActive({ ...timer, state: 'paused' })).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shouldTimerFire returns true when target time has passed', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: 0 });
|
||||||
|
expect(shouldTimerFire(timer, Date.now() + 1000)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shouldTimerFire returns false for future timer', () => {
|
||||||
|
const timer = createCountdown({ label: 'Test', durationMs: ONE_HOUR });
|
||||||
|
expect(shouldTimerFire(timer)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shouldTimerFire returns true for expired snooze', () => {
|
||||||
|
const timer = fireTimer(createCountdown({ label: 'Test', durationMs: 0 }));
|
||||||
|
const snoozed = snoozeTimer(timer, 5);
|
||||||
|
// Check after snooze expires
|
||||||
|
expect(shouldTimerFire(snoozed, Date.now() + 10 * ONE_MIN)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
377
web/src/lib/timer-engine.ts
Normal file
377
web/src/lib/timer-engine.ts
Normal file
@ -0,0 +1,377 @@
|
|||||||
|
// ── Timer Engine ───────────────────────────────────────────────
|
||||||
|
// Core timer types, state machine, and lifecycle management
|
||||||
|
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
import type { UrgencyLevel } from './urgency';
|
||||||
|
import type { CascadeConfig, CascadeWarning } from './cascade';
|
||||||
|
import { calculateCascadeWarnings, getCascadeIntervals } from './cascade';
|
||||||
|
|
||||||
|
// ── Types ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export type TimerType = 'alarm' | 'countdown' | 'pomodoro' | 'event';
|
||||||
|
|
||||||
|
export type TimerState =
|
||||||
|
| 'idle'
|
||||||
|
| 'active'
|
||||||
|
| 'warning'
|
||||||
|
| 'firing'
|
||||||
|
| 'snoozed'
|
||||||
|
| 'dismissed'
|
||||||
|
| 'completed'
|
||||||
|
| 'paused';
|
||||||
|
|
||||||
|
export interface Timer {
|
||||||
|
id: string;
|
||||||
|
type: TimerType;
|
||||||
|
label: string;
|
||||||
|
description?: string;
|
||||||
|
urgency: UrgencyLevel;
|
||||||
|
state: TimerState;
|
||||||
|
|
||||||
|
// Time fields
|
||||||
|
targetTime: number; // epoch ms — when the timer fires
|
||||||
|
duration: number | null; // ms — for countdowns/pomodoro
|
||||||
|
createdAt: number;
|
||||||
|
startedAt: number | null;
|
||||||
|
pausedAt: number | null;
|
||||||
|
firedAt: number | null;
|
||||||
|
dismissedAt: number | null;
|
||||||
|
completedAt: number | null;
|
||||||
|
|
||||||
|
// Elapsed tracking for pause/resume
|
||||||
|
elapsedBeforePause: number; // ms accumulated before last pause
|
||||||
|
|
||||||
|
// Cascade
|
||||||
|
cascade: CascadeConfig;
|
||||||
|
warnings: CascadeWarning[];
|
||||||
|
|
||||||
|
// Pomodoro-specific
|
||||||
|
pomodoroConfig?: PomodoroConfig;
|
||||||
|
pomodoroState?: PomodoroState;
|
||||||
|
|
||||||
|
// Snooze
|
||||||
|
snoozeCount: number;
|
||||||
|
snoozedUntil: number | null;
|
||||||
|
|
||||||
|
// Metadata
|
||||||
|
category?: string;
|
||||||
|
tags?: string[];
|
||||||
|
linkedTimerId?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PomodoroConfig {
|
||||||
|
workMinutes: number;
|
||||||
|
breakMinutes: number;
|
||||||
|
longBreakMinutes: number;
|
||||||
|
rounds: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PomodoroState {
|
||||||
|
currentRound: number;
|
||||||
|
isBreak: boolean;
|
||||||
|
isLongBreak: boolean;
|
||||||
|
completedRounds: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DEFAULT_POMODORO_CONFIG: PomodoroConfig = {
|
||||||
|
workMinutes: 25,
|
||||||
|
breakMinutes: 5,
|
||||||
|
longBreakMinutes: 15,
|
||||||
|
rounds: 4,
|
||||||
|
};
|
||||||
|
|
||||||
|
// ── Factory Functions ──────────────────────────────────────────
|
||||||
|
|
||||||
|
export interface CreateAlarmParams {
|
||||||
|
label: string;
|
||||||
|
targetTime: number;
|
||||||
|
urgency?: UrgencyLevel;
|
||||||
|
cascade?: CascadeConfig;
|
||||||
|
category?: string;
|
||||||
|
description?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createAlarm(params: CreateAlarmParams): Timer {
|
||||||
|
const cascade = params.cascade ?? { preset: 'standard', intervals: [] };
|
||||||
|
const intervals = getCascadeIntervals(cascade);
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: uuidv4(),
|
||||||
|
type: 'alarm',
|
||||||
|
label: params.label,
|
||||||
|
description: params.description,
|
||||||
|
urgency: params.urgency ?? 'standard',
|
||||||
|
state: 'active',
|
||||||
|
targetTime: params.targetTime,
|
||||||
|
duration: params.targetTime - now,
|
||||||
|
createdAt: now,
|
||||||
|
startedAt: now,
|
||||||
|
pausedAt: null,
|
||||||
|
firedAt: null,
|
||||||
|
dismissedAt: null,
|
||||||
|
completedAt: null,
|
||||||
|
elapsedBeforePause: 0,
|
||||||
|
cascade,
|
||||||
|
warnings: calculateCascadeWarnings(params.targetTime, intervals, now),
|
||||||
|
snoozeCount: 0,
|
||||||
|
snoozedUntil: null,
|
||||||
|
category: params.category,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CreateCountdownParams {
|
||||||
|
label: string;
|
||||||
|
durationMs: number;
|
||||||
|
urgency?: UrgencyLevel;
|
||||||
|
cascade?: CascadeConfig;
|
||||||
|
category?: string;
|
||||||
|
description?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createCountdown(params: CreateCountdownParams): Timer {
|
||||||
|
const now = Date.now();
|
||||||
|
const targetTime = now + params.durationMs;
|
||||||
|
const cascade = params.cascade ?? { preset: 'standard', intervals: [] };
|
||||||
|
const intervals = getCascadeIntervals(cascade);
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: uuidv4(),
|
||||||
|
type: 'countdown',
|
||||||
|
label: params.label,
|
||||||
|
description: params.description,
|
||||||
|
urgency: params.urgency ?? 'standard',
|
||||||
|
state: 'active',
|
||||||
|
targetTime,
|
||||||
|
duration: params.durationMs,
|
||||||
|
createdAt: now,
|
||||||
|
startedAt: now,
|
||||||
|
pausedAt: null,
|
||||||
|
firedAt: null,
|
||||||
|
dismissedAt: null,
|
||||||
|
completedAt: null,
|
||||||
|
elapsedBeforePause: 0,
|
||||||
|
cascade,
|
||||||
|
warnings: calculateCascadeWarnings(targetTime, intervals, now),
|
||||||
|
snoozeCount: 0,
|
||||||
|
snoozedUntil: null,
|
||||||
|
category: params.category,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CreatePomodoroParams {
|
||||||
|
label?: string;
|
||||||
|
config?: Partial<PomodoroConfig>;
|
||||||
|
urgency?: UrgencyLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createPomodoro(params: CreatePomodoroParams = {}): Timer {
|
||||||
|
const now = Date.now();
|
||||||
|
const config = { ...DEFAULT_POMODORO_CONFIG, ...params.config };
|
||||||
|
const durationMs = config.workMinutes * 60 * 1000;
|
||||||
|
const targetTime = now + durationMs;
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: uuidv4(),
|
||||||
|
type: 'pomodoro',
|
||||||
|
label: params.label ?? 'Focus Session',
|
||||||
|
urgency: params.urgency ?? 'standard',
|
||||||
|
state: 'active',
|
||||||
|
targetTime,
|
||||||
|
duration: durationMs,
|
||||||
|
createdAt: now,
|
||||||
|
startedAt: now,
|
||||||
|
pausedAt: null,
|
||||||
|
firedAt: null,
|
||||||
|
dismissedAt: null,
|
||||||
|
completedAt: null,
|
||||||
|
elapsedBeforePause: 0,
|
||||||
|
cascade: { preset: 'minimal', intervals: [] },
|
||||||
|
warnings: calculateCascadeWarnings(targetTime, [1], now),
|
||||||
|
pomodoroConfig: config,
|
||||||
|
pomodoroState: {
|
||||||
|
currentRound: 1,
|
||||||
|
isBreak: false,
|
||||||
|
isLongBreak: false,
|
||||||
|
completedRounds: 0,
|
||||||
|
},
|
||||||
|
snoozeCount: 0,
|
||||||
|
snoozedUntil: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── State Transitions ──────────────────────────────────────────
|
||||||
|
|
||||||
|
export function pauseTimer(timer: Timer): Timer {
|
||||||
|
if (timer.state !== 'active' && timer.state !== 'warning') return timer;
|
||||||
|
const now = Date.now();
|
||||||
|
const elapsed = timer.elapsedBeforePause + (now - (timer.startedAt ?? now));
|
||||||
|
return {
|
||||||
|
...timer,
|
||||||
|
state: 'paused',
|
||||||
|
pausedAt: now,
|
||||||
|
elapsedBeforePause: elapsed,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resumeTimer(timer: Timer): Timer {
|
||||||
|
if (timer.state !== 'paused') return timer;
|
||||||
|
const now = Date.now();
|
||||||
|
const remainingMs = (timer.duration ?? 0) - timer.elapsedBeforePause;
|
||||||
|
const newTargetTime = now + remainingMs;
|
||||||
|
|
||||||
|
const intervals = getCascadeIntervals(timer.cascade);
|
||||||
|
return {
|
||||||
|
...timer,
|
||||||
|
state: 'active',
|
||||||
|
startedAt: now,
|
||||||
|
pausedAt: null,
|
||||||
|
targetTime: newTargetTime,
|
||||||
|
warnings: calculateCascadeWarnings(newTargetTime, intervals, now),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fireTimer(timer: Timer): Timer {
|
||||||
|
if (timer.state === 'dismissed' || timer.state === 'completed') return timer;
|
||||||
|
return {
|
||||||
|
...timer,
|
||||||
|
state: 'firing',
|
||||||
|
firedAt: Date.now(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function snoozeTimer(timer: Timer, snoozeMinutes: number): Timer {
|
||||||
|
if (timer.state !== 'firing') return timer;
|
||||||
|
const now = Date.now();
|
||||||
|
const snoozeUntil = now + snoozeMinutes * 60 * 1000;
|
||||||
|
const intervals = getCascadeIntervals(timer.cascade);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...timer,
|
||||||
|
state: 'snoozed',
|
||||||
|
targetTime: snoozeUntil,
|
||||||
|
snoozedUntil: snoozeUntil,
|
||||||
|
snoozeCount: timer.snoozeCount + 1,
|
||||||
|
warnings: calculateCascadeWarnings(snoozeUntil, intervals.filter(m => m <= snoozeMinutes), now),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function dismissTimer(timer: Timer): Timer {
|
||||||
|
return {
|
||||||
|
...timer,
|
||||||
|
state: 'dismissed',
|
||||||
|
dismissedAt: Date.now(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function completeTimer(timer: Timer): Timer {
|
||||||
|
return {
|
||||||
|
...timer,
|
||||||
|
state: 'completed',
|
||||||
|
completedAt: Date.now(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Pomodoro Transitions ───────────────────────────────────────
|
||||||
|
|
||||||
|
export function advancePomodoro(timer: Timer): Timer | null {
|
||||||
|
if (timer.type !== 'pomodoro' || !timer.pomodoroConfig || !timer.pomodoroState) return null;
|
||||||
|
|
||||||
|
const { pomodoroConfig: config, pomodoroState: state } = timer;
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
if (state.isBreak || state.isLongBreak) {
|
||||||
|
// Long break finished → all done
|
||||||
|
if (state.isLongBreak) {
|
||||||
|
return completeTimer(timer);
|
||||||
|
}
|
||||||
|
// Short break finished → start next work round
|
||||||
|
const nextRound = state.currentRound + 1;
|
||||||
|
if (nextRound > config.rounds) {
|
||||||
|
return completeTimer(timer);
|
||||||
|
}
|
||||||
|
const durationMs = config.workMinutes * 60 * 1000;
|
||||||
|
return {
|
||||||
|
...timer,
|
||||||
|
state: 'active',
|
||||||
|
targetTime: now + durationMs,
|
||||||
|
duration: durationMs,
|
||||||
|
startedAt: now,
|
||||||
|
firedAt: null,
|
||||||
|
elapsedBeforePause: 0,
|
||||||
|
warnings: calculateCascadeWarnings(now + durationMs, [1], now),
|
||||||
|
pomodoroState: {
|
||||||
|
currentRound: nextRound,
|
||||||
|
isBreak: false,
|
||||||
|
isLongBreak: false,
|
||||||
|
completedRounds: state.completedRounds,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// Work finished → start break
|
||||||
|
const completedRounds = state.completedRounds + 1;
|
||||||
|
const isLongBreak = completedRounds >= config.rounds;
|
||||||
|
|
||||||
|
if (isLongBreak && completedRounds >= config.rounds) {
|
||||||
|
// All rounds done, long break
|
||||||
|
const durationMs = config.longBreakMinutes * 60 * 1000;
|
||||||
|
return {
|
||||||
|
...timer,
|
||||||
|
state: 'active',
|
||||||
|
targetTime: now + durationMs,
|
||||||
|
duration: durationMs,
|
||||||
|
startedAt: now,
|
||||||
|
firedAt: null,
|
||||||
|
elapsedBeforePause: 0,
|
||||||
|
warnings: [],
|
||||||
|
pomodoroState: {
|
||||||
|
currentRound: state.currentRound,
|
||||||
|
isBreak: false,
|
||||||
|
isLongBreak: true,
|
||||||
|
completedRounds,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const durationMs = config.breakMinutes * 60 * 1000;
|
||||||
|
return {
|
||||||
|
...timer,
|
||||||
|
state: 'active',
|
||||||
|
targetTime: now + durationMs,
|
||||||
|
duration: durationMs,
|
||||||
|
startedAt: now,
|
||||||
|
firedAt: null,
|
||||||
|
elapsedBeforePause: 0,
|
||||||
|
warnings: [],
|
||||||
|
pomodoroState: {
|
||||||
|
currentRound: state.currentRound,
|
||||||
|
isBreak: true,
|
||||||
|
isLongBreak: false,
|
||||||
|
completedRounds,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Utility ────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
export function getRemainingMs(timer: Timer, now: number = Date.now()): number {
|
||||||
|
if (timer.state === 'paused') {
|
||||||
|
return (timer.duration ?? 0) - timer.elapsedBeforePause;
|
||||||
|
}
|
||||||
|
return Math.max(0, timer.targetTime - now);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isTimerActive(timer: Timer): boolean {
|
||||||
|
return ['active', 'warning', 'snoozed'].includes(timer.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function shouldTimerFire(timer: Timer, now: number = Date.now()): boolean {
|
||||||
|
if (timer.state === 'snoozed' && timer.snoozedUntil && now >= timer.snoozedUntil) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ((timer.state === 'active' || timer.state === 'warning') && now >= timer.targetTime) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
102
web/src/lib/urgency.ts
Normal file
102
web/src/lib/urgency.ts
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
// ── Urgency System ─────────────────────────────────────────────
|
||||||
|
// 5 levels mapping to notification style, sound, vibration, visual intensity, snooze behavior
|
||||||
|
|
||||||
|
export type UrgencyLevel = 'critical' | 'important' | 'standard' | 'gentle' | 'passive';
|
||||||
|
|
||||||
|
export interface UrgencyConfig {
|
||||||
|
level: UrgencyLevel;
|
||||||
|
label: string;
|
||||||
|
color: string;
|
||||||
|
bgColor: string;
|
||||||
|
borderColor: string;
|
||||||
|
notificationStyle: 'persistent' | 'prominent' | 'default' | 'subtle' | 'badge';
|
||||||
|
soundEnabled: boolean;
|
||||||
|
vibrationPattern: number[]; // ms on/off pattern
|
||||||
|
visualIntensity: number; // 0-1
|
||||||
|
autoSnoozeMinutes: number | null; // null = no auto-snooze
|
||||||
|
requireConfirmToDismiss: boolean;
|
||||||
|
fullScreenOverlay: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const URGENCY_CONFIGS: Record<UrgencyLevel, UrgencyConfig> = {
|
||||||
|
critical: {
|
||||||
|
level: 'critical',
|
||||||
|
label: 'Critical',
|
||||||
|
color: '#FF4757',
|
||||||
|
bgColor: 'rgba(255, 71, 87, 0.15)',
|
||||||
|
borderColor: 'rgba(255, 71, 87, 0.4)',
|
||||||
|
notificationStyle: 'persistent',
|
||||||
|
soundEnabled: true,
|
||||||
|
vibrationPattern: [200, 100, 200, 100, 400],
|
||||||
|
visualIntensity: 1.0,
|
||||||
|
autoSnoozeMinutes: null,
|
||||||
|
requireConfirmToDismiss: true,
|
||||||
|
fullScreenOverlay: true,
|
||||||
|
},
|
||||||
|
important: {
|
||||||
|
level: 'important',
|
||||||
|
label: 'Important',
|
||||||
|
color: '#FF9F43',
|
||||||
|
bgColor: 'rgba(255, 159, 67, 0.15)',
|
||||||
|
borderColor: 'rgba(255, 159, 67, 0.4)',
|
||||||
|
notificationStyle: 'prominent',
|
||||||
|
soundEnabled: true,
|
||||||
|
vibrationPattern: [200, 100, 200],
|
||||||
|
visualIntensity: 0.8,
|
||||||
|
autoSnoozeMinutes: 10,
|
||||||
|
requireConfirmToDismiss: false,
|
||||||
|
fullScreenOverlay: false,
|
||||||
|
},
|
||||||
|
standard: {
|
||||||
|
level: 'standard',
|
||||||
|
label: 'Standard',
|
||||||
|
color: '#FECA57',
|
||||||
|
bgColor: 'rgba(254, 202, 87, 0.15)',
|
||||||
|
borderColor: 'rgba(254, 202, 87, 0.4)',
|
||||||
|
notificationStyle: 'default',
|
||||||
|
soundEnabled: true,
|
||||||
|
vibrationPattern: [200],
|
||||||
|
visualIntensity: 0.6,
|
||||||
|
autoSnoozeMinutes: 5,
|
||||||
|
requireConfirmToDismiss: false,
|
||||||
|
fullScreenOverlay: false,
|
||||||
|
},
|
||||||
|
gentle: {
|
||||||
|
level: 'gentle',
|
||||||
|
label: 'Gentle',
|
||||||
|
color: '#2ED573',
|
||||||
|
bgColor: 'rgba(46, 213, 115, 0.15)',
|
||||||
|
borderColor: 'rgba(46, 213, 115, 0.4)',
|
||||||
|
notificationStyle: 'subtle',
|
||||||
|
soundEnabled: true,
|
||||||
|
vibrationPattern: [100],
|
||||||
|
visualIntensity: 0.3,
|
||||||
|
autoSnoozeMinutes: 5,
|
||||||
|
requireConfirmToDismiss: false,
|
||||||
|
fullScreenOverlay: false,
|
||||||
|
},
|
||||||
|
passive: {
|
||||||
|
level: 'passive',
|
||||||
|
label: 'Passive',
|
||||||
|
color: '#A5B1C7',
|
||||||
|
bgColor: 'rgba(165, 177, 199, 0.1)',
|
||||||
|
borderColor: 'rgba(165, 177, 199, 0.2)',
|
||||||
|
notificationStyle: 'badge',
|
||||||
|
soundEnabled: false,
|
||||||
|
vibrationPattern: [],
|
||||||
|
visualIntensity: 0.1,
|
||||||
|
autoSnoozeMinutes: null,
|
||||||
|
requireConfirmToDismiss: false,
|
||||||
|
fullScreenOverlay: false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const URGENCY_ORDER: UrgencyLevel[] = ['critical', 'important', 'standard', 'gentle', 'passive'];
|
||||||
|
|
||||||
|
export function getUrgencyConfig(level: UrgencyLevel): UrgencyConfig {
|
||||||
|
return URGENCY_CONFIGS[level];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getUrgencyIndex(level: UrgencyLevel): number {
|
||||||
|
return URGENCY_ORDER.indexOf(level);
|
||||||
|
}
|
||||||
34
web/tsconfig.json
Normal file
34
web/tsconfig.json
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2017",
|
||||||
|
"lib": ["dom", "dom.iterable", "esnext"],
|
||||||
|
"allowJs": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"strict": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"module": "esnext",
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"isolatedModules": true,
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
"incremental": true,
|
||||||
|
"plugins": [
|
||||||
|
{
|
||||||
|
"name": "next"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"paths": {
|
||||||
|
"@/*": ["./src/*"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"next-env.d.ts",
|
||||||
|
"**/*.ts",
|
||||||
|
"**/*.tsx",
|
||||||
|
".next/types/**/*.ts",
|
||||||
|
".next/dev/types/**/*.ts",
|
||||||
|
"**/*.mts"
|
||||||
|
],
|
||||||
|
"exclude": ["node_modules"]
|
||||||
|
}
|
||||||
16
web/vitest.config.ts
Normal file
16
web/vitest.config.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { defineConfig } from 'vitest/config';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
test: {
|
||||||
|
globals: true,
|
||||||
|
environment: 'jsdom',
|
||||||
|
setupFiles: [],
|
||||||
|
include: ['src/**/*.test.ts', 'src/**/*.test.tsx'],
|
||||||
|
},
|
||||||
|
resolve: {
|
||||||
|
alias: {
|
||||||
|
'@': path.resolve(__dirname, './src'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user