IOS小组件/文案
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import type { UserProfileScoring } from '@/src/storage/appStorage';
|
||||
|
||||
import { advanceSuixinState, buildInitialSuixinState, computeSuixinSolidColor, pickSuixinBaseThemeId } from '../index';
|
||||
import { computeTFromStep } from '../progress';
|
||||
import { lerpHex } from '../colorMath';
|
||||
|
||||
function buildProfile(partial?: Partial<UserProfileScoring>): UserProfileScoring {
|
||||
const base: UserProfileScoring = {
|
||||
profile_version: 'v1.2',
|
||||
profile_source: 'questionnaire',
|
||||
profile_generated_at: '2026-01-30T00:00:00Z',
|
||||
profile_confidence: 1.0,
|
||||
profile_answered: { stage: true, emotion: true, context: true, need: true },
|
||||
stage: { expecting: 1, parenting: 0, unknown: 0 },
|
||||
emotion_score: 0.6,
|
||||
context: {},
|
||||
need: {},
|
||||
rule_hits: [],
|
||||
hard_rules: { forbidden_risk_flags: [], forbidden_content_predicates: [] },
|
||||
};
|
||||
return { ...base, ...(partial ?? {}) };
|
||||
}
|
||||
|
||||
describe('suixinTheme', () => {
|
||||
it('pickSuixinBaseThemeId: stage.unknown=1 → neutral', () => {
|
||||
const p = buildProfile({ stage: { unknown: 1 } as any, need: { rest_balance: 1 } });
|
||||
expect(pickSuixinBaseThemeId(p)).toBe('neutral');
|
||||
});
|
||||
|
||||
it('pickSuixinBaseThemeId: need 为空 → neutral', () => {
|
||||
const p = buildProfile({ need: {} });
|
||||
expect(pickSuixinBaseThemeId(p)).toBe('neutral');
|
||||
});
|
||||
|
||||
it('pickSuixinBaseThemeId: need 命中 → 对应 base theme', () => {
|
||||
const p = buildProfile({ need: { rest_balance: 1 } });
|
||||
expect(pickSuixinBaseThemeId(p)).toBe('rest_balance');
|
||||
});
|
||||
|
||||
it('computeTFromStep: 始终在 [0,1] 且往返不突跳', () => {
|
||||
const ts = Array.from({ length: 80 }).map((_, i) => computeTFromStep({ stepIndex: i, segments: 12, seed: 'boot' }));
|
||||
for (const t of ts) {
|
||||
expect(t).toBeGreaterThanOrEqual(0);
|
||||
expect(t).toBeLessThanOrEqual(1);
|
||||
}
|
||||
// 往返波形:起点与一个周期后的 t 相同
|
||||
expect(computeTFromStep({ stepIndex: 0, segments: 12, seed: 'boot' })).toBe(
|
||||
computeTFromStep({ stepIndex: 22, segments: 12, seed: 'boot' }) // 2*(N-1)=22
|
||||
);
|
||||
});
|
||||
|
||||
it('lerpHex: t=0/1 输出边界色', () => {
|
||||
expect(lerpHex('#000000', '#FFFFFF', 0)).toBe('#000000');
|
||||
expect(lerpHex('#000000', '#FFFFFF', 1)).toBe('#FFFFFF');
|
||||
});
|
||||
|
||||
it('buildInitialSuixinState: 生成可用状态并可推进', () => {
|
||||
const p = buildProfile({ need: { emotional_support: 1 } });
|
||||
const init = buildInitialSuixinState({ bootId: 'boot-1', profile: p, now: new Date('2026-02-05T00:00:00Z') });
|
||||
expect(init.schema_version).toBe(1);
|
||||
expect(init.base_theme_id).toBe('emotional_support');
|
||||
expect(init.boot_id).toBe('boot-1');
|
||||
expect(init.last_color).toMatch(/^#[0-9A-F]{6}$/);
|
||||
|
||||
const next = advanceSuixinState(init, new Date('2026-02-05T00:00:01Z'));
|
||||
expect(next.step_index).toBe(1);
|
||||
expect(next.base_theme_id).toBe(init.base_theme_id);
|
||||
expect(next.last_color).toMatch(/^#[0-9A-F]{6}$/);
|
||||
});
|
||||
|
||||
it('computeSuixinSolidColor: 输出为合法 hex', () => {
|
||||
const c = computeSuixinSolidColor({ baseThemeId: 'neutral', stepIndex: 3, seed: 'boot' });
|
||||
expect(c).toMatch(/^#[0-9A-F]{6}$/);
|
||||
});
|
||||
});
|
||||
|
||||
53
client/src/features/suixinTheme/colorMath.ts
Normal file
53
client/src/features/suixinTheme/colorMath.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
function clamp01(t: number): number {
|
||||
if (!Number.isFinite(t)) return 0;
|
||||
return Math.min(1, Math.max(0, t));
|
||||
}
|
||||
|
||||
type Rgb = { r: number; g: number; b: number };
|
||||
|
||||
function toByte(v: number): number {
|
||||
if (!Number.isFinite(v)) return 0;
|
||||
return Math.min(255, Math.max(0, Math.round(v)));
|
||||
}
|
||||
|
||||
export function hexToRgb(hex: string): Rgb | null {
|
||||
const h = String(hex || '').trim();
|
||||
const m = /^#?([0-9a-fA-F]{6})$/.exec(h);
|
||||
if (!m) return null;
|
||||
const raw = m[1];
|
||||
const n = parseInt(raw, 16);
|
||||
// eslint-disable-next-line no-bitwise
|
||||
const r = (n >> 16) & 0xff;
|
||||
// eslint-disable-next-line no-bitwise
|
||||
const g = (n >> 8) & 0xff;
|
||||
// eslint-disable-next-line no-bitwise
|
||||
const b = n & 0xff;
|
||||
return { r, g, b };
|
||||
}
|
||||
|
||||
export function rgbToHex(rgb: Rgb): string {
|
||||
const r = toByte(rgb.r).toString(16).padStart(2, '0');
|
||||
const g = toByte(rgb.g).toString(16).padStart(2, '0');
|
||||
const b = toByte(rgb.b).toString(16).padStart(2, '0');
|
||||
return `#${r}${g}${b}`.toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅允许线性插值(Hard Rule)
|
||||
*/
|
||||
export function lerpRgb(a: Rgb, b: Rgb, t: number): Rgb {
|
||||
const tt = clamp01(t);
|
||||
return {
|
||||
r: a.r + (b.r - a.r) * tt,
|
||||
g: a.g + (b.g - a.g) * tt,
|
||||
b: a.b + (b.b - a.b) * tt,
|
||||
};
|
||||
}
|
||||
|
||||
export function lerpHex(topHex: string, bottomHex: string, t: number, fallbackHex = '#E8F1EC'): string {
|
||||
const top = hexToRgb(topHex);
|
||||
const bottom = hexToRgb(bottomHex);
|
||||
if (!top || !bottom) return fallbackHex;
|
||||
return rgbToHex(lerpRgb(top, bottom, t));
|
||||
}
|
||||
|
||||
65
client/src/features/suixinTheme/index.ts
Normal file
65
client/src/features/suixinTheme/index.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { SuixinBaseThemeId, SuixinThemeStateV1, UserProfileScoring } from '@/src/storage/appStorage';
|
||||
|
||||
import { getThemeTriplet, NEUTRAL_THEME_COLORS } from './palette';
|
||||
import { lerpHex } from './colorMath';
|
||||
import { computeTFromStep } from './progress';
|
||||
import { pickSuixinBaseThemeId } from './pickTheme';
|
||||
|
||||
export { NEUTRAL_THEME_COLORS } from './palette';
|
||||
export { pickSuixinBaseThemeId } from './pickTheme';
|
||||
|
||||
/**
|
||||
* 根据 base theme 与 step 计算当前背景纯色
|
||||
*/
|
||||
export function computeSuixinSolidColor(args: {
|
||||
baseThemeId: SuixinBaseThemeId;
|
||||
stepIndex: number;
|
||||
seed: string;
|
||||
}): string {
|
||||
const [top, _mid, bottom] = getThemeTriplet(args.baseThemeId);
|
||||
const t = computeTFromStep({ stepIndex: args.stepIndex, segments: 12, seed: args.seed });
|
||||
return lerpHex(top, bottom, t, NEUTRAL_THEME_COLORS[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化一份随心状态(在冷启动会话内锁定 base theme)
|
||||
*/
|
||||
export function buildInitialSuixinState(args: {
|
||||
bootId: string;
|
||||
profile: UserProfileScoring | null;
|
||||
now?: Date;
|
||||
}): SuixinThemeStateV1 {
|
||||
const now = args.now ?? new Date();
|
||||
const baseThemeId = pickSuixinBaseThemeId(args.profile);
|
||||
const seed = args.bootId;
|
||||
const step_index = 0;
|
||||
const last_color = computeSuixinSolidColor({ baseThemeId, stepIndex: step_index, seed });
|
||||
return {
|
||||
schema_version: 1,
|
||||
saved_at: now.toISOString(),
|
||||
boot_id: args.bootId,
|
||||
base_theme_id: baseThemeId,
|
||||
seed,
|
||||
step_index,
|
||||
last_color,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换文案时推进一步(不跨主题)
|
||||
*/
|
||||
export function advanceSuixinState(prev: SuixinThemeStateV1, now?: Date): SuixinThemeStateV1 {
|
||||
const nextStep = (Number.isFinite(prev.step_index) ? prev.step_index : 0) + 1;
|
||||
const last_color = computeSuixinSolidColor({
|
||||
baseThemeId: prev.base_theme_id,
|
||||
stepIndex: nextStep,
|
||||
seed: prev.seed,
|
||||
});
|
||||
return {
|
||||
...prev,
|
||||
saved_at: (now ?? new Date()).toISOString(),
|
||||
step_index: nextStep,
|
||||
last_color,
|
||||
};
|
||||
}
|
||||
|
||||
20
client/src/features/suixinTheme/palette.ts
Normal file
20
client/src/features/suixinTheme/palette.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import type { SuixinBaseThemeId } from '@/src/storage/appStorage';
|
||||
|
||||
/**
|
||||
* 随心主题色盘(与设计说明文档保持一致)
|
||||
*/
|
||||
export const BASE_THEME_COLORS: Record<Exclude<SuixinBaseThemeId, 'neutral'>, [string, string, string]> = {
|
||||
emotional_support: ['#F6DCE4', '#FFEFF4', '#FFF7FA'],
|
||||
parenting_pressure: ['#D6EAF5', '#EEF6FB', '#F8FCFF'],
|
||||
self_worth: ['#FFD8A8', '#FFE8C9', '#FFF6E5'],
|
||||
anxiety_relief: ['#DFF3EA', '#ECFBF6', '#F6FFFB'],
|
||||
rest_balance: ['#F2E6D8', '#FAF3EC', '#FFFDF9'],
|
||||
};
|
||||
|
||||
export const NEUTRAL_THEME_COLORS: [string, string, string] = ['#F4F7F2', '#E8F1EC', '#EDF4F8'];
|
||||
|
||||
export function getThemeTriplet(themeId: SuixinBaseThemeId): [string, string, string] {
|
||||
if (themeId === 'neutral') return NEUTRAL_THEME_COLORS;
|
||||
return BASE_THEME_COLORS[themeId];
|
||||
}
|
||||
|
||||
32
client/src/features/suixinTheme/pickTheme.ts
Normal file
32
client/src/features/suixinTheme/pickTheme.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { SuixinBaseThemeId, UserProfileScoring } from '@/src/storage/appStorage';
|
||||
|
||||
const ALL_NEED_THEME_IDS: ReadonlySet<string> = new Set([
|
||||
'emotional_support',
|
||||
'parenting_pressure',
|
||||
'self_worth',
|
||||
'anxiety_relief',
|
||||
'rest_balance',
|
||||
]);
|
||||
|
||||
/**
|
||||
* Base Theme 选择(Theme Picking)
|
||||
*
|
||||
* Hard Rules(对齐设计文档):
|
||||
* - mom_stage = unknown → 强制 Neutral
|
||||
* - need 跳过/缺失 → Neutral
|
||||
*/
|
||||
export function pickSuixinBaseThemeId(profile: UserProfileScoring | null | undefined): SuixinBaseThemeId {
|
||||
if (!profile) return 'neutral';
|
||||
|
||||
// Hard Rule:unknown → Neutral
|
||||
if (profile.stage?.unknown === 1) return 'neutral';
|
||||
|
||||
const needObj = profile.need ?? {};
|
||||
const keys = Object.keys(needObj);
|
||||
if (keys.length === 0) return 'neutral';
|
||||
|
||||
const needId = keys[0];
|
||||
if (!ALL_NEED_THEME_IDS.has(needId)) return 'neutral';
|
||||
return needId as Exclude<SuixinBaseThemeId, 'neutral'>;
|
||||
}
|
||||
|
||||
40
client/src/features/suixinTheme/progress.ts
Normal file
40
client/src/features/suixinTheme/progress.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
function clampInt(n: number, min: number, max: number): number {
|
||||
if (!Number.isFinite(n)) return min;
|
||||
return Math.min(max, Math.max(min, Math.floor(n)));
|
||||
}
|
||||
|
||||
function hashStringToInt32(input: string): number {
|
||||
// 简单可复现 hash:用于把 seed 映射为偏移量(不用于安全场景)
|
||||
let h = 0;
|
||||
for (let i = 0; i < input.length; i += 1) {
|
||||
// eslint-disable-next-line no-bitwise
|
||||
h = (h * 31 + input.charCodeAt(i)) | 0;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 t ∈ [0,1],用于 Color(t)=lerp(top,bottom,t)
|
||||
*
|
||||
* 说明:
|
||||
* - Home 没有 scroll,用“切换文案 step_index”模拟连续流动
|
||||
* - 采用往返波形,避免从 1 回到 0 的突跳
|
||||
*/
|
||||
export function computeTFromStep(args: {
|
||||
stepIndex: number;
|
||||
segments?: number; // N,默认 12
|
||||
seed?: string; // 允许用 seed 做初始相位偏移(同一次冷启动内稳定)
|
||||
}): number {
|
||||
const N = clampInt(args.segments ?? 12, 3, 60);
|
||||
const stepIndex = clampInt(args.stepIndex, 0, 1_000_000_000);
|
||||
const period = 2 * (N - 1);
|
||||
|
||||
const seed = String(args.seed ?? '');
|
||||
const offset = seed ? Math.abs(hashStringToInt32(seed)) % period : 0;
|
||||
|
||||
const phase = (stepIndex + offset) % period;
|
||||
const up = phase <= (N - 1);
|
||||
const pos = up ? phase : period - phase;
|
||||
return pos / (N - 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user