-
-
Notifications
You must be signed in to change notification settings - Fork 8
fix(sdk): apply Stricli flag defaults in SDK invoke path #1027
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
Merged
+304
−18
Merged
Changes from all commits
Commits
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,183 @@ | ||
| /** | ||
| * Unit tests for the SDK invoke layer. | ||
| * | ||
| * Focuses on `applyFlagDefaults` which replicates Stricli's default | ||
| * application for the SDK direct-invoke path (bypasses Stricli parsing). | ||
| */ | ||
|
|
||
| import { describe, expect, test } from "vitest"; | ||
| import { applyFlagDefaults, type FlagDef } from "../../src/lib/sdk-invoke.js"; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // applyFlagDefaults — parsed flags with defaults | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe("applyFlagDefaults: parsed flags with defaults", () => { | ||
| test("calls parse on string default when flag is missing", () => { | ||
| const flagDefs: Record<string, FlagDef> = { | ||
| period: { | ||
| kind: "parsed", | ||
| default: "7d", | ||
| parse: (v: string) => ({ type: "relative", period: v }), | ||
| }, | ||
| }; | ||
| const result = applyFlagDefaults({}, flagDefs); | ||
| expect(result.period).toEqual({ type: "relative", period: "7d" }); | ||
| }); | ||
|
|
||
| test("calls parse on string default when flag value is undefined", () => { | ||
| const flagDefs: Record<string, FlagDef> = { | ||
| period: { | ||
| kind: "parsed", | ||
| default: "90d", | ||
| parse: (v: string) => ({ type: "relative", period: v }), | ||
| }, | ||
| }; | ||
| const result = applyFlagDefaults({ period: undefined }, flagDefs); | ||
| expect(result.period).toEqual({ type: "relative", period: "90d" }); | ||
| }); | ||
|
|
||
| test("preserves caller-provided value over default", () => { | ||
| const flagDefs: Record<string, FlagDef> = { | ||
| period: { | ||
| kind: "parsed", | ||
| default: "7d", | ||
| parse: (v: string) => ({ type: "relative", period: v }), | ||
| }, | ||
| }; | ||
| const callerValue = { type: "relative", period: "24h" }; | ||
| const result = applyFlagDefaults({ period: callerValue }, flagDefs); | ||
| expect(result.period).toBe(callerValue); | ||
| }); | ||
|
|
||
| test("handles parse function that returns a number", () => { | ||
| const flagDefs: Record<string, FlagDef> = { | ||
| limit: { | ||
| kind: "parsed", | ||
| default: "25", | ||
| parse: (v: string) => Number(v), | ||
| }, | ||
| }; | ||
| const result = applyFlagDefaults({}, flagDefs); | ||
| expect(result.limit).toBe(25); | ||
| }); | ||
|
|
||
| test("re-throws if parse function rejects its own default", () => { | ||
| const flagDefs: Record<string, FlagDef> = { | ||
| period: { | ||
| kind: "parsed", | ||
| default: "invalid", | ||
| parse: () => { | ||
| throw new Error("parse error"); | ||
| }, | ||
| }, | ||
| }; | ||
| expect(() => applyFlagDefaults({}, flagDefs)).toThrow("parse error"); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // applyFlagDefaults — boolean flags | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe("applyFlagDefaults: boolean flags", () => { | ||
| test("applies raw boolean default", () => { | ||
| const flagDefs: Record<string, FlagDef> = { | ||
| json: { kind: "boolean", default: false }, | ||
| fresh: { kind: "boolean", default: false }, | ||
| }; | ||
| const result = applyFlagDefaults({}, flagDefs); | ||
| expect(result.json).toBe(false); | ||
| expect(result.fresh).toBe(false); | ||
| }); | ||
|
|
||
| test("preserves caller-provided boolean over default", () => { | ||
| const flagDefs: Record<string, FlagDef> = { | ||
| fresh: { kind: "boolean", default: false }, | ||
| }; | ||
| const result = applyFlagDefaults({ fresh: true }, flagDefs); | ||
| expect(result.fresh).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // applyFlagDefaults — enum flags | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe("applyFlagDefaults: enum flags", () => { | ||
| test("applies raw enum default", () => { | ||
| const flagDefs: Record<string, FlagDef> = { | ||
| sort: { kind: "enum", default: "date" }, | ||
| }; | ||
| const result = applyFlagDefaults({}, flagDefs); | ||
| expect(result.sort).toBe("date"); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // applyFlagDefaults — optional flags without defaults | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe("applyFlagDefaults: optional flags without defaults", () => { | ||
| test("does not inject value for optional flag with no default", () => { | ||
| const flagDefs: Record<string, FlagDef> = { | ||
| query: { kind: "parsed", optional: true }, | ||
| }; | ||
| const result = applyFlagDefaults({}, flagDefs); | ||
| expect("query" in result).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // applyFlagDefaults — strips undefined, preserves other falsy values | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe("applyFlagDefaults: undefined stripping", () => { | ||
| test("strips undefined values from input flags", () => { | ||
| const flagDefs: Record<string, FlagDef> = {}; | ||
| const result = applyFlagDefaults( | ||
| { a: undefined, b: null, c: 0, d: "", e: false }, | ||
| flagDefs | ||
| ); | ||
| expect("a" in result).toBe(false); | ||
| expect(result.b).toBeNull(); | ||
| expect(result.c).toBe(0); | ||
| expect(result.d).toBe(""); | ||
| expect(result.e).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // applyFlagDefaults — multiple flags combined | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe("applyFlagDefaults: combined scenario", () => { | ||
| test("applies defaults for missing flags while preserving provided ones", () => { | ||
| const flagDefs: Record<string, FlagDef> = { | ||
| period: { | ||
| kind: "parsed", | ||
| default: "7d", | ||
| parse: (v: string) => ({ type: "relative", period: v }), | ||
| }, | ||
| limit: { | ||
| kind: "parsed", | ||
| default: "25", | ||
| parse: (v: string) => Number(v), | ||
| }, | ||
| sort: { kind: "enum", default: "date" }, | ||
| fresh: { kind: "boolean", default: false }, | ||
| query: { kind: "parsed", optional: true }, | ||
| }; | ||
| const result = applyFlagDefaults({ limit: 50, query: undefined }, flagDefs); | ||
| // period: default applied via parse | ||
| expect(result.period).toEqual({ type: "relative", period: "7d" }); | ||
| // limit: caller value preserved | ||
| expect(result.limit).toBe(50); | ||
| // sort: default applied (raw) | ||
| expect(result.sort).toBe("date"); | ||
| // fresh: default applied (raw) | ||
| expect(result.fresh).toBe(false); | ||
| // query: undefined stripped, no default → absent | ||
| expect("query" in result).toBe(false); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.