Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
erikb-stripe committed Dec 10, 2024
1 parent 21984fd commit 6587fd0
Showing 1 changed file with 178 additions and 130 deletions.
308 changes: 178 additions & 130 deletions test/suite/stripeTerminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,141 +4,189 @@ import * as vscode from 'vscode';
import {StripeTerminal} from '../../src/stripeTerminal';

suite('stripeTerminal', function () {
this.timeout(20000);

let sandbox: sinon.SinonSandbox;

const terminalStub = <vscode.Terminal><unknown>{
name: 'Stubbed Terminal',
processId: Promise.resolve(undefined),
creationOptions: {},
exitStatus: undefined,
sendText: (text: string, addNewLine?: boolean) => { },
show: (preserveFocus?: boolean) => { },
hide: () => { },
dispose: () => { },
};

setup(() => {
sandbox = sinon.createSandbox();
});

teardown(() => {
sandbox.restore();
});

['/usr/local/bin/stripe', '/custom/path/to/stripe'].forEach((path) => {
suite(`when the Stripe CLI is installed at ${path}`, () => {
test('runs command with valid project name', async () => {
const executeTaskSpy = sandbox.spy(vscode.tasks, 'executeTask');
sandbox.stub(terminalStub, 'sendText');
sandbox.stub(vscode.window, 'createTerminal').returns(terminalStub);

// Mock the configuration with a valid project name
const stripeClientStub = <any>{
getCLIPath: () => { },
isAuthenticated: () => true,
};

// Mock the getConfiguration function to return an invalid project name
// Mock the getConfiguration function to return a configuration object with a specific project name
sandbox.stub(vscode.workspace, 'getConfiguration').returns({
get: (key: string) => {
if (key === 'projectName') {
return 'Valid_Project-Name'; // or 'Invalid Project Name!' for the invalid test
}
return null; // Return null for any other keys
},
});
this.timeout(20000);

let sandbox: sinon.SinonSandbox;

const terminalStub = <vscode.Terminal>(<unknown>{
name: 'Stubbed Terminal',
processId: Promise.resolve(undefined),
creationOptions: {},
exitStatus: undefined,
sendText: (text: string, addNewLine?: boolean) => {},
show: (preserveFocus?: boolean) => {},
hide: () => {},
dispose: () => {},
});

setup(() => {
sandbox = sinon.createSandbox();
});

teardown(() => {
sandbox.restore();
});

['/usr/local/bin/stripe', '/custom/path/to/stripe'].forEach((path) => {
suite(`when the Stripe CLI is installed at ${path}`, () => {
test('runs command with valid project name', async () => {
const executeTaskSpy = sandbox.spy(vscode.tasks, 'executeTask');
sandbox.stub(terminalStub, 'sendText');
sandbox.stub(vscode.window, 'createTerminal').returns(terminalStub);

// Mock the configuration with a valid project name
const stripeClientStub = <any>{
getCLIPath: () => {},
isAuthenticated: () => true,
};

// Mock the getConfiguration function to return an invalid project name
sandbox.stub(vscode.workspace, 'getConfiguration').returns({
get: (key: string) => {
if (key === 'projectName') {
return 'Valid_Project-Name'; // Invalid project name
}
return null;
},
has: function (section: string): boolean {
throw new Error('Function not implemented.');
},
inspect: function <T>(
section: string,
):
| {
key: string;
defaultValue?: T;
globalValue?: T;
workspaceValue?: T;
workspaceFolderValue?: T;
defaultLanguageValue?: T;
globalLanguageValue?: T;
workspaceLanguageValue?: T;
workspaceFolderLanguageValue?: T;
languageIds?: string[];
}
| undefined {
throw new Error('Function not implemented.');
},
update: function (
section: string,
value: any,
configurationTarget?: vscode.ConfigurationTarget | boolean | null,
overrideInLanguage?: boolean,
): Thenable<void> {
throw new Error('Function not implemented.');
},
});

sandbox.stub(stripeClientStub, 'getCLIPath').returns(Promise.resolve(path));

const stripeTerminal = new StripeTerminal(stripeClientStub);
await stripeTerminal.execute('listen', ['--forward-to', 'localhost']);

assert.strictEqual(executeTaskSpy.callCount, 1);
assert.deepStrictEqual(executeTaskSpy.args[0], [
new vscode.Task(
{type: 'stripe', command: 'listen'},
vscode.TaskScope.Workspace,
'listen',
'stripe',
new vscode.ShellExecution(path, [
'listen',
'--forward-to',
'localhost',
'--project-name',
'Valid_Project-Name'
],
{
shellQuoting: {
escape: {
escapeChar: '\\',
charsToEscape: '&`|"\'',
},
},
}),
),
]);
});

test('throws error for invalid project name', async () => {
// Mock the configuration with an invalid project name
const stripeClientStub = <any>{
getCLIPath: () => { },
isAuthenticated: () => true,
};

// Mock the getConfiguration function to return an invalid project name
sandbox.stub(vscode.workspace, 'getConfiguration').returns({
get: (key: string) => {
if (key === 'projectName') {
return 'Invalid Project Name!'; // Invalid project name
}
return null;
},
has: function (section: string): boolean {
throw new Error('Function not implemented.');
sandbox.stub(stripeClientStub, 'getCLIPath').returns(Promise.resolve(path));

const stripeTerminal = new StripeTerminal(stripeClientStub);
await stripeTerminal.execute('listen', ['--forward-to', 'localhost']);

assert.strictEqual(executeTaskSpy.callCount, 1);
assert.deepStrictEqual(executeTaskSpy.args[0], [
new vscode.Task(
{type: 'stripe', command: 'listen'},
vscode.TaskScope.Workspace,
'listen',
'stripe',
new vscode.ShellExecution(
path,
['listen', '--forward-to', 'localhost', '--project-name', 'Valid_Project-Name'],
{
shellQuoting: {
escape: {
escapeChar: '\\',
charsToEscape: '&`|"\'',
},
inspect: function <T>(section: string): { key: string; defaultValue?: T; globalValue?: T; workspaceValue?: T; workspaceFolderValue?: T; defaultLanguageValue?: T; globalLanguageValue?: T; workspaceLanguageValue?: T; workspaceFolderLanguageValue?: T; languageIds?: string[] } | undefined {
throw new Error('Function not implemented.');
},
update: function (section: string, value: any, configurationTarget?: vscode.ConfigurationTarget | boolean | null, overrideInLanguage?: boolean): Thenable<void> {
throw new Error('Function not implemented.');
}
});

sandbox.stub(vscode.window, 'createTerminal').returns(terminalStub);
sandbox.stub(stripeClientStub, 'getCLIPath').returns(Promise.resolve(path));

const stripeTerminal = new StripeTerminal(stripeClientStub);

// Expect an error to be thrown due to invalid project name
await assert.rejects(
async () => {
await stripeTerminal.execute('listen', ['--forward-to', 'localhost']);
},
{
name: 'Error',
message: "Invalid project name: 'Invalid Project Name!'. Project names can only contain letters, numbers, spaces, underscores, and hyphens.",
}
);
});
},
},
),
),
]);
});

test('throws error for invalid project name', async () => {
// Mock the configuration with an invalid project name
const stripeClientStub = <any>{
getCLIPath: () => {},
isAuthenticated: () => true,
};

// Mock the getConfiguration function to return an invalid project name
sandbox.stub(vscode.workspace, 'getConfiguration').returns({
get: (key: string) => {
if (key === 'projectName') {
return 'Invalid Project Name!'; // Invalid project name
}
return null;
},
has: function (section: string): boolean {
throw new Error('Function not implemented.');
},
inspect: function <T>(
section: string,
):
| {
key: string;
defaultValue?: T;
globalValue?: T;
workspaceValue?: T;
workspaceFolderValue?: T;
defaultLanguageValue?: T;
globalLanguageValue?: T;
workspaceLanguageValue?: T;
workspaceFolderLanguageValue?: T;
languageIds?: string[];
}
| undefined {
throw new Error('Function not implemented.');
},
update: function (
section: string,
value: any,
configurationTarget?: vscode.ConfigurationTarget | boolean | null,
overrideInLanguage?: boolean,
): Thenable<void> {
throw new Error('Function not implemented.');
},
});
});

suite('with no Stripe CLI installed', () => {
test('does not run command', async () => {
const sendTextStub = sandbox.stub(terminalStub, 'sendText');
const createTerminalStub = sandbox.stub(vscode.window, 'createTerminal').returns(terminalStub);
const stripeClientStub = <any>{getCLIPath: () => { }, isAuthenticated: () => true};
sandbox.stub(stripeClientStub, 'getCLIPath').returns(null);
sandbox.stub(vscode.window, 'createTerminal').returns(terminalStub);
sandbox.stub(stripeClientStub, 'getCLIPath').returns(Promise.resolve(path));

const stripeTerminal = new StripeTerminal(stripeClientStub);
await stripeTerminal.execute('listen', ['--forward-to', 'localhost']);
const stripeTerminal = new StripeTerminal(stripeClientStub);

assert.strictEqual(createTerminalStub.callCount, 0);
assert.deepStrictEqual(sendTextStub.callCount, 0);
});
// Expect an error to be thrown due to invalid project name
await assert.rejects(
async () => {
await stripeTerminal.execute('listen', ['--forward-to', 'localhost']);
},
{
name: 'Error',
message:
"Invalid project name: 'Invalid Project Name!'. Project names can only contain letters, numbers, spaces, underscores, and hyphens.",
},
);
});
});
});

suite('with no Stripe CLI installed', () => {
test('does not run command', async () => {
const sendTextStub = sandbox.stub(terminalStub, 'sendText');
const createTerminalStub = sandbox
.stub(vscode.window, 'createTerminal')
.returns(terminalStub);
const stripeClientStub = <any>{getCLIPath: () => {}, isAuthenticated: () => true};
sandbox.stub(stripeClientStub, 'getCLIPath').returns(null);

const stripeTerminal = new StripeTerminal(stripeClientStub);
await stripeTerminal.execute('listen', ['--forward-to', 'localhost']);

assert.strictEqual(createTerminalStub.callCount, 0);
assert.deepStrictEqual(sendTextStub.callCount, 0);
});
});
});

0 comments on commit 6587fd0

Please sign in to comment.