From 97e79ca3f8ad659991fa943021e823c646a25fa3 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 25 Apr 2026 14:37:01 -0700 Subject: [PATCH 1/3] chore: bump version to 0.8.0-dev --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 027b8ea02..033ff04f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nemar-cli", - "version": "0.7.26", + "version": "0.8.0-dev", "description": "CLI for NEMAR (Neuroelectromagnetic Data Archive and Tools Resource) dataset management", "type": "module", "main": "dist/index.js", From d7fe698da2fead038b69f93ddc22da91f53c70f2 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 25 Apr 2026 14:40:22 -0700 Subject: [PATCH 2/3] Phase 9: flip CLI default API URL to api.nemar.org Source changes: - src/lib/api.ts, src/lib/config.ts, src/commands/auth.ts: DEFAULT_API_URL is now https://api.nemar.org. IS_DEV_BUILD detection (workers.dev substring) is unchanged and still flips for CI-injected dev builds. - src/lib/config.ts: new migrateApiUrl() runs on module load and silently rewrites stored apiUrl entries that match the legacy default. Custom URLs (workers.dev, self-hosted) are untouched. The legacy redirect on the personal Cloudflare worker keeps stale configs working through Phase 11. Test: - test/config-migrate-api-url.test.ts: real integration test against a temp config dir, covers legacy rewrite, idempotent rewrite of already-new URL, and pass-through of dev/custom URLs. - test/setup.ts default updated to api.nemar.org. CI: - .github/workflows/npm-publish.yml: dev-branch sed pattern updated to source from api.nemar.org and target nemar-api-dev.sccn-org.workers.dev (SCCN dev worker, not the personal one). Without this dev npm publishes would silently keep pointing at api.nemar.org. Docs: - scripts/generate-docs.ts: API reference base URL updated. Refs #330 --- .github/workflows/npm-publish.yml | 4 +- scripts/generate-docs.ts | 2 +- src/commands/auth.ts | 2 +- src/lib/api.ts | 2 +- src/lib/config.ts | 35 ++++++++++++++++- test/config-migrate-api-url.test.ts | 58 +++++++++++++++++++++++++++++ test/setup.ts | 2 +- 7 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 test/config-migrate-api-url.test.ts diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index ab86a4cf7..fa9429157 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -142,9 +142,9 @@ jobs: if: steps.version.outputs.branch == 'dev' run: | echo "Replacing production API URL with dev API URL in dist/index.js" - sed -i 's|https://api.osc.earth/nemar|https://nemar-api-dev.shirazi-10f.workers.dev|g' dist/index.js + sed -i 's|https://api.nemar.org|https://nemar-api-dev.sccn-org.workers.dev|g' dist/index.js echo "Verifying replacement:" - grep -c "nemar-api-dev.shirazi-10f.workers.dev" dist/index.js || (echo "ERROR: URL replacement failed" && exit 1) + grep -c "nemar-api-dev.sccn-org.workers.dev" dist/index.js || (echo "ERROR: URL replacement failed" && exit 1) echo "✓ Dev backend URL injected successfully" - name: Publish to npm diff --git a/scripts/generate-docs.ts b/scripts/generate-docs.ts index e97897bc6..f88f9d5a9 100644 --- a/scripts/generate-docs.ts +++ b/scripts/generate-docs.ts @@ -1054,7 +1054,7 @@ NEMAR CLI communicates with the NEMAR API. This reference is for advanced users. ## Base URL \`\`\` -https://api.osc.earth/nemar +https://api.nemar.org \`\`\` ## Authentication diff --git a/src/commands/auth.ts b/src/commands/auth.ts index 16a725a48..fd41315a6 100644 --- a/src/commands/auth.ts +++ b/src/commands/auth.ts @@ -143,7 +143,7 @@ export async function loginAction(options: { key?: string } & ConfirmOptions): P // Store credentials as a named account and set as active storeAccount(result.user.username, { apiKey, - apiUrl: "https://api.osc.earth/nemar", + apiUrl: "https://api.nemar.org", username: result.user.username, email: result.user.email, githubUsername: result.user.github_username, diff --git a/src/lib/api.ts b/src/lib/api.ts index 9542aaa7e..832e09a68 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -9,7 +9,7 @@ import { getConfig } from "./config.js"; import { printMaintenanceBanner } from "./maintenance-banner.js"; import { version } from "./version.js"; -const DEFAULT_API_URL = "https://api.osc.earth/nemar"; +const DEFAULT_API_URL = "https://api.nemar.org"; /** ORCID identifier format: XXXX-XXXX-XXXX-XXXX (last char may be X) */ export const ORCID_REGEX = /^\d{4}-\d{4}-\d{4}-\d{3}[\dX]$/; diff --git a/src/lib/config.ts b/src/lib/config.ts index ca559ef82..f6144027a 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -22,7 +22,15 @@ import { join } from "node:path"; import Conf from "conf"; import { z } from "zod"; -const DEFAULT_API_URL = "https://api.osc.earth/nemar"; +const DEFAULT_API_URL = "https://api.nemar.org"; + +/** + * Old default URL pre-Phase-9. Rewritten to DEFAULT_API_URL on CLI launch by + * migrateApiUrl(). The legacy redirect on personal Cloudflare keeps stale + * configs working too, but we proactively rewrite so users don't ride the + * redirect forever. + */ +const LEGACY_DEFAULT_API_URL = "https://api.osc.earth/nemar"; /** * Standardized config directory: ~/.config/nemar/ on all platforms. @@ -180,6 +188,31 @@ export function migrateConfig(): void { // Run migration on module load migrateConfig(); +/** + * Phase 9 (epic #314): rewrite stored apiUrl entries that still point at the + * pre-cutover personal-account URL. Legacy redirect keeps old configs working, + * but we rewrite proactively so users converge on the canonical host. + * + * Only rewrites *exact* matches of the legacy default. Custom URLs (dev + * builds, self-hosted, workers.dev fallbacks) are left alone. + */ +export function migrateApiUrl(): void { + const accounts = config.get("accounts") as Record | undefined; + if (!accounts) return; + let changed = false; + for (const [name, account] of Object.entries(accounts)) { + if (account?.apiUrl === LEGACY_DEFAULT_API_URL) { + accounts[name] = { ...account, apiUrl: DEFAULT_API_URL }; + changed = true; + } + } + if (changed) { + config.store = { ...config.store, accounts }; + } +} + +migrateApiUrl(); + /** * Get the active account name */ diff --git a/test/config-migrate-api-url.test.ts b/test/config-migrate-api-url.test.ts new file mode 100644 index 000000000..9379a7d9a --- /dev/null +++ b/test/config-migrate-api-url.test.ts @@ -0,0 +1,58 @@ +import { afterAll, beforeAll, describe, expect, test } from "bun:test"; +import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +const LEGACY_URL = "https://api.osc.earth/nemar"; +const NEW_URL = "https://api.nemar.org"; +const DEV_URL = "https://nemar-api-dev.sccn-org.workers.dev"; +const CUSTOM_URL = "https://api.example.com/nemar"; + +// Module-level state in src/lib/config.ts captures NEMAR_CONFIG_DIR at first +// import. Bun caches modules across tests, so we set up the config once, +// import once, and assert all migration scenarios from one snapshot. + +const testDir = join(tmpdir(), `nemar-cfg-migrate-${Date.now()}-${process.pid}`); + +beforeAll(() => { + mkdirSync(testDir, { recursive: true }); + process.env.NEMAR_CONFIG_DIR = testDir; + writeFileSync( + join(testDir, "config.json"), + JSON.stringify({ + activeAccount: "alice", + accounts: { + alice: { apiUrl: LEGACY_URL, apiKey: "k1" }, + bob: { apiUrl: LEGACY_URL, apiKey: "k2" }, + carol: { apiUrl: NEW_URL, apiKey: "k3" }, + dev: { apiUrl: DEV_URL, apiKey: "k4" }, + custom: { apiUrl: CUSTOM_URL, apiKey: "k5" }, + }, + }), + ); +}); + +afterAll(() => { + delete process.env.NEMAR_CONFIG_DIR; + if (existsSync(testDir)) rmSync(testDir, { recursive: true, force: true }); +}); + +describe("migrateApiUrl", () => { + test("importing config.ts triggers migration of all stored apiUrl entries", async () => { + // Module load runs migrateApiUrl() at top level + await import("../src/lib/config.ts"); + + const after = JSON.parse(await Bun.file(join(testDir, "config.json")).text()) as { + accounts: Record; + }; + + // Both legacy URLs rewritten + expect(after.accounts.alice.apiUrl).toBe(NEW_URL); + expect(after.accounts.bob.apiUrl).toBe(NEW_URL); + // Already-correct URL stays + expect(after.accounts.carol.apiUrl).toBe(NEW_URL); + // Non-default URLs untouched + expect(after.accounts.dev.apiUrl).toBe(DEV_URL); + expect(after.accounts.custom.apiUrl).toBe(CUSTOM_URL); + }); +}); diff --git a/test/setup.ts b/test/setup.ts index 9696143f9..23e5ad2a3 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -25,7 +25,7 @@ if (existsSync(envPath)) { // Test configuration export const TEST_CONFIG = { - apiUrl: process.env.TEST_API_URL || "https://api.osc.earth/nemar", + apiUrl: process.env.TEST_API_URL || "https://api.nemar.org", password: process.env.TEST_PASSWORD || "TestPassword123!", adminApiKey: process.env.TEST_ADMIN_API_KEY || "", userApiKey: process.env.TEST_USER_API_KEY || "", From 591fab27e27abebbffb4e1ce640a8c245dd6a713 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 25 Apr 2026 14:42:44 -0700 Subject: [PATCH 3/3] Phase 9 review fixes - Export DEFAULT_API_URL from src/lib/config and use it in auth.ts (was: duplicate hardcoded literal). Eliminates drift risk on the next URL change. Build dedupes the literal too. - Add idempotency note on migrateApiUrl() per reviewer ask. - Test: cover account with no apiUrl field (legacy-flat migration result) to guard the optional-chaining guard. Refs #330 --- src/commands/auth.ts | 3 ++- src/lib/config.ts | 6 +++++- test/config-migrate-api-url.test.ts | 6 ++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/commands/auth.ts b/src/commands/auth.ts index fd41315a6..1e9080382 100644 --- a/src/commands/auth.ts +++ b/src/commands/auth.ts @@ -31,6 +31,7 @@ import { signup, } from "../lib/api.js"; import { + DEFAULT_API_URL, clearAllConfig, clearConfig, getAccounts, @@ -143,7 +144,7 @@ export async function loginAction(options: { key?: string } & ConfirmOptions): P // Store credentials as a named account and set as active storeAccount(result.user.username, { apiKey, - apiUrl: "https://api.nemar.org", + apiUrl: DEFAULT_API_URL, username: result.user.username, email: result.user.email, githubUsername: result.user.github_username, diff --git a/src/lib/config.ts b/src/lib/config.ts index f6144027a..8f42e837e 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -22,7 +22,7 @@ import { join } from "node:path"; import Conf from "conf"; import { z } from "zod"; -const DEFAULT_API_URL = "https://api.nemar.org"; +export const DEFAULT_API_URL = "https://api.nemar.org"; /** * Old default URL pre-Phase-9. Rewritten to DEFAULT_API_URL on CLI launch by @@ -195,6 +195,10 @@ migrateConfig(); * * Only rewrites *exact* matches of the legacy default. Custom URLs (dev * builds, self-hosted, workers.dev fallbacks) are left alone. + * + * Idempotent and safe under concurrent CLI launches: the exact-match guard + * means a second run is a no-op, and Conf writes via atomic file replace, so + * a racing process sees pre- or post-state, never partial. */ export function migrateApiUrl(): void { const accounts = config.get("accounts") as Record | undefined; diff --git a/test/config-migrate-api-url.test.ts b/test/config-migrate-api-url.test.ts index 9379a7d9a..30eff5591 100644 --- a/test/config-migrate-api-url.test.ts +++ b/test/config-migrate-api-url.test.ts @@ -27,6 +27,10 @@ beforeAll(() => { carol: { apiUrl: NEW_URL, apiKey: "k3" }, dev: { apiUrl: DEV_URL, apiKey: "k4" }, custom: { apiUrl: CUSTOM_URL, apiKey: "k5" }, + // Legacy-flat migration produces accounts with no apiUrl when none + // was stored. Optional chaining in migrateApiUrl must not crash here + // and must leave the field absent. + partial: { apiKey: "k6" }, }, }), ); @@ -54,5 +58,7 @@ describe("migrateApiUrl", () => { // Non-default URLs untouched expect(after.accounts.dev.apiUrl).toBe(DEV_URL); expect(after.accounts.custom.apiUrl).toBe(CUSTOM_URL); + // Account with no apiUrl: must not crash, must not invent one + expect(after.accounts.partial.apiUrl).toBeUndefined(); }); });