feat: add lint

Signed-off-by: d0zingcat <iamtangli42@gmail.com>
This commit is contained in:
2026-01-14 19:36:46 +08:00
parent f414264050
commit 451793f6ce
41 changed files with 3724 additions and 3017 deletions

View File

@@ -1,28 +1,30 @@
export { };
export {};
// Simulate admin checking requests
async function run() {
console.log('Fetching pending topics as admin...');
const adminEmail = (process.env.ADMIN_EMAILS || '').split(',')[0].trim();
const res = await fetch('http://localhost:3000/api/topics/requests', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Cookie': `session=${encodeURIComponent(JSON.stringify({
id: 'admin_123',
name: 'Admin User',
email: adminEmail,
isAdmin: true
}))}`
}
});
console.log("Fetching pending topics as admin...");
const adminEmail = (process.env.ADMIN_EMAILS || "").split(",")[0].trim();
const res = await fetch("http://localhost:3000/api/topics/requests", {
method: "GET",
headers: {
"Content-Type": "application/json",
Cookie: `session=${encodeURIComponent(
JSON.stringify({
id: "admin_123",
name: "Admin User",
email: adminEmail,
isAdmin: true,
}),
)}`,
},
});
if (res.ok) {
const data = await res.json();
console.log('Pending topics:', JSON.stringify(data, null, 2));
} else {
console.log('Error:', res.status, await res.text());
}
if (res.ok) {
const data = await res.json();
console.log("Pending topics:", JSON.stringify(data, null, 2));
} else {
console.log("Error:", res.status, await res.text());
}
}
run();

View File

@@ -1,28 +1,30 @@
export { };
export {};
async function run() {
console.log('Fetching dashboard stats as admin...');
const adminEmail = (process.env.ADMIN_EMAILS || '').split(',')[0].trim();
const res = await fetch('http://localhost:3000/api/dashboard/stats', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
// Admin cookie
'Cookie': `session=${encodeURIComponent(JSON.stringify({
id: 'admin_123',
name: 'Admin User',
email: adminEmail,
isAdmin: true
}))}`
}
});
console.log("Fetching dashboard stats as admin...");
const adminEmail = (process.env.ADMIN_EMAILS || "").split(",")[0].trim();
const res = await fetch("http://localhost:3000/api/dashboard/stats", {
method: "GET",
headers: {
"Content-Type": "application/json",
// Admin cookie
Cookie: `session=${encodeURIComponent(
JSON.stringify({
id: "admin_123",
name: "Admin User",
email: adminEmail,
isAdmin: true,
}),
)}`,
},
});
if (res.ok) {
const data = await res.json();
console.log('Dashboard Stats:', JSON.stringify(data, null, 2));
} else {
console.log('Error:', res.status, await res.text());
}
if (res.ok) {
const data = await res.json();
console.log("Dashboard Stats:", JSON.stringify(data, null, 2));
} else {
console.log("Error:", res.status, await res.text());
}
}
run();

View File

@@ -1,9 +1,10 @@
import { Database } from 'bun:sqlite';
const db = new Database('dev.db');
import { Database } from "bun:sqlite";
const db = new Database("dev.db");
try {
const query = db.query("SELECT * FROM topics");
const topics = query.all();
console.log('Topics:', JSON.stringify(topics, null, 2));
const query = db.query("SELECT * FROM topics");
const topics = query.all();
console.log("Topics:", JSON.stringify(topics, null, 2));
} catch (e) {
console.error('Error querying topics:', e);
console.error("Error querying topics:", e);
}

View File

@@ -1,40 +1,41 @@
// Simulate topic creation
import { client } from './client'; // This won't work in node script easily due to frontend dependencies
import { client } from "./client"; // This won't work in node script easily due to frontend dependencies
// Let's use fetch directly against the server
async function run() {
console.log('Creating pending topic...');
const res = await fetch('http://localhost:3000/api/topics', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// We need to bake a cookie.
// But we can't easily bake a signed cookie without the secret.
// Wait, the cookies are not signed in the strict sense, just set.
// But `middleware.ts` parses `JSON.parse(sessionCookie)`.
console.log("Creating pending topic...");
const res = await fetch("http://localhost:3000/api/topics", {
method: "POST",
headers: {
"Content-Type": "application/json",
// We need to bake a cookie.
// But we can't easily bake a signed cookie without the secret.
// Wait, the cookies are not signed in the strict sense, just set.
// But `middleware.ts` parses `JSON.parse(sessionCookie)`.
// Let's fake a session cookie for a non-admin user.
'Cookie': `session=${encodeURIComponent(JSON.stringify({
id: 'user_123',
name: 'Test User',
email: 'test@example.com',
isAdmin: false
}))}`
},
body: JSON.stringify({
name: 'Test Pending Topic',
slug: 'test-pending',
description: 'This should be pending'
})
});
// Let's fake a session cookie for a non-admin user.
Cookie: `session=${encodeURIComponent(
JSON.stringify({
id: "user_123",
name: "Test User",
email: "test@example.com",
isAdmin: false,
}),
)}`,
},
body: JSON.stringify({
name: "Test Pending Topic",
slug: "test-pending",
description: "This should be pending",
}),
});
if (res.ok) {
const data = await res.json();
console.log('Created topic:', data);
} else {
console.log('Error:', res.status, await res.text());
}
if (res.ok) {
const data = await res.json();
console.log("Created topic:", data);
} else {
console.log("Error:", res.status, await res.text());
}
}
run();

View File

@@ -1,69 +1,78 @@
import postgres from 'postgres';
import postgres from "postgres";
const sql = postgres('postgres://localhost:5432/alertmessagecenter');
const sql = postgres("postgres://localhost:5432/alertmessagecenter");
async function run() {
try {
// 1. Get a topic
const [topic] = await sql`SELECT * FROM topics LIMIT 1`;
if (!topic) {
console.log('No topics found. Create a topic first.');
return;
}
console.log('Using topic:', topic.id, topic.slug);
try {
// 1. Get a topic
const [topic] = await sql`SELECT * FROM topics LIMIT 1`;
if (!topic) {
console.log("No topics found. Create a topic first.");
return;
}
console.log("Using topic:", topic.id, topic.slug);
// 2. Define a fake user ID
const fakeUserId = 'user_fake_002';
// 2. Define a fake user ID
const fakeUserId = "user_fake_002";
// Clean up first
await sql`DELETE FROM subscriptions WHERE user_id = ${fakeUserId}`;
await sql`DELETE FROM users WHERE id = ${fakeUserId}`;
// Clean up first
await sql`DELETE FROM subscriptions WHERE user_id = ${fakeUserId}`;
await sql`DELETE FROM users WHERE id = ${fakeUserId}`;
// 3. Try to subscribe with non-existent user
console.log('\n--- Attempt 1: Subscribe with non-existent user ---');
const res1 = await fetch(`http://localhost:3000/api/topics/${topic.id}/subscribe/${fakeUserId}`, {
method: 'POST',
headers: {
'Cookie': `session=${encodeURIComponent(JSON.stringify({
id: fakeUserId,
name: 'Fake User',
email: 'fake@example.com',
isAdmin: false
}))}`
}
});
console.log('Status:', res1.status);
const text1 = await res1.text();
console.log('Response:', text1); // Expect 500 FK violation
// 3. Try to subscribe with non-existent user
console.log("\n--- Attempt 1: Subscribe with non-existent user ---");
const res1 = await fetch(
`http://localhost:3000/api/topics/${topic.id}/subscribe/${fakeUserId}`,
{
method: "POST",
headers: {
Cookie: `session=${encodeURIComponent(
JSON.stringify({
id: fakeUserId,
name: "Fake User",
email: "fake@example.com",
isAdmin: false,
}),
)}`,
},
},
);
console.log("Status:", res1.status);
const text1 = await res1.text();
console.log("Response:", text1); // Expect 500 FK violation
// 4. Create the user
console.log('\n--- Creating user... ---');
await sql`INSERT INTO users (id, name, feishu_user_id, email, is_admin)
// 4. Create the user
console.log("\n--- Creating user... ---");
await sql`INSERT INTO users (id, name, feishu_user_id, email, is_admin)
VALUES (${fakeUserId}, 'Fake User', 'ou_fake', 'fake2@example.com', false)
ON CONFLICT (id) DO NOTHING`;
// 5. Try to subscribe again
console.log('\n--- Attempt 2: Subscribe with existing user ---');
const res2 = await fetch(`http://localhost:3000/api/topics/${topic.id}/subscribe/${fakeUserId}`, {
method: 'POST',
headers: {
'Cookie': `session=${encodeURIComponent(JSON.stringify({
id: fakeUserId,
name: 'Fake User',
email: 'fake@example.com',
isAdmin: false
}))}`
}
});
console.log('Status:', res2.status);
const text2 = await res2.text();
console.log('Response:', text2); // Expect 200
} catch (e) {
console.error(e);
} finally {
await sql.end();
}
// 5. Try to subscribe again
console.log("\n--- Attempt 2: Subscribe with existing user ---");
const res2 = await fetch(
`http://localhost:3000/api/topics/${topic.id}/subscribe/${fakeUserId}`,
{
method: "POST",
headers: {
Cookie: `session=${encodeURIComponent(
JSON.stringify({
id: fakeUserId,
name: "Fake User",
email: "fake@example.com",
isAdmin: false,
}),
)}`,
},
},
);
console.log("Status:", res2.status);
const text2 = await res2.text();
console.log("Response:", text2); // Expect 200
} catch (e) {
console.error(e);
} finally {
await sql.end();
}
}
run();