From 933e6018160eb419c3707d7673e8d29f74c8e1bf Mon Sep 17 00:00:00 2001 From: Syngnat Date: Mon, 13 Jul 2026 12:55:22 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(snippet):=20=E4=BF=9D?= =?UTF-8?q?=E7=95=99=E4=BB=A3=E7=A0=81=E7=89=87=E6=AE=B5=E5=B0=BE=E9=83=A8?= =?UTF-8?q?=E7=A9=BA=E7=99=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 持久化时完整保留代码片段正文中的尾部空格与换行 - 继续拒绝仅包含空白字符的无效片段 - 补充保存、存储及重新加载后的正文一致性测试 --- frontend/src/store.test.ts | 25 +++++++++++++++++++++++++ frontend/src/store.ts | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/frontend/src/store.test.ts b/frontend/src/store.test.ts index 91874196..c0b20673 100644 --- a/frontend/src/store.test.ts +++ b/frontend/src/store.test.ts @@ -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); + }); }); diff --git a/frontend/src/store.ts b/frontend/src/store.ts index fb61d196..f9eeeba5 100644 --- a/frontend/src/store.ts +++ b/frontend/src/store.ts @@ -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",