|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#30335] Setup → Storage → 测试连接 answers in the caller's language. |
| 5 | + * |
| 6 | + * The console prints the `storage/test` action's `message` as the toast, bare. |
| 7 | + * The settings action route hands the handler the caller's display language as |
| 8 | + * the input's `locale` (`Accept-Language`, else `?locale`), the same source the |
| 9 | + * mail test notice reads. These pins run the REAL handler the plugin registers, |
| 10 | + * on a real local adapter, with that input member set or absent. |
| 11 | + * |
| 12 | + * `ok`, `severity`, the adapter name and the probe's cleanup are not the |
| 13 | + * subject: each case asserts the first two are what they were, so a |
| 14 | + * translation can never have moved a verdict. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, expect, it } from 'vitest'; |
| 18 | +import { promises as fs } from 'node:fs'; |
| 19 | +import { tmpdir } from 'node:os'; |
| 20 | +import { join } from 'node:path'; |
| 21 | +import { StorageServicePlugin } from './storage-service-plugin.js'; |
| 22 | +import { LocalStorageAdapter } from './local-storage-adapter.js'; |
| 23 | +import { storageTestCopy, STORAGE_TEST_COPY } from './storage-test-copy.js'; |
| 24 | + |
| 25 | +function makeCtx() { |
| 26 | + const services = new Map<string, unknown>(); |
| 27 | + const hooks: Array<() => Promise<void> | void> = []; |
| 28 | + const ctx: any = { |
| 29 | + logger: { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} }, |
| 30 | + registerService: (name: string, svc: unknown) => { services.set(name, svc); }, |
| 31 | + getService: <T>(name: string): T => { |
| 32 | + const s = services.get(name); |
| 33 | + if (!s) throw new Error(`service '${name}' not registered`); |
| 34 | + return s as T; |
| 35 | + }, |
| 36 | + hook: (event: string, fn: () => Promise<void> | void) => { |
| 37 | + if (event === 'kernel:ready') hooks.push(fn); |
| 38 | + }, |
| 39 | + _flushReady: async () => { for (const h of hooks) await h(); }, |
| 40 | + }; |
| 41 | + return ctx; |
| 42 | +} |
| 43 | + |
| 44 | +/** A settings host that keeps the registered action so a test can run it. */ |
| 45 | +function makeSettingsHost() { |
| 46 | + const actions = new Map<string, (input: unknown) => Promise<any>>(); |
| 47 | + return { |
| 48 | + createClient: (_ns: string) => ({}), |
| 49 | + getNamespace: async (_ns: string) => ({ values: {} }), |
| 50 | + subscribe: (_ns: string, _fn: () => void) => {}, |
| 51 | + registerAction: (ns: string, id: string, fn: (input: unknown) => Promise<any>) => { |
| 52 | + actions.set(`${ns}/${id}`, fn); |
| 53 | + }, |
| 54 | + run: async (input: Record<string, unknown>) => { |
| 55 | + const fn = actions.get('storage/test'); |
| 56 | + if (!fn) throw new Error('storage/test was not registered'); |
| 57 | + return await fn({ namespace: 'storage', actionId: 'test', ctx: {}, ...input }); |
| 58 | + }, |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +async function bootPlugin() { |
| 63 | + const root = await fs.mkdtemp(join(tmpdir(), 'oss-30335-')); |
| 64 | + const plugin = new StorageServicePlugin({ adapter: 'local', local: { rootDir: root }, registerRoutes: false }); |
| 65 | + const ctx = makeCtx(); |
| 66 | + const settings = makeSettingsHost(); |
| 67 | + ctx.registerService('settings', settings); |
| 68 | + await plugin.init(ctx); |
| 69 | + await plugin.start(ctx); |
| 70 | + await ctx._flushReady(); |
| 71 | + return { root, settings, plugin }; |
| 72 | +} |
| 73 | + |
| 74 | +/** The local adapter with a download that answers bytes other than the ones uploaded. */ |
| 75 | +function corruptDownloads(): () => void { |
| 76 | + const original = LocalStorageAdapter.prototype.download; |
| 77 | + LocalStorageAdapter.prototype.download = async function (this: LocalStorageAdapter, key: string) { |
| 78 | + void key; |
| 79 | + return Buffer.from('not the probe', 'utf-8'); |
| 80 | + } as typeof original; |
| 81 | + return () => { LocalStorageAdapter.prototype.download = original; }; |
| 82 | +} |
| 83 | + |
| 84 | +describe('[#30335] storage/test answers in the caller\'s language', () => { |
| 85 | + it('zh-CN: the success sentence is Chinese and names the adapter; the verdict is unchanged', async () => { |
| 86 | + const { root, settings } = await bootPlugin(); |
| 87 | + const result = await settings.run({ values: { adapter: 'local', local_root: root }, locale: 'zh-CN' }); |
| 88 | + expect(result.ok).toBe(true); |
| 89 | + expect(result.severity).toBe('info'); |
| 90 | + expect(result.message).toBe('存储连接测试成功(适配器:local)。'); |
| 91 | + expect(result.message).not.toContain('round-trip'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('en: the success sentence is the English one the console showed before', async () => { |
| 95 | + const { root, settings } = await bootPlugin(); |
| 96 | + const result = await settings.run({ values: { adapter: 'local', local_root: root }, locale: 'en-US' }); |
| 97 | + expect(result.ok).toBe(true); |
| 98 | + expect(result.severity).toBe('info'); |
| 99 | + expect(result.message).toBe('Storage round-trip succeeded (adapter=local).'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('no locale, or one with no copy: English, exactly as an older settings host gets it', async () => { |
| 103 | + const { root, settings } = await bootPlugin(); |
| 104 | + const values = { adapter: 'local', local_root: root }; |
| 105 | + for (const input of [{ values }, { values, locale: 'fr-FR' }, { values, locale: '' }]) { |
| 106 | + const result = await settings.run(input); |
| 107 | + expect(result.ok).toBe(true); |
| 108 | + expect(result.severity).toBe('info'); |
| 109 | + expect(result.message).toBe('Storage round-trip succeeded (adapter=local).'); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + it('a language tag without the region still picks its copy (zh → zh-CN)', async () => { |
| 114 | + const { root, settings } = await bootPlugin(); |
| 115 | + const result = await settings.run({ values: { adapter: 'local', local_root: root }, locale: 'zh' }); |
| 116 | + expect(result.message).toBe('存储连接测试成功(适配器:local)。'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('zh-CN: a download that does not match the upload is refused in Chinese', async () => { |
| 120 | + const { root, settings } = await bootPlugin(); |
| 121 | + const restore = corruptDownloads(); |
| 122 | + try { |
| 123 | + const result = await settings.run({ values: { adapter: 'local', local_root: root }, locale: 'zh-CN' }); |
| 124 | + expect(result.ok).toBe(false); |
| 125 | + expect(result.severity).toBe('error'); |
| 126 | + expect(result.message).toBe('探测文件下载后的内容与上传的不一致。'); |
| 127 | + } finally { |
| 128 | + restore(); |
| 129 | + } |
| 130 | + // The probe object is still removed on this path (#13726). |
| 131 | + const left = await fs.readdir(join(root, '__objectstack_probe__')).catch(() => []); |
| 132 | + expect(left).toEqual([]); |
| 133 | + }); |
| 134 | + |
| 135 | + it('zh-CN: incomplete S3 settings are refused in Chinese, naming the two fields', async () => { |
| 136 | + const { settings } = await bootPlugin(); |
| 137 | + const result = await settings.run({ values: { adapter: 's3', s3_bucket: '', s3_region: '' }, locale: 'zh-CN' }); |
| 138 | + expect(result.ok).toBe(false); |
| 139 | + expect(result.severity).toBe('error'); |
| 140 | + expect(result.message).toBe('S3 适配器需要填写存储桶(s3_bucket)和区域(s3_region)。'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('no locale: incomplete S3 settings keep the English refusal', async () => { |
| 144 | + const { settings } = await bootPlugin(); |
| 145 | + const result = await settings.run({ values: { adapter: 's3', s3_bucket: '', s3_region: '' } }); |
| 146 | + expect(result.ok).toBe(false); |
| 147 | + expect(result.message).toBe('StorageServicePlugin: S3 adapter requires s3_bucket and s3_region'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('zh-CN: the adapter\'s own error text stays verbatim behind a Chinese lead-in', async () => { |
| 151 | + const { settings } = await bootPlugin(); |
| 152 | + // A root the process cannot create: a path under a regular FILE. |
| 153 | + const file = join(await fs.mkdtemp(join(tmpdir(), 'oss-30335-file-')), 'not-a-dir'); |
| 154 | + await fs.writeFile(file, 'x'); |
| 155 | + const zh = await settings.run({ values: { adapter: 'local', local_root: join(file, 'root') }, locale: 'zh-CN' }); |
| 156 | + const en = await settings.run({ values: { adapter: 'local', local_root: join(file, 'root') } }); |
| 157 | + expect(zh.ok).toBe(false); |
| 158 | + expect(zh.severity).toBe('error'); |
| 159 | + expect(en.ok).toBe(false); |
| 160 | + // English stays the adapter's sentence alone; Chinese is lead-in + that same sentence. |
| 161 | + expect(en.message.length).toBeGreaterThan(0); |
| 162 | + expect(zh.message).toBe(`存储连接测试失败:${en.message}`); |
| 163 | + }); |
| 164 | +}); |
| 165 | + |
| 166 | +describe('[#30335] the storage/test copy table', () => { |
| 167 | + it('serves every locale the package\'s translations serve, each with every sentence', () => { |
| 168 | + expect(Object.keys(STORAGE_TEST_COPY).sort()).toEqual(['en', 'es-ES', 'ja-JP', 'zh-CN']); |
| 169 | + for (const locale of Object.keys(STORAGE_TEST_COPY)) { |
| 170 | + const copy = storageTestCopy(locale); |
| 171 | + expect(copy.succeeded('s3')).toContain('s3'); |
| 172 | + expect(copy.downloadMismatch.length).toBeGreaterThan(0); |
| 173 | + expect(copy.s3SettingsIncomplete).toContain('s3_bucket'); |
| 174 | + expect(copy.s3SettingsIncomplete).toContain('s3_region'); |
| 175 | + expect(copy.failed('EACCES')).toContain('EACCES'); |
| 176 | + } |
| 177 | + }); |
| 178 | +}); |
0 commit comments