Skip to content

Commit

Permalink
feat: Various polish, debug, tests (#16)
Browse files Browse the repository at this point in the history
### **PR Type**
Enhancement, Tests, Bug fix


___

### **Description**
- Introduced new `Debug` component for resource monitoring.

- Enhanced `Build` and `Apply` commands with UI improvements.

- Added comprehensive unit tests for `Apply`, `Build`, and `Watch`
commands.

- Improved database connection handling and error reporting.


___



### **Changes walkthrough** 📝
<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Tests</strong></td><td><details><summary>3
files</summary><table>
<tr>
<td><strong>apply.test.tsx</strong><dd><code>Added unit tests for
`Apply` command functionality.</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; </dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-b86a1df589c794b641e68687d765ed5aa2dcc3d195afb6a056844b3dca864ce0">+75/-0</a>&nbsp;
&nbsp; </td>

</tr>

<tr>
<td><strong>build.test.tsx</strong><dd><code>Added unit tests for
`Build` command functionality.</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; </dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-f82cace99bc6ced0d2a9bee725156909094b816f9028e9c764a32e5fab243101">+75/-0</a>&nbsp;
&nbsp; </td>

</tr>

<tr>
<td><strong>watch.test.tsx</strong><dd><code>Enhanced `Watch` command
tests with new scenarios.</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-60b4eaf9ca0b0c4386f9ce027af6b7bac33b7e45dfc50f1d1b8be629da217e09">+12/-3</a>&nbsp;
&nbsp; </td>

</tr>

</table></details></td></tr><tr><td><strong>Enhancement</strong></td><td><details><summary>15
files</summary><table>
<tr>
<td><strong>_app.tsx</strong><dd><code>Integrated `Debug` component and
improved error handling.</code></dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-29ec6bf855cf99d79aafeebd99f52d709ac2e7357bca30c388958211a5de6a00">+13/-18</a>&nbsp;
</td>

</tr>

<tr>
<td><strong>apply.tsx</strong><dd><code>Refactored <code>Apply</code>
command with UI updates and
<code>useTemplateProcessor</code>.</code></dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-048dadad4da211077c0e615f1eb9ad76b62532048e6b57ad644050d9edfc8a6d">+26/-18</a>&nbsp;
</td>

</tr>

<tr>
<td><strong>build.tsx</strong><dd><code>Refactored <code>Build</code>
command with UI updates and
<code>useTemplateProcessor</code>.</code></dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-ef39b25f5c2209ff0076460982d59d05f0e27ce410844ac5750f2aff486c2747">+36/-9</a>&nbsp;
&nbsp; </td>

</tr>

<tr>
<td><strong>index.tsx</strong><dd><code>Updated error handling with
custom theme colors.</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-28dd48060efda88c3ae7ffc80ff0a3fa13de4c93e5d0042cedbd658cfa448a9a">+2/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
<td><strong>register.tsx</strong><dd><code>Enhanced error and success
message styling.</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-30f4d718aa6db4264ee4211ae37fe659b89605f0763d4a11948373b678af2c8a">+6/-5</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
<td><strong>watch.tsx</strong><dd><code>Improved `Watch` command with
toggling logs and better UI.</code></dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-94584737db839cc08efd961ac278439f2b37c97054e7e5d5849b6d2e94c76122">+52/-32</a>&nbsp;
</td>

</tr>

<tr>
<td><strong>Branding.tsx</strong><dd><code>Updated branding component
with dynamic color themes.</code>&nbsp; &nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-4467c117bddbb0b0bdde09ca8c76c92d24b52bc29a0539746c43d85a772d611b">+7/-7</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
<td><strong>Debug.tsx</strong><dd><code>Added `Debug` component for
monitoring resources and database stats.</code></dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-5071a44bfd2bd2dd3b7464dbdd003d3bf347b5f2ebd31b0896b1f40b9ef737c1">+153/-0</a>&nbsp;
</td>

</tr>

<tr>
<td><strong>ProcessingResults.tsx</strong><dd><code>Introduced
<code>ProcessingResults</code> component for displaying command
results.</code></dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-ff92416245b6cb90aa6d56038986775687fe9b9870db1b447bebf9e0e389dc81">+52/-0</a>&nbsp;
&nbsp; </td>

</tr>

<tr>
<td><strong>Quittable.tsx</strong><dd><code>Adjusted quit instructions
for consistency.</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-6cf1737b6dd7737b14934e9a47e97362b456ce59dd76cf2c78b56925bffa703b">+1/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
<td><strong>customTheme.tsx</strong><dd><code>Added custom theme for
consistent UI styling.</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-909214a57729517b86c109ab37e636127e544d2b6a9ba6c26ead3b9540205043">+68/-0</a>&nbsp;
&nbsp; </td>

</tr>

<tr>
<td><strong>useTemplateProcessor.ts</strong><dd><code>Added
`useTemplateProcessor` hook for template processing.</code></dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-1ff1fba5100d9b9d45cedad25a8422fe651fc0f51ff3e1ef379ccd598cc97d9b">+86/-0</a>&nbsp;
&nbsp; </td>

</tr>

<tr>
<td><strong>templateManager.ts</strong><dd><code>Enhanced template
manager with better error handling and tracking.</code></dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-f9ba80d876577e9d72806578a507f2d6f0ae9469a7d2272b26c0ec98666a3a72">+33/-6</a>&nbsp;
&nbsp; </td>

</tr>

<tr>
<td><strong>types.ts</strong><dd><code>Updated types to include new
fields for processing results.</code></dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-c54113cf61ec99691748a3890bfbeb00e10efb3f0a76f03a0fd9ec49072e410a">+3/-0</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
<td><strong>store.ts</strong><dd><code>Added persistent store for user
preferences.</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-bb40fa4d6485c08b7cf51cc105643ade6d2ac4d5767c5caf3ad3ecc064c03366">+10/-0</a>&nbsp;
&nbsp; </td>

</tr>
</table></details></td></tr><tr><td><strong>Bug
fix</strong></td><td><details><summary>2 files</summary><table>
<tr>
<td><strong>useDatabaseConnection.ts</strong><dd><code>Improved database
connection handling and retry logic.</code>&nbsp; &nbsp; &nbsp;
</dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-d5f7b365d316d789ec4f5b2c4d824e1b73f6a02be6907848f8fd4f9e0eb5e0c6">+18/-32</a>&nbsp;
</td>

</tr>

<tr>
<td><strong>databaseConnection.ts</strong><dd><code>Improved database
connection pool management and error handling.</code></dd></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-f5a92b0e9524e3bfefb69804041a22b982e087192ea0fb4763cec2308f593142">+34/-19</a>&nbsp;
</td>

</tr>
</table></details></td></tr><tr><td><strong>Additional
files</strong></td><td><details><summary>5 files</summary><table>
<tr>
  <td><strong>README.md</strong></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5">+1/-3</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td><strong>package-lock.json</strong></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-053150b640a7ce75eff69d1a22cae7f0f94ad64ce9a855db544dda0929316519">+143/-2</a>&nbsp;
</td>

</tr>

<tr>
  <td><strong>package.json</strong></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519">+2/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td><strong>tsconfig.json</strong></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-b55cdbef4907b7045f32cc5360d48d262cca5f94062e353089f189f4460039e0">+0/-1</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>

<tr>
  <td><strong>vitest.config.ts</strong></td>
<td><a
href="https://github.com/t1mmen/srtd/pull/16/files#diff-2ee894bf23aa44ff4ce12a3da8af19ab20180474ebf2176dbe5f2eea3f96dc92">+1/-0</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></details></td></tr></tr></tbody></table>

___

> 💡 **PR-Agent usage**: Comment `/help "your question"` on any pull
request to receive relevant information
  • Loading branch information
t1mmen authored Jan 2, 2025
1 parent 3f0547c commit 6a733b7
Show file tree
Hide file tree
Showing 28 changed files with 925 additions and 163 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-games-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@t1mmen/srtd": patch
---

Add debug component to test db connections/watchers/etc
5 changes: 5 additions & 0 deletions .changeset/spicy-hats-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@t1mmen/srtd": patch
---

Streamline build & apply, allow build to also apply.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,8 @@ supabase migration up # Apply using Supabase CLI

## The Power of Templates 💪

Without templates, the smallest change to a function would show up as a complete rewrite in your version control system. With templates, the diff is clear and concise.

Templates make code reviews meaningful. Consider this PR adding priority to a notification function:

Without templates, this would appear as a complete rewrite in your PR.

### Perfect For 🎯

Expand Down
145 changes: 143 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"description": "Supabase Repeatable Template Definitions (srtd): 🪄 Live-reloading SQL templates for Supabase DX. Make your database changes reviewable and migrations maintainable! 🚀",
"bin": {
"srtd": "dist/cli.js"
"srtd": "dist/src/cli.js"
},
"type": "module",
"engines": {
Expand Down Expand Up @@ -68,6 +68,7 @@
"dependencies": {
"@inkjs/ui": "^2.0.0",
"chokidar": "^4.0.3",
"conf": "^13.1.0",
"glob": "^10.0.0",
"ink": "^5.1.0",
"pastel": "^3.0.0",
Expand Down
75 changes: 75 additions & 0 deletions src/__tests__/apply.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import fs from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { render } from 'ink-testing-library';
import React from 'react';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import Apply from '../commands/apply.js';
import { connect } from '../utils/databaseConnection.js';
import { TEST_FN_PREFIX } from './vitest.setup.js';

vi.mock('../hooks/useTemplateProcessor', () => ({
useTemplateProcessor: vi.fn().mockImplementation(() => ({
isProcessing: false,
result: {
built: [],
applied: ['test.sql'],
skipped: [],
errors: [],
},
})),
}));

describe('Apply Command', () => {
const testContext = {
timestamp: Date.now(),
testFunctionName: `${TEST_FN_PREFIX}${Date.now()}`,
testDir: path.join(tmpdir(), `test-apply-command-${Date.now()}`),
};

vi.mock('ink', async importOriginal => {
const actual = (await importOriginal()) as typeof import('ink');
const mockExit = vi.fn();
return {
...actual,
useApp: () => ({ exit: mockExit }),
};
});

async function createTestTemplate(content: string) {
const templateDir = path.join(testContext.testDir, 'test-templates');
await fs.mkdir(templateDir, { recursive: true });
const templatePath = path.join(templateDir, `test-${testContext.timestamp}.sql`);
await fs.writeFile(templatePath, content);
}

beforeEach(async () => {
const validSQL = `
CREATE OR REPLACE FUNCTION ${testContext.testFunctionName}()
RETURNS void AS $$
BEGIN NULL; END;
$$ LANGUAGE plpgsql;
`;
await createTestTemplate(validSQL);
});

afterEach(async () => {
const client = await connect();
try {
await client.query(`DROP FUNCTION IF EXISTS ${testContext.testFunctionName}()`);
} finally {
client.release();
}
await fs.rm(testContext.testDir, { recursive: true, force: true });
});

test('shows progress and success', async () => {
const { lastFrame } = render(<Apply options={{ force: false }} />);
expect(lastFrame()).toMatch(/ test\.sql/);
});

test('handles force flag', async () => {
const { lastFrame } = render(<Apply options={{ force: true }} />);
expect(lastFrame()).toMatch(/ test\.sql/);
});
});
Loading

0 comments on commit 6a733b7

Please sign in to comment.