|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `os plugin publish` uploads the compiled manifest from the one decode |
| 5 | + * `readCompiledPluginManifest` makes (#27914): the `plugin_manifest` payload |
| 6 | + * is the archive entry's own document, key for key, and the door does not |
| 7 | + * depend on the CLI's `MANIFEST_FILENAME` spelling the runtime's |
| 8 | + * `COMPILED_PLUGIN_MANIFEST_FILE`. |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, it, expect, afterEach, vi } from 'vitest'; |
| 12 | +import { mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 13 | +import { tmpdir } from 'node:os'; |
| 14 | +import { join } from 'node:path'; |
| 15 | + |
| 16 | +// The CLI's constant spelled differently from the runtime's, as a drift |
| 17 | +// between the two packages would leave it. |
| 18 | +vi.mock('../src/utils/osplugin.js', async (importOriginal) => ({ |
| 19 | + ...(await importOriginal<typeof import('../src/utils/osplugin.js')>()), |
| 20 | + MANIFEST_FILENAME: 'objectstack.plugin.drifted.json', |
| 21 | +})); |
| 22 | + |
| 23 | +import { createTarGz, type ArchiveFile } from '../src/utils/osplugin.js'; |
| 24 | +import PluginPublish from '../src/commands/plugin/publish.js'; |
| 25 | + |
| 26 | +// The runtime's spelling (`COMPILED_PLUGIN_MANIFEST_FILE`), which the artifact uses. |
| 27 | +const RUNTIME_MANIFEST_FILE = 'objectstack.plugin.json'; |
| 28 | + |
| 29 | +// No `integrity` map: the preflight's exemption list is spelled with the |
| 30 | +// CLI constant, and this file is about the manifest read only. |
| 31 | +const manifest = { |
| 32 | + version: '1.2.0', id: 'com.acme.demo', name: 'Demo', type: 'plugin', |
| 33 | + main: 'dist/index.mjs', packaging: 'bundled', runtime: 'sandbox', |
| 34 | + permissions: { services: ['object'] }, |
| 35 | +}; |
| 36 | +const manifestText = JSON.stringify(manifest, null, 4); |
| 37 | + |
| 38 | +describe('os plugin publish: the uploaded manifest is the entry it decoded once', () => { |
| 39 | + let dir: string | undefined; |
| 40 | + const prevEnv = { url: process.env.OS_CLOUD_URL, key: process.env.OS_CLOUD_API_KEY }; |
| 41 | + afterEach(async () => { |
| 42 | + vi.unstubAllGlobals(); |
| 43 | + process.env.OS_CLOUD_URL = prevEnv.url; |
| 44 | + process.env.OS_CLOUD_API_KEY = prevEnv.key; |
| 45 | + if (dir) await rm(dir, { recursive: true, force: true }); |
| 46 | + }); |
| 47 | + |
| 48 | + it("publishes when the CLI's filename constant and the runtime's differ, and uploads the entry's document key for key", async () => { |
| 49 | + const files: ArchiveFile[] = [ |
| 50 | + { path: 'dist/index.mjs', data: new Uint8Array(Buffer.from('export const x=1;\n')) }, |
| 51 | + { path: RUNTIME_MANIFEST_FILE, data: new Uint8Array(Buffer.from(manifestText)) }, |
| 52 | + ]; |
| 53 | + dir = await mkdtemp(join(tmpdir(), 'plugin-publish-entry-')); |
| 54 | + const artifactPath = join(dir, 'com.acme.demo-1.2.0.osplugin'); |
| 55 | + await writeFile(artifactPath, new Uint8Array(createTarGz(files))); |
| 56 | + process.env.OS_CLOUD_URL = 'http://cloud.test'; |
| 57 | + process.env.OS_CLOUD_API_KEY = 'tok_123'; |
| 58 | + const bodies: any[] = []; |
| 59 | + vi.stubGlobal('fetch', vi.fn(async (url: string, init: any) => { |
| 60 | + bodies.push(JSON.parse(init.body)); |
| 61 | + const data = url.endsWith('/versions') ? { version: '1.2.0' } : { id: 'pkg_1', created: true }; |
| 62 | + return { ok: true, status: 200, json: async () => ({ success: true, data }), statusText: 'OK' } as any; |
| 63 | + })); |
| 64 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 65 | + try { |
| 66 | + await PluginPublish.run([artifactPath]); |
| 67 | + } finally { |
| 68 | + logSpy.mockRestore(); |
| 69 | + } |
| 70 | + expect(bodies).toHaveLength(2); |
| 71 | + expect(JSON.stringify(bodies[1].plugin_manifest)).toBe(JSON.stringify(JSON.parse(manifestText))); |
| 72 | + }); |
| 73 | +}); |
0 commit comments