added full api
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { createWorkspace, ensurePersonalWorkspaceForUser } from './workspaceRepository.js';
|
||||
import { mockDb } from '../test/mockDb.js';
|
||||
import type { UserRow } from '../types.js';
|
||||
|
||||
const user: UserRow = {
|
||||
id: 'user-1',
|
||||
email: 'owner@example.com',
|
||||
password_hash: null,
|
||||
name: 'Owner',
|
||||
created_at: '2026-04-14T00:00:00.000Z',
|
||||
};
|
||||
|
||||
test('ensurePersonalWorkspaceForUser returns an existing workspace without creating one', async () => {
|
||||
const { calls } = mockDb({
|
||||
rowCount: 1,
|
||||
rows: [{ workspace_id: 42 }],
|
||||
});
|
||||
|
||||
const workspaceId = await ensurePersonalWorkspaceForUser(user);
|
||||
|
||||
assert.equal(workspaceId, 42);
|
||||
assert.equal(calls.length, 1);
|
||||
assert.match(calls[0].text, /FROM workspace_members/);
|
||||
});
|
||||
|
||||
test('createWorkspace inserts owner membership and returns the created workspace', async () => {
|
||||
const { calls } = mockDb(
|
||||
{ rowCount: 1, rows: [] },
|
||||
{ rowCount: 1, rows: [] },
|
||||
{
|
||||
rowCount: 1,
|
||||
rows: [
|
||||
{
|
||||
id: 9,
|
||||
name: 'My Rescue',
|
||||
workspace_type: 'rescue',
|
||||
billing_email: 'billing@example.com',
|
||||
billing_plan: 'rescue_free',
|
||||
created_at: '2026-04-14T00:00:00.000Z',
|
||||
updated_at: '2026-04-14T00:00:00.000Z',
|
||||
},
|
||||
],
|
||||
},
|
||||
);
|
||||
|
||||
const workspace = await createWorkspace({
|
||||
id: 9,
|
||||
name: 'My Rescue',
|
||||
workspaceType: 'rescue',
|
||||
billingEmail: 'billing@example.com',
|
||||
billingPlan: 'rescue_free',
|
||||
owner: user,
|
||||
});
|
||||
|
||||
assert.equal(workspace?.id, 9);
|
||||
assert.equal(calls.length, 3);
|
||||
assert.match(calls[0].text, /INSERT INTO workspaces/);
|
||||
assert.match(calls[1].text, /INSERT INTO workspace_members/);
|
||||
assert.match(calls[2].text, /SELECT id, name, workspace_type/);
|
||||
});
|
||||
Reference in New Issue
Block a user