Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions docs/remove_session_contract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# remove_session Behavior Contract

## Purpose
Safely and idempotently remove a session from the SQLite session store.

Integration tests run repeatedly. This function must be reliable and safe to call multiple times.

---

## Function Signature

remove_session --db <db_path> --session-id <id>

---

## Required Parameters

--db (required): Path to SQLite database
--session-id (required): ID of session to remove

---

## Success Definition

Success means:

If session exists:
- Session row is deleted
- Exit code 0

If session does NOT exist:
- No-op (safe)
- Exit code 0

Database remains valid and consistent.

---

## Expected DB Changes

DELETE FROM sessions WHERE session_id = ?

No other tables are modified.

---

## Idempotency Guarantee

Calling:

remove_session X
remove_session X

Produces the same final database state as calling it once.

---

## Failure Modes

Missing DB file -> Exit code 1
Invalid arguments -> Exit code 2
SQLite error -> Exit code 3

Errors must log a helpful message to stderr.

---

## Logging Expectations

On success: no output
On no-op: optional info log
On failure: descriptive error message
22 changes: 22 additions & 0 deletions test/helpers/sqlite_fixture.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

create_test_db() {
DB_PATH="$1"

sqlite3 "$DB_PATH" <<EOF
CREATE TABLE sessions (
session_id TEXT PRIMARY KEY,
user_id TEXT,
device_id TEXT,
created_at TEXT
);
Comment on lines +7 to +12
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The schema defined in this fixture does not match the actual internet_sessions table schema. Specifically, the table name is incorrect (sessions vs internet_sessions), and the columns (device_id, created_at) do not align with the system's SQL definition. Tests should use a schema that accurately reflects the production environment.


INSERT INTO sessions VALUES
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The table name should be updated to internet_sessions to match the project schema.

Suggested change
INSERT INTO sessions VALUES
INSERT INTO internet_sessions VALUES

('s1', 'u1', 'd1', '2026-02-01'),
('s2', 'u2', 'd2', '2026-02-01');
EOF
}

count_sessions() {
sqlite3 "$1" "SELECT COUNT(*) FROM sessions;"
}
71 changes: 71 additions & 0 deletions test/remove_session_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash
set -e

source test/helpers/sqlite_fixture.sh

TMP_DB=$(mktemp)

setup() {
rm -f "$TMP_DB"
create_test_db "$TMP_DB"
}

teardown() {
rm -f "$TMP_DB"
}

test_remove_existing_session() {
setup

./remove_session --db "$TMP_DB" --session-id s1

COUNT=$(count_sessions "$TMP_DB")
if [ "$COUNT" -ne 1 ]; then
echo "Expected 1 session remaining"
exit 1
fi

teardown
}

test_remove_nonexistent_session() {
setup

./remove_session --db "$TMP_DB" --session-id does_not_exist

COUNT=$(count_sessions "$TMP_DB")
if [ "$COUNT" -ne 2 ]; then
echo "Expected 2 sessions remaining"
exit 1
fi

teardown
}

test_idempotency() {
setup

./remove_session --db "$TMP_DB" --session-id s1
./remove_session --db "$TMP_DB" --session-id s1

COUNT=$(count_sessions "$TMP_DB")
if [ "$COUNT" -ne 1 ]; then
echo "Idempotency failed"
exit 1
fi

teardown
}

test_missing_db() {
./remove_session --db missing.db --session-id s1 && exit 1
}

echo "Running remove_session tests..."

test_remove_existing_session
test_remove_nonexistent_session
test_idempotency
test_missing_db

echo "All tests passed."
Loading