feat(admin): add image quota management UI on memind_adm 5174
Add image-quota admin pages and API wiring for plan defaults, ledger, and per-user grants, plus local verify scripts and AGENTS.md note that this repo is the sole admin UI surface. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Local smoke test for image-quota admin API (memind_adm 8085).
|
||||
* Usage: node scripts/verify-image-quota-local.mjs
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const base = process.env.ADM_DEV_BACKEND ?? 'http://127.0.0.1:8085';
|
||||
|
||||
function loadEnvFile(filePath) {
|
||||
if (!fs.existsSync(filePath)) return;
|
||||
for (const line of fs.readFileSync(filePath, 'utf8').split('\n')) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith('#')) continue;
|
||||
const eq = trimmed.indexOf('=');
|
||||
if (eq <= 0) continue;
|
||||
const key = trimmed.slice(0, eq).trim();
|
||||
let value = trimmed.slice(eq + 1).trim();
|
||||
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
||||
value = value.slice(1, -1);
|
||||
}
|
||||
if (!(key in process.env)) process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
loadEnvFile(path.join(root, '.env'));
|
||||
|
||||
const username = process.env.H5_ADMIN_USERNAME?.trim() || 'admin';
|
||||
const password = process.env.H5_ADMIN_PASSWORD?.trim() || process.env.ADMIN_PASSWORD?.trim() || '';
|
||||
|
||||
async function request(method, urlPath, { body, cookie } = {}) {
|
||||
const res = await fetch(`${base}${urlPath}`, {
|
||||
method,
|
||||
headers: {
|
||||
...(body ? { 'Content-Type': 'application/json' } : {}),
|
||||
...(cookie ? { Cookie: cookie } : {}),
|
||||
},
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
const text = await res.text();
|
||||
let json = null;
|
||||
try {
|
||||
json = text ? JSON.parse(text) : null;
|
||||
} catch {
|
||||
json = { raw: text.slice(0, 200) };
|
||||
}
|
||||
return { status: res.status, json, setCookie: res.headers.getSetCookie?.() ?? [] };
|
||||
}
|
||||
|
||||
function cookieFromSetCookie(setCookie) {
|
||||
return setCookie.map((c) => c.split(';')[0]).join('; ');
|
||||
}
|
||||
|
||||
function assertOk(cond, msg) {
|
||||
if (!cond) throw new Error(msg);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log(`==> Admin API base: ${base}`);
|
||||
|
||||
if (!password) {
|
||||
console.error('缺少 H5_ADMIN_PASSWORD(请在 memind_adm/.env 配置)');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const login = await request('POST', '/auth/login', {
|
||||
body: { username, password },
|
||||
});
|
||||
assertOk(login.status === 200, `登录失败 HTTP ${login.status}: ${JSON.stringify(login.json)}`);
|
||||
const cookie = cookieFromSetCookie(login.setCookie);
|
||||
assertOk(cookie, '登录未返回 session cookie');
|
||||
console.log(`✓ 管理员登录 (${username})`);
|
||||
|
||||
const config = await request('GET', '/admin-api/image-quota/config', { cookie });
|
||||
assertOk(config.status === 200, `config HTTP ${config.status}`);
|
||||
assertOk(Array.isArray(config.json?.plans), 'config.plans 应为数组');
|
||||
assertOk(config.json.plans.length > 0, 'config.plans 不应为空');
|
||||
console.log(`✓ GET /admin-api/image-quota/config (${config.json.plans.length} 套餐)`);
|
||||
|
||||
const users = await request('GET', '/admin-api/users?page=1&pageSize=5&role=user', { cookie });
|
||||
assertOk(users.status === 200, `users HTTP ${users.status}`);
|
||||
const sampleUser = users.json?.users?.[0];
|
||||
assertOk(sampleUser?.id, '需要至少一个 user 账号做用户级测试');
|
||||
console.log(`✓ 样本用户: ${sampleUser.username} (${sampleUser.id})`);
|
||||
|
||||
const userQuota = await request('GET', `/admin-api/users/${sampleUser.id}/image-quota`, { cookie });
|
||||
assertOk(userQuota.status === 200, `user image-quota HTTP ${userQuota.status}`);
|
||||
assertOk(userQuota.json?.quota, '应返回 quota 对象');
|
||||
console.log(
|
||||
`✓ GET user image-quota: remaining=${userQuota.json.quota.remaining ?? '∞'} unlimited=${userQuota.json.quota.unlimited}`,
|
||||
);
|
||||
|
||||
const ledger = await request('GET', '/admin-api/image-quota/ledger?page=1&pageSize=5', { cookie });
|
||||
assertOk(ledger.status === 200, `ledger HTTP ${ledger.status}`);
|
||||
assertOk(Array.isArray(ledger.json?.entries), 'ledger.entries 应为数组');
|
||||
console.log(`✓ GET /admin-api/image-quota/ledger (${ledger.json.total} 条)`);
|
||||
|
||||
const grant = await request('POST', `/admin-api/users/${sampleUser.id}/image-quota/grant`, {
|
||||
cookie,
|
||||
body: { delta: 1, note: 'local-verify-image-quota' },
|
||||
});
|
||||
assertOk(grant.status === 200, `grant HTTP ${grant.status}: ${JSON.stringify(grant.json)}`);
|
||||
assertOk(grant.json?.quota, 'grant 应返回更新后的 quota');
|
||||
console.log(`✓ POST grant +1 → remaining=${grant.json.quota.remaining ?? '∞'}`);
|
||||
|
||||
const revoke = await request('POST', `/admin-api/users/${sampleUser.id}/image-quota/grant`, {
|
||||
cookie,
|
||||
body: { delta: -1, note: 'local-verify-image-quota-rollback' },
|
||||
});
|
||||
assertOk(revoke.status === 200, `rollback grant HTTP ${revoke.status}`);
|
||||
console.log('✓ POST grant -1 回滚测试额度');
|
||||
|
||||
console.log('\n全部 image-quota Admin API 联调通过。');
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error('\n联调失败:', err.message);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env node
|
||||
/** Portal smoke: /auth/me subscription includes image quota fields */
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const memindRoot = path.join(path.dirname(fileURLToPath(import.meta.url)), '../..', 'Memind');
|
||||
const portalBase = process.env.PORTAL_BASE ?? 'http://127.0.0.1:8081';
|
||||
|
||||
function loadEnvFile(filePath) {
|
||||
if (!fs.existsSync(filePath)) return;
|
||||
for (const line of fs.readFileSync(filePath, 'utf8').split('\n')) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith('#')) continue;
|
||||
const eq = trimmed.indexOf('=');
|
||||
if (eq <= 0) continue;
|
||||
const key = trimmed.slice(0, eq).trim();
|
||||
let value = trimmed.slice(eq + 1).trim();
|
||||
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
||||
value = value.slice(1, -1);
|
||||
}
|
||||
if (!(key in process.env)) process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
loadEnvFile(path.join(memindRoot, '.env'));
|
||||
|
||||
const username = process.env.H5_TEST_USERNAME?.trim() || process.env.H5_ADMIN_USERNAME?.trim() || 'admin';
|
||||
const password = process.env.H5_TEST_PASSWORD?.trim() || process.env.H5_ADMIN_PASSWORD?.trim() || '';
|
||||
|
||||
async function request(method, urlPath, { body, cookie } = {}) {
|
||||
const res = await fetch(`${portalBase}${urlPath}`, {
|
||||
method,
|
||||
headers: {
|
||||
...(body ? { 'Content-Type': 'application/json' } : {}),
|
||||
...(cookie ? { Cookie: cookie } : {}),
|
||||
},
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
const text = await res.text();
|
||||
let json = null;
|
||||
try { json = text ? JSON.parse(text) : null; } catch { json = { raw: text.slice(0, 200) }; }
|
||||
return { status: res.status, json, setCookie: res.headers.getSetCookie?.() ?? [] };
|
||||
}
|
||||
|
||||
function cookieFromSetCookie(setCookie) {
|
||||
return setCookie.map((c) => c.split(';')[0]).join('; ');
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log(`==> Portal base: ${portalBase}`);
|
||||
if (!password) {
|
||||
console.error('缺少登录密码(Memind/.env 中 H5_ADMIN_PASSWORD 或 H5_TEST_PASSWORD)');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const login = await request('POST', '/auth/login', { body: { username, password } });
|
||||
if (login.status !== 200) {
|
||||
console.error(`登录失败 HTTP ${login.status}`);
|
||||
process.exit(1);
|
||||
}
|
||||
const cookie = cookieFromSetCookie(login.setCookie);
|
||||
console.log(`✓ Portal 登录 (${username})`);
|
||||
|
||||
const me = await request('GET', '/auth/me', { cookie });
|
||||
if (me.status !== 200) {
|
||||
console.error(`/auth/me HTTP ${me.status}`);
|
||||
process.exit(1);
|
||||
}
|
||||
const sub = me.json?.user?.subscription;
|
||||
if (!sub) {
|
||||
console.log('⚠ 当前用户无 active subscription(免费用户可能无订阅记录)');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const fields = ['periodImagesLimit', 'periodImagesUsed', 'periodImagesBonus'];
|
||||
for (const f of fields) {
|
||||
if (!(f in sub)) {
|
||||
console.error(`subscription 缺少字段: ${f}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
console.log(
|
||||
`✓ /auth/me subscription 图片额度: limit=${sub.periodImagesLimit} bonus=${sub.periodImagesBonus ?? 0} used=${sub.periodImagesUsed}`,
|
||||
);
|
||||
console.log('\nPortal 用户侧 subscription 字段联调通过。');
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error('联调失败:', err.message);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user