Add VSCode task for automatic build on project open and update dependencies

Remove .vscode from .gitignore to allow for project-specific settings. Introduce a VSCode task to run `npm run dev` automatically when the project is opened. Update environment configuration files and dependencies to enhance support for the Node adapter.

Fixes #13
This commit is contained in:
GitHub Copilot
2025-05-01 21:05:24 +00:00
parent 8058df41fd
commit b51fe35a16
23 changed files with 342 additions and 401 deletions

View File

@@ -1,60 +1,54 @@
import { describe, it, expect } from "vitest";
import { GET as listAccounts } from "../pages/api/accounts/index";
import { GET as getAccount } from "../pages/api/accounts/[id]/index";
import { GET as listTransactions } from "../pages/api/accounts/[id]/transactions/index";
import { createMockAPIContext } from "./setup";
import { describe, it, expect } from 'vitest';
import { GET as listAccounts } from '../pages/api/accounts/index';
import { GET as getAccount } from '../pages/api/accounts/[id]/index';
import { GET as listTransactions } from '../pages/api/accounts/[id]/transactions/index';
import { createMockAPIContext } from './setup';
describe("Accounts API", () => {
describe("GET /api/accounts", () => {
it("should return all accounts", async () => {
describe('Accounts API', () => {
describe('GET /api/accounts', () => {
it('should return all accounts', async () => {
const response = await listAccounts(createMockAPIContext() as any);
const accounts = await response.json();
expect(response.status).toBe(200);
expect(accounts).toHaveLength(2);
expect(accounts[0]).toHaveProperty("id", "1");
expect(accounts[1]).toHaveProperty("id", "2");
expect(accounts[0]).toHaveProperty('id', '1');
expect(accounts[1]).toHaveProperty('id', '2');
});
});
describe("GET /api/accounts/:id", () => {
it("should return a specific account", async () => {
const response = await getAccount(
createMockAPIContext({ params: { id: "1" } }) as any
);
describe('GET /api/accounts/:id', () => {
it('should return a specific account', async () => {
const response = await getAccount(createMockAPIContext({ params: { id: '1' } }) as any);
const account = await response.json();
expect(response.status).toBe(200);
expect(account).toHaveProperty("id", "1");
expect(account).toHaveProperty("name", "Test Checking");
expect(account).toHaveProperty('id', '1');
expect(account).toHaveProperty('name', 'Test Checking');
});
it("should return 404 for non-existent account", async () => {
const response = await getAccount(
createMockAPIContext({ params: { id: "999" } }) as any
);
it('should return 404 for non-existent account', async () => {
const response = await getAccount(createMockAPIContext({ params: { id: '999' } }) as any);
const error = await response.json();
expect(response.status).toBe(404);
expect(error).toHaveProperty("error", "Account not found");
expect(error).toHaveProperty('error', 'Account not found');
});
});
describe("GET /api/accounts/:id/transactions", () => {
it("should return transactions for an account", async () => {
const response = await listTransactions(
createMockAPIContext({ params: { id: "1" } }) as any
);
describe('GET /api/accounts/:id/transactions', () => {
it('should return transactions for an account', async () => {
const response = await listTransactions(createMockAPIContext({ params: { id: '1' } }) as any);
const transactions = await response.json();
expect(response.status).toBe(200);
expect(transactions).toHaveLength(1);
expect(transactions[0]).toHaveProperty("accountId", "1");
expect(transactions[0]).toHaveProperty('accountId', '1');
});
it("should return empty array for account with no transactions", async () => {
it('should return empty array for account with no transactions', async () => {
const response = await listTransactions(
createMockAPIContext({ params: { id: "999" } }) as any
createMockAPIContext({ params: { id: '999' } }) as any
);
const transactions = await response.json();

View File

@@ -1,27 +1,27 @@
import { beforeEach } from "vitest";
import { accounts, transactions } from "../data/store";
import type { APIContext } from "astro";
import { beforeEach } from 'vitest';
import { accounts, transactions } from '../data/store';
import type { APIContext } from 'astro';
// Create a mock APIContext factory
export function createMockAPIContext<
T extends Record<string, string> = Record<string, string>
>({ params = {} as T } = {}): Partial<APIContext> {
export function createMockAPIContext<T extends Record<string, string> = Record<string, string>>({
params = {} as T,
} = {}): Partial<APIContext> {
return {
params,
props: {},
request: new Request("http://localhost:4321"),
site: new URL("http://localhost:4321"),
generator: "test",
url: new URL("http://localhost:4321"),
clientAddress: "127.0.0.1",
request: new Request('http://localhost:4321'),
site: new URL('http://localhost:4321'),
generator: 'test',
url: new URL('http://localhost:4321'),
clientAddress: '127.0.0.1',
cookies: new Headers() as any, // Cast Headers to cookies as we don't need cookie functionality in tests
redirect: () => new Response(),
locals: {},
preferredLocale: undefined,
preferredLocaleList: [],
currentLocale: undefined,
routePattern: "/api/[...path]",
originPathname: "/api",
routePattern: '/api/[...path]',
originPathname: '/api',
getActionResult: () => undefined,
isPrerendered: false,
};
@@ -33,15 +33,15 @@ beforeEach(() => {
accounts.length = 0;
accounts.push(
{
id: "1",
name: "Test Checking",
last4: "1234",
id: '1',
name: 'Test Checking',
last4: '1234',
balance: 1000.0,
},
{
id: "2",
name: "Test Savings",
last4: "5678",
id: '2',
name: 'Test Savings',
last4: '5678',
balance: 5000.0,
}
);
@@ -50,17 +50,17 @@ beforeEach(() => {
transactions.length = 0;
transactions.push(
{
id: "1",
accountId: "1",
date: "2025-04-24",
description: "Test Transaction 1",
id: '1',
accountId: '1',
date: '2025-04-24',
description: 'Test Transaction 1',
amount: -50.0,
},
{
id: "2",
accountId: "2",
date: "2025-04-24",
description: "Test Transaction 2",
id: '2',
accountId: '2',
date: '2025-04-24',
description: 'Test Transaction 2',
amount: 100.0,
}
);

View File

@@ -7,31 +7,31 @@
// - Add load testing for API endpoints
// - Implement test data factories
import { describe, it, expect } from "vitest";
import { POST as createTransaction } from "../pages/api/transactions/index";
import { describe, it, expect } from 'vitest';
import { POST as createTransaction } from '../pages/api/transactions/index';
import {
PUT as updateTransaction,
DELETE as deleteTransaction,
} from "../pages/api/transactions/[id]/index";
import { accounts, transactions } from "../data/store";
import type { Transaction } from "../types";
import { createMockAPIContext } from "./setup";
} from '../pages/api/transactions/[id]/index';
import { accounts, transactions } from '../data/store';
import type { Transaction } from '../types';
import { createMockAPIContext } from './setup';
describe("Transactions API", () => {
describe("POST /api/transactions", () => {
it("should create a new transaction", async () => {
describe('Transactions API', () => {
describe('POST /api/transactions', () => {
it('should create a new transaction', async () => {
const initialBalance = accounts[0].balance;
const newTransaction = {
accountId: "1",
date: "2025-04-24",
description: "Test New Transaction",
accountId: '1',
date: '2025-04-24',
description: 'Test New Transaction',
amount: -25.0,
};
const ctx = createMockAPIContext() as any;
ctx.request = new Request("http://localhost:4321/api/transactions", {
method: "POST",
headers: { "Content-Type": "application/json" },
ctx.request = new Request('http://localhost:4321/api/transactions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newTransaction),
});
@@ -39,21 +39,21 @@ describe("Transactions API", () => {
const result = await response.json();
expect(response.status).toBe(201);
expect(result).toHaveProperty("id");
expect(result).toHaveProperty('id');
expect(result.description).toBe(newTransaction.description);
expect(accounts[0].balance).toBe(initialBalance + newTransaction.amount);
});
it("should reject transaction with missing fields", async () => {
it('should reject transaction with missing fields', async () => {
const invalidTransaction = {
accountId: "1",
accountId: '1',
// Missing required fields
};
const ctx = createMockAPIContext() as any;
ctx.request = new Request("http://localhost:4321/api/transactions", {
method: "POST",
headers: { "Content-Type": "application/json" },
ctx.request = new Request('http://localhost:4321/api/transactions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(invalidTransaction),
});
@@ -61,21 +61,21 @@ describe("Transactions API", () => {
const error = await response.json();
expect(response.status).toBe(400);
expect(error).toHaveProperty("error", "Missing required fields");
expect(error).toHaveProperty('error', 'Missing required fields');
});
it("should reject transaction with invalid account", async () => {
it('should reject transaction with invalid account', async () => {
const invalidTransaction = {
accountId: "999",
date: "2025-04-24",
description: "Invalid Account Test",
accountId: '999',
date: '2025-04-24',
description: 'Invalid Account Test',
amount: 100,
};
const ctx = createMockAPIContext() as any;
ctx.request = new Request("http://localhost:4321/api/transactions", {
method: "POST",
headers: { "Content-Type": "application/json" },
ctx.request = new Request('http://localhost:4321/api/transactions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(invalidTransaction),
});
@@ -83,38 +83,38 @@ describe("Transactions API", () => {
const error = await response.json();
expect(response.status).toBe(404);
expect(error).toHaveProperty("error", "Account not found");
expect(error).toHaveProperty('error', 'Account not found');
});
it("should reject invalid request body", async () => {
it('should reject invalid request body', async () => {
const ctx = createMockAPIContext() as any;
ctx.request = new Request("http://localhost:4321/api/transactions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: "invalid json",
ctx.request = new Request('http://localhost:4321/api/transactions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: 'invalid json',
});
const response = await createTransaction(ctx);
const error = await response.json();
expect(response.status).toBe(400);
expect(error).toHaveProperty("error", "Invalid request body");
expect(error).toHaveProperty('error', 'Invalid request body');
});
});
describe("PUT /api/transactions/:id", () => {
it("should update an existing transaction", async () => {
describe('PUT /api/transactions/:id', () => {
it('should update an existing transaction', async () => {
const initialBalance = accounts[0].balance;
const originalAmount = transactions[0].amount;
const updates = {
description: "Updated Description",
description: 'Updated Description',
amount: -75.0,
};
const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
ctx.request = new Request("http://localhost:4321/api/transactions/1", {
method: "PUT",
headers: { "Content-Type": "application/json" },
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updates),
});
@@ -124,49 +124,47 @@ describe("Transactions API", () => {
expect(response.status).toBe(200);
expect(result.description).toBe(updates.description);
expect(result.amount).toBe(updates.amount);
expect(accounts[0].balance).toBe(
initialBalance - originalAmount + updates.amount
);
expect(accounts[0].balance).toBe(initialBalance - originalAmount + updates.amount);
});
it("should reject update with invalid request body", async () => {
const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
ctx.request = new Request("http://localhost:4321/api/transactions/1", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: "invalid json",
it('should reject update with invalid request body', async () => {
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: 'invalid json',
});
const response = await updateTransaction(ctx);
const error = await response.json();
expect(response.status).toBe(400);
expect(error).toHaveProperty("error", "Invalid request body");
expect(error).toHaveProperty('error', 'Invalid request body');
});
it("should reject update for non-existent transaction", async () => {
const ctx = createMockAPIContext({ params: { id: "999" } }) as any;
ctx.request = new Request("http://localhost:4321/api/transactions/999", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ description: "Test" }),
it('should reject update for non-existent transaction', async () => {
const ctx = createMockAPIContext({ params: { id: '999' } }) as any;
ctx.request = new Request('http://localhost:4321/api/transactions/999', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ description: 'Test' }),
});
const response = await updateTransaction(ctx);
const error = await response.json();
expect(response.status).toBe(404);
expect(error).toHaveProperty("error", "Transaction not found");
expect(error).toHaveProperty('error', 'Transaction not found');
});
it("should reject update for non-existent account", async () => {
it('should reject update for non-existent account', async () => {
// First update the transaction to point to a non-existent account
transactions[0].accountId = "999";
transactions[0].accountId = '999';
const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
ctx.request = new Request("http://localhost:4321/api/transactions/1", {
method: "PUT",
headers: { "Content-Type": "application/json" },
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ amount: -100 }),
});
@@ -174,25 +172,25 @@ describe("Transactions API", () => {
const error = await response.json();
expect(response.status).toBe(404);
expect(error).toHaveProperty("error", "Account not found");
expect(error).toHaveProperty('error', 'Account not found');
// Reset account ID for other tests
transactions[0].accountId = "1";
transactions[0].accountId = '1';
});
it("should handle account balance updates correctly when switching accounts", async () => {
it('should handle account balance updates correctly when switching accounts', async () => {
// Create initial state
const oldAccount = accounts[0];
const newAccount = accounts[1];
const initialOldBalance = oldAccount.balance;
const initialNewBalance = newAccount.balance;
const oldTransaction = transactions.find((t) => t.id === "1");
if (!oldTransaction) throw new Error("Test transaction not found");
const oldTransaction = transactions.find((t) => t.id === '1');
if (!oldTransaction) throw new Error('Test transaction not found');
const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
ctx.request = new Request("http://localhost:4321/api/transactions/1", {
method: "PUT",
headers: { "Content-Type": "application/json" },
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
accountId: newAccount.id,
amount: -100,
@@ -206,41 +204,36 @@ describe("Transactions API", () => {
expect(result.accountId).toBe(newAccount.id);
// Old account should have the old amount removed
expect(oldAccount.balance).toBe(
initialOldBalance + Math.abs(oldTransaction.amount)
);
expect(oldAccount.balance).toBe(initialOldBalance + Math.abs(oldTransaction.amount));
// New account should have the new amount added
expect(newAccount.balance).toBe(initialNewBalance - 100);
});
it("should reject update without transaction ID", async () => {
it('should reject update without transaction ID', async () => {
const ctx = createMockAPIContext() as any;
ctx.request = new Request(
"http://localhost:4321/api/transactions/undefined",
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ description: "Test" }),
}
);
ctx.request = new Request('http://localhost:4321/api/transactions/undefined', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ description: 'Test' }),
});
const response = await updateTransaction(ctx);
const error = await response.json();
expect(response.status).toBe(400);
expect(error).toHaveProperty("error", "Transaction ID is required");
expect(error).toHaveProperty('error', 'Transaction ID is required');
});
it("should reject update when old account is missing", async () => {
it('should reject update when old account is missing', async () => {
// Store current accounts and clear the array
const savedAccounts = [...accounts];
accounts.length = 0;
const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
ctx.request = new Request("http://localhost:4321/api/transactions/1", {
method: "PUT",
headers: { "Content-Type": "application/json" },
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ amount: -100 }),
});
@@ -248,19 +241,19 @@ describe("Transactions API", () => {
const error = await response.json();
expect(response.status).toBe(404);
expect(error).toHaveProperty("error", "Account not found");
expect(error).toHaveProperty('error', 'Account not found');
// Restore accounts
accounts.push(...savedAccounts);
});
it("should reject update when new account doesn't exist", async () => {
const ctx = createMockAPIContext({ params: { id: "1" } }) as any;
ctx.request = new Request("http://localhost:4321/api/transactions/1", {
method: "PUT",
headers: { "Content-Type": "application/json" },
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
accountId: "999", // Non-existent account
accountId: '999', // Non-existent account
amount: -100,
}),
});
@@ -269,18 +262,18 @@ describe("Transactions API", () => {
const error = await response.json();
expect(response.status).toBe(404);
expect(error).toHaveProperty("error", "Account not found");
expect(error).toHaveProperty('error', 'Account not found');
});
});
describe("DELETE /api/transactions/:id", () => {
it("should delete a transaction", async () => {
describe('DELETE /api/transactions/:id', () => {
it('should delete a transaction', async () => {
const initialBalance = accounts[0].balance;
const transactionAmount = transactions[0].amount;
const initialCount = transactions.length;
const response = await deleteTransaction(
createMockAPIContext({ params: { id: "1" } }) as any
createMockAPIContext({ params: { id: '1' } }) as any
);
expect(response.status).toBe(204);
@@ -288,45 +281,45 @@ describe("Transactions API", () => {
expect(accounts[0].balance).toBe(initialBalance - transactionAmount);
});
it("should reject delete without transaction ID", async () => {
it('should reject delete without transaction ID', async () => {
const response = await deleteTransaction(createMockAPIContext() as any);
const error = await response.json();
expect(response.status).toBe(400);
expect(error).toHaveProperty("error", "Transaction ID is required");
expect(error).toHaveProperty('error', 'Transaction ID is required');
});
it("should return 404 for non-existent transaction", async () => {
it('should return 404 for non-existent transaction', async () => {
const response = await deleteTransaction(
createMockAPIContext({ params: { id: "999" } }) as any
createMockAPIContext({ params: { id: '999' } }) as any
);
const error = await response.json();
expect(response.status).toBe(404);
expect(error).toHaveProperty("error", "Transaction not found");
expect(error).toHaveProperty('error', 'Transaction not found');
});
it("should handle deletion with non-existent account", async () => {
it('should handle deletion with non-existent account', async () => {
// Create a transaction then remove its account
const testTransaction: Transaction = {
id: "test-delete",
accountId: "test-account",
date: "2025-04-24",
description: "Test Delete",
id: 'test-delete',
accountId: 'test-account',
date: '2025-04-24',
description: 'Test Delete',
amount: 100,
};
transactions.push(testTransaction);
const response = await deleteTransaction(
createMockAPIContext({ params: { id: "test-delete" } }) as any
createMockAPIContext({ params: { id: 'test-delete' } }) as any
);
const error = await response.json();
expect(response.status).toBe(404);
expect(error).toHaveProperty("error", "Account not found");
expect(error).toHaveProperty('error', 'Account not found');
});
});
});