registri dei piani in git (AI-OS#309)
Refs gitea:Adriano/AI-OS#310 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
# Review package: c9a9ff9..HEAD
|
||||
|
||||
## Commits
|
||||
42d603b feat: slugify, rate limit ed env helper con test
|
||||
|
||||
## Files changed
|
||||
src/lib/env.ts | 5 +++++
|
||||
src/lib/rate-limit.ts | 17 +++++++++++++++++
|
||||
src/lib/slug.ts | 8 ++++++++
|
||||
tests/rate-limit.test.ts | 23 +++++++++++++++++++++++
|
||||
tests/slug.test.ts | 20 ++++++++++++++++++++
|
||||
5 files changed, 73 insertions(+)
|
||||
|
||||
## Diff
|
||||
diff --git a/src/lib/env.ts b/src/lib/env.ts
|
||||
new file mode 100644
|
||||
index 0000000..fe0ba40
|
||||
--- /dev/null
|
||||
+++ b/src/lib/env.ts
|
||||
@@ -0,0 +1,5 @@
|
||||
+export function getEnv(name: string, fallback?: string): string {
|
||||
+ const v = process.env[name] ?? (import.meta.env ? import.meta.env[name] : undefined) ?? fallback;
|
||||
+ if (v === undefined) throw new Error(`Variabile d'ambiente mancante: ${name}`);
|
||||
+ return v;
|
||||
+}
|
||||
diff --git a/src/lib/rate-limit.ts b/src/lib/rate-limit.ts
|
||||
new file mode 100644
|
||||
index 0000000..9f8905c
|
||||
--- /dev/null
|
||||
+++ b/src/lib/rate-limit.ts
|
||||
@@ -0,0 +1,17 @@
|
||||
+const buckets = new Map<string, number[]>();
|
||||
+
|
||||
+export function rateLimit(key: string, max: number, windowMs: number): boolean {
|
||||
+ const now = Date.now();
|
||||
+ const recent = (buckets.get(key) ?? []).filter((t) => now - t < windowMs);
|
||||
+ if (recent.length >= max) {
|
||||
+ buckets.set(key, recent);
|
||||
+ return false;
|
||||
+ }
|
||||
+ recent.push(now);
|
||||
+ buckets.set(key, recent);
|
||||
+ return true;
|
||||
+}
|
||||
+
|
||||
+export function _resetBuckets(): void {
|
||||
+ buckets.clear();
|
||||
+}
|
||||
diff --git a/src/lib/slug.ts b/src/lib/slug.ts
|
||||
new file mode 100644
|
||||
index 0000000..5bdd460
|
||||
--- /dev/null
|
||||
+++ b/src/lib/slug.ts
|
||||
@@ -0,0 +1,8 @@
|
||||
+export function slugify(input: string): string {
|
||||
+ return input
|
||||
+ .toLowerCase()
|
||||
+ .normalize('NFD')
|
||||
+ .replace(/[̀-ͯ]/g, '')
|
||||
+ .replace(/[^a-z0-9]+/g, '-')
|
||||
+ .replace(/^-+|-+$/g, '');
|
||||
+}
|
||||
diff --git a/tests/rate-limit.test.ts b/tests/rate-limit.test.ts
|
||||
new file mode 100644
|
||||
index 0000000..930c2dc
|
||||
--- /dev/null
|
||||
+++ b/tests/rate-limit.test.ts
|
||||
@@ -0,0 +1,23 @@
|
||||
+import { describe, it, expect, vi, afterEach } from 'vitest';
|
||||
+import { rateLimit, _resetBuckets } from '../src/lib/rate-limit';
|
||||
+
|
||||
+afterEach(() => { _resetBuckets(); vi.useRealTimers(); });
|
||||
+
|
||||
+describe('rateLimit', () => {
|
||||
+ it('consente fino a max richieste', () => {
|
||||
+ expect(rateLimit('a', 2, 1000)).toBe(true);
|
||||
+ expect(rateLimit('a', 2, 1000)).toBe(true);
|
||||
+ expect(rateLimit('a', 2, 1000)).toBe(false);
|
||||
+ });
|
||||
+ it('chiavi indipendenti', () => {
|
||||
+ expect(rateLimit('a', 1, 1000)).toBe(true);
|
||||
+ expect(rateLimit('b', 1, 1000)).toBe(true);
|
||||
+ });
|
||||
+ it('la finestra scade', () => {
|
||||
+ vi.useFakeTimers();
|
||||
+ expect(rateLimit('a', 1, 1000)).toBe(true);
|
||||
+ expect(rateLimit('a', 1, 1000)).toBe(false);
|
||||
+ vi.advanceTimersByTime(1100);
|
||||
+ expect(rateLimit('a', 1, 1000)).toBe(true);
|
||||
+ });
|
||||
+});
|
||||
diff --git a/tests/slug.test.ts b/tests/slug.test.ts
|
||||
new file mode 100644
|
||||
index 0000000..5ca5bed
|
||||
--- /dev/null
|
||||
+++ b/tests/slug.test.ts
|
||||
@@ -0,0 +1,20 @@
|
||||
+import { describe, it, expect } from 'vitest';
|
||||
+import { slugify } from '../src/lib/slug';
|
||||
+
|
||||
+describe('slugify', () => {
|
||||
+ it('minuscole e trattini', () => {
|
||||
+ expect(slugify('Il Metodo InsanityLab')).toBe('il-metodo-insanitylab');
|
||||
+ });
|
||||
+ it('rimuove accenti', () => {
|
||||
+ expect(slugify('Attività fisica è già qui')).toBe('attivita-fisica-e-gia-qui');
|
||||
+ });
|
||||
+ it('comprime simboli e spazi multipli', () => {
|
||||
+ expect(slugify('Blog & Edugo -- news!!')).toBe('blog-edugo-news');
|
||||
+ });
|
||||
+ it('niente trattini ai bordi', () => {
|
||||
+ expect(slugify(' ciao ')).toBe('ciao');
|
||||
+ });
|
||||
+ it('stringa vuota resta vuota', () => {
|
||||
+ expect(slugify('')).toBe('');
|
||||
+ });
|
||||
+});
|
||||
Reference in New Issue
Block a user