🐛 fix(snippet): 保留代码片段尾部空白

- 持久化时完整保留代码片段正文中的尾部空格与换行
- 继续拒绝仅包含空白字符的无效片段
- 补充保存、存储及重新加载后的正文一致性测试
This commit is contained in:
Syngnat
2026-07-13 12:55:22 +08:00
parent b23e768973
commit 933e601816
2 changed files with 27 additions and 2 deletions

View File

@@ -2268,4 +2268,29 @@ describe('store appearance persistence', () => {
expect(persistedSnippets).toHaveLength(1);
expect(persistedSnippets[0].syntaxHelp).toBe('新说明:目标表、数据源、关联字段均可修改');
});
it('preserves custom SQL snippet body whitespace across reloads', async () => {
const { useStore } = await importStore();
const body = 'SELECT ${1:columns} FROM ${2:table_name}\n ';
useStore.getState().saveSqlSnippet({
id: 'custom-trailing-whitespace',
prefix: 'trail',
name: 'Trailing whitespace',
body,
isBuiltin: false,
createdAt: 1710000000000,
});
expect(useStore.getState().sqlSnippets.find((snippet) => snippet.id === 'custom-trailing-whitespace')?.body)
.toBe(body);
const persisted = JSON.parse(storage.getItem('lite-db-storage') || '{}');
expect(persisted.state.sqlSnippets.find((snippet: { id: string }) => snippet.id === 'custom-trailing-whitespace')?.body)
.toBe(body);
vi.resetModules();
const reloaded = await importStore();
expect(reloaded.useStore.getState().sqlSnippets.find((snippet) => snippet.id === 'custom-trailing-whitespace')?.body)
.toBe(body);
});
});

View File

@@ -1664,8 +1664,8 @@ const sanitizeSqlSnippets = (value: unknown): SqlSnippet[] => {
.toLowerCase()
.replace(/[^a-z0-9_]/g, "")
.slice(0, 20);
const body = toTrimmedString(raw.body);
if (!prefix || !body) return;
const body = typeof raw.body === "string" ? raw.body : "";
if (!prefix || !body.trim()) return;
const id = toTrimmedString(raw.id, `snippet-${index + 1}`) || `snippet-${index + 1}`;
const fallbackName = indexedStoreFallback(
"store.fallback.sql_snippet_name",