-
Notifications
You must be signed in to change notification settings - Fork 0
fix(config): unpin installs frozen on the retired beta-api host #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tonychang04
wants to merge
3
commits into
main
Choose a base branch
from
fix/retired-api-url-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // readGlobal freezes whatever DEFAULT_API was in force at first login into ~/.insta/config.json | ||
| // (persist() writes the resolved object straight back), so installs from v0.0.3..v0.0.16 carry | ||
| // the retired beta-api host permanently and upgrading the binary does not move them. These cover | ||
| // the retirement path and, just as importantly, the values that must NOT be rewritten. | ||
| import { test, expect, beforeEach, afterEach, vi } from 'vitest' | ||
| import { mkdtempSync, mkdirSync, writeFileSync, readFileSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
|
|
||
| const RETIRED = 'https://beta-api.insta.insforge.dev' | ||
| const DEFAULT_API = 'https://api.instacloud.com' | ||
|
|
||
| /** Point HOME at a fresh dir, optionally seeding ~/.insta/config.json. */ | ||
| function seedHome(global: Record<string, unknown> | null): string { | ||
| const home = mkdtempSync(join(tmpdir(), 'insta-home-')) | ||
| process.env.HOME = home | ||
| if (global) { | ||
| mkdirSync(join(home, '.insta'), { recursive: true }) | ||
| writeFileSync(join(home, '.insta', 'config.json'), JSON.stringify(global)) | ||
| } | ||
| return home | ||
| } | ||
|
|
||
| /** GLOBAL_FILE is resolved at module load, so HOME must be set before each import. */ | ||
| const load = () => { vi.resetModules(); return import('../src/config.js') } | ||
|
|
||
| const readRaw = (home: string) => | ||
| JSON.parse(readFileSync(join(home, '.insta', 'config.json'), 'utf8')) as { apiUrl: string } | ||
|
|
||
| beforeEach(() => { delete process.env.INSTA_API_URL }) | ||
| afterEach(() => { delete process.env.INSTA_API_URL; vi.resetModules() }) | ||
|
|
||
| test('a persisted retired host is replaced by the current default', async () => { | ||
| seedHome({ apiUrl: RETIRED, accessToken: 't' }) | ||
| expect((await (await load()).readGlobal()).apiUrl).toBe(DEFAULT_API) | ||
| }) | ||
|
|
||
| test('the session minted by the retired deployment is dropped with it', async () => { | ||
| seedHome({ apiUrl: RETIRED, accessToken: 'a', refreshToken: 'r', user: { id: 'u', email: null, name: null } }) | ||
| const cfg = await (await load()).readGlobal() | ||
| expect(cfg.accessToken).toBeUndefined() | ||
| expect(cfg.refreshToken).toBeUndefined() | ||
| expect(cfg.user).toBeUndefined() | ||
| }) | ||
|
|
||
| test('non-credential settings survive the replacement', async () => { | ||
| seedHome({ apiUrl: RETIRED, accessToken: 't', autoUpdate: false }) | ||
| expect(await (await load()).readGlobal()).toMatchObject({ apiUrl: DEFAULT_API, autoUpdate: false }) | ||
| }) | ||
|
|
||
| test('the notice goes to stderr, so --json output on stdout stays parseable', async () => { | ||
| seedHome({ apiUrl: RETIRED, accessToken: 't' }) | ||
| const err = vi.spyOn(process.stderr, 'write').mockReturnValue(true) | ||
| const out = vi.spyOn(process.stdout, 'write').mockReturnValue(true) | ||
| await (await load()).readGlobal() | ||
| expect(err).toHaveBeenCalledWith(expect.stringContaining(RETIRED)) | ||
| expect(out).not.toHaveBeenCalled() | ||
| err.mockRestore(); out.mockRestore() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: A failed assertion in the stderr/stdout test leaves both process stream spies installed because cleanup is performed only after the assertions. Restoring the spies in a Prompt for AI agents |
||
| }) | ||
|
|
||
| test('a healthy config keeps its session', async () => { | ||
| seedHome({ apiUrl: 'https://api.instacloud.com', accessToken: 'a', refreshToken: 'r' }) | ||
| expect(await (await load()).readGlobal()).toMatchObject({ accessToken: 'a', refreshToken: 'r' }) | ||
| }) | ||
|
|
||
| test('a trailing slash does not smuggle the retired host through', async () => { | ||
| seedHome({ apiUrl: `${RETIRED}/` }) | ||
| expect((await (await load()).readGlobal()).apiUrl).toBe(DEFAULT_API) | ||
| }) | ||
|
|
||
| test('the healed value reaches disk the next time any command persists', async () => { | ||
| const home = seedHome({ apiUrl: RETIRED, accessToken: 't' }) | ||
| expect(readRaw(home).apiUrl).toBe(RETIRED) // precondition: the stale value really is on disk | ||
| const { readGlobal, writeGlobal } = await load() | ||
| await writeGlobal(await readGlobal()) // what upgrade.ts and ApiClient.persist() both do | ||
| expect(readRaw(home).apiUrl).toBe(DEFAULT_API) | ||
| }) | ||
|
|
||
| test('a deliberate self-hosted or localhost apiUrl still wins', async () => { | ||
| seedHome({ apiUrl: 'http://localhost:8080' }) | ||
| expect((await (await load()).readGlobal()).apiUrl).toBe('http://localhost:8080') | ||
| }) | ||
|
|
||
| test('INSTA_API_URL beats both the retired host and the default', async () => { | ||
| seedHome({ apiUrl: RETIRED }) | ||
| process.env.INSTA_API_URL = 'https://api.example.test' | ||
| expect((await (await load()).readGlobal()).apiUrl).toBe('https://api.example.test') | ||
| }) | ||
|
|
||
| test('with no config file at all the default is used', async () => { | ||
| seedHome(null) | ||
| expect((await (await load()).readGlobal()).apiUrl).toBe(DEFAULT_API) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: With
INSTA_API_URLset, retired configurations skip session cleanup and the recovery notice. An authenticated request that gets a 401 can then POST the retired deployment’s refresh token to the env-selected endpoint; detect/clear the retired persisted URL before applying the env API override.Prompt for AI agents